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