Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 1 | """Test result object""" |
| 2 | |
| 3 | import traceback |
| 4 | |
| 5 | from . import util |
| 6 | |
| 7 | |
| 8 | class TestResult(object): |
| 9 | """Holder for test result information. |
| 10 | |
| 11 | Test results are automatically managed by the TestCase and TestSuite |
| 12 | classes, and do not need to be explicitly manipulated by writers of tests. |
| 13 | |
| 14 | Each instance holds the total number of tests run, and collections of |
| 15 | failures and errors that occurred among those test runs. The collections |
| 16 | contain tuples of (testcase, exceptioninfo), where exceptioninfo is the |
| 17 | formatted traceback of the error that occurred. |
| 18 | """ |
| 19 | def __init__(self): |
| 20 | self.failures = [] |
| 21 | self.errors = [] |
| 22 | self.testsRun = 0 |
| 23 | self.skipped = [] |
| 24 | self.expectedFailures = [] |
| 25 | self.unexpectedSuccesses = [] |
| 26 | self.shouldStop = False |
| 27 | |
| 28 | def startTest(self, test): |
| 29 | "Called when the given test is about to be run" |
| 30 | self.testsRun = self.testsRun + 1 |
| 31 | |
| 32 | def startTestRun(self): |
| 33 | """Called once before any tests are executed. |
| 34 | |
| 35 | See startTest for a method called before each test. |
| 36 | """ |
| 37 | |
| 38 | def stopTest(self, test): |
| 39 | "Called when the given test has been run" |
| 40 | pass |
| 41 | |
| 42 | def stopTestRun(self): |
| 43 | """Called once after all tests are executed. |
| 44 | |
| 45 | See stopTest for a method called after each test. |
| 46 | """ |
| 47 | |
| 48 | def addError(self, test, err): |
| 49 | """Called when an error has occurred. 'err' is a tuple of values as |
| 50 | returned by sys.exc_info(). |
| 51 | """ |
| 52 | self.errors.append((test, self._exc_info_to_string(err, test))) |
| 53 | |
| 54 | def addFailure(self, test, err): |
| 55 | """Called when an error has occurred. 'err' is a tuple of values as |
| 56 | returned by sys.exc_info().""" |
| 57 | self.failures.append((test, self._exc_info_to_string(err, test))) |
| 58 | |
| 59 | def addSuccess(self, test): |
| 60 | "Called when a test has completed successfully" |
| 61 | pass |
| 62 | |
| 63 | def addSkip(self, test, reason): |
| 64 | """Called when a test is skipped.""" |
| 65 | self.skipped.append((test, reason)) |
| 66 | |
| 67 | def addExpectedFailure(self, test, err): |
| 68 | """Called when an expected failure/error occured.""" |
| 69 | self.expectedFailures.append( |
| 70 | (test, self._exc_info_to_string(err, test))) |
| 71 | |
| 72 | def addUnexpectedSuccess(self, test): |
| 73 | """Called when a test was expected to fail, but succeed.""" |
| 74 | self.unexpectedSuccesses.append(test) |
| 75 | |
| 76 | def wasSuccessful(self): |
| 77 | "Tells whether or not this result was a success" |
| 78 | return len(self.failures) == len(self.errors) == 0 |
| 79 | |
| 80 | def stop(self): |
| 81 | "Indicates that the tests should be aborted" |
| 82 | self.shouldStop = True |
| 83 | |
| 84 | def _exc_info_to_string(self, err, test): |
| 85 | """Converts a sys.exc_info()-style tuple of values into a string.""" |
| 86 | exctype, value, tb = err |
| 87 | # Skip test runner traceback levels |
| 88 | while tb and self._is_relevant_tb_level(tb): |
| 89 | tb = tb.tb_next |
| 90 | if exctype is test.failureException: |
| 91 | # Skip assert*() traceback levels |
| 92 | length = self._count_relevant_tb_levels(tb) |
| 93 | return ''.join(traceback.format_exception(exctype, value, tb, length)) |
| 94 | return ''.join(traceback.format_exception(exctype, value, tb)) |
| 95 | |
| 96 | def _is_relevant_tb_level(self, tb): |
| 97 | globs = tb.tb_frame.f_globals |
| 98 | is_relevant = '__name__' in globs and \ |
| 99 | globs["__name__"].startswith("unittest") |
| 100 | del globs |
| 101 | return is_relevant |
| 102 | |
| 103 | def _count_relevant_tb_levels(self, tb): |
| 104 | length = 0 |
| 105 | while tb and not self._is_relevant_tb_level(tb): |
| 106 | length += 1 |
| 107 | tb = tb.tb_next |
| 108 | return length |
| 109 | |
| 110 | def __repr__(self): |
| 111 | return "<%s run=%i errors=%i failures=%i>" % \ |
| 112 | (util.strclass(self.__class__), self.testsRun, len(self.errors), |
| 113 | len(self.failures)) |