blob: 746967ec28a7b652b9d77bd070ee50b96dd58de5 [file] [log] [blame]
Benjamin Petersonbed7d042009-07-19 21:01:52 +00001"""Test result object"""
2
3import traceback
4
5from . import util
6
7
8class 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 """
Benjamin Peterson847a4112010-03-14 15:04:17 +000019 _previousTestClass = None
20 _moduleSetUpFailed = False
21 def __init__(self, stream=None, descriptions=None, verbosity=None):
Benjamin Petersonbed7d042009-07-19 21:01:52 +000022 self.failures = []
23 self.errors = []
24 self.testsRun = 0
25 self.skipped = []
26 self.expectedFailures = []
27 self.unexpectedSuccesses = []
28 self.shouldStop = False
29
Benjamin Peterson847a4112010-03-14 15:04:17 +000030 def printErrors(self):
31 "Called by TestRunner after test run"
32
Benjamin Petersonbed7d042009-07-19 21:01:52 +000033 def startTest(self, test):
34 "Called when the given test is about to be run"
Michael Foord34c94622010-02-10 15:51:42 +000035 self.testsRun += 1
Benjamin Petersonbed7d042009-07-19 21:01:52 +000036
37 def startTestRun(self):
38 """Called once before any tests are executed.
39
40 See startTest for a method called before each test.
41 """
42
43 def stopTest(self, test):
Michael Foord34c94622010-02-10 15:51:42 +000044 """Called when the given test has been run"""
Benjamin Petersonbed7d042009-07-19 21:01:52 +000045
46 def stopTestRun(self):
47 """Called once after all tests are executed.
48
49 See stopTest for a method called after each test.
50 """
51
52 def addError(self, test, err):
53 """Called when an error has occurred. 'err' is a tuple of values as
54 returned by sys.exc_info().
55 """
56 self.errors.append((test, self._exc_info_to_string(err, test)))
57
58 def addFailure(self, test, err):
59 """Called when an error has occurred. 'err' is a tuple of values as
60 returned by sys.exc_info()."""
61 self.failures.append((test, self._exc_info_to_string(err, test)))
62
63 def addSuccess(self, test):
64 "Called when a test has completed successfully"
65 pass
66
67 def addSkip(self, test, reason):
68 """Called when a test is skipped."""
69 self.skipped.append((test, reason))
70
71 def addExpectedFailure(self, test, err):
72 """Called when an expected failure/error occured."""
73 self.expectedFailures.append(
74 (test, self._exc_info_to_string(err, test)))
75
76 def addUnexpectedSuccess(self, test):
77 """Called when a test was expected to fail, but succeed."""
78 self.unexpectedSuccesses.append(test)
79
80 def wasSuccessful(self):
81 "Tells whether or not this result was a success"
82 return len(self.failures) == len(self.errors) == 0
83
84 def stop(self):
85 "Indicates that the tests should be aborted"
86 self.shouldStop = True
87
88 def _exc_info_to_string(self, err, test):
89 """Converts a sys.exc_info()-style tuple of values into a string."""
90 exctype, value, tb = err
91 # Skip test runner traceback levels
92 while tb and self._is_relevant_tb_level(tb):
93 tb = tb.tb_next
94 if exctype is test.failureException:
95 # Skip assert*() traceback levels
96 length = self._count_relevant_tb_levels(tb)
97 return ''.join(traceback.format_exception(exctype, value, tb, length))
98 return ''.join(traceback.format_exception(exctype, value, tb))
99
100 def _is_relevant_tb_level(self, tb):
101 globs = tb.tb_frame.f_globals
102 is_relevant = '__name__' in globs and \
103 globs["__name__"].startswith("unittest")
104 del globs
105 return is_relevant
106
107 def _count_relevant_tb_levels(self, tb):
108 length = 0
109 while tb and not self._is_relevant_tb_level(tb):
110 length += 1
111 tb = tb.tb_next
112 return length
113
114 def __repr__(self):
Benjamin Peterson847a4112010-03-14 15:04:17 +0000115 return ("<%s run=%i errors=%i failures=%i>" %
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000116 (util.strclass(self.__class__), self.testsRun, len(self.errors),
Benjamin Peterson847a4112010-03-14 15:04:17 +0000117 len(self.failures)))