blob: 4538304908a51d40bb3eeb39b278d68310c989ad [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 """
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"
Michael Foord34c94622010-02-10 15:51:42 +000030 self.testsRun += 1
Benjamin Petersonbed7d042009-07-19 21:01:52 +000031
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):
Michael Foord34c94622010-02-10 15:51:42 +000039 """Called when the given test has been run"""
Benjamin Petersonbed7d042009-07-19 21:01:52 +000040
41 def stopTestRun(self):
42 """Called once after all tests are executed.
43
44 See stopTest for a method called after each test.
45 """
46
47 def addError(self, test, err):
48 """Called when an error has occurred. 'err' is a tuple of values as
49 returned by sys.exc_info().
50 """
51 self.errors.append((test, self._exc_info_to_string(err, test)))
52
53 def addFailure(self, test, err):
54 """Called when an error has occurred. 'err' is a tuple of values as
55 returned by sys.exc_info()."""
56 self.failures.append((test, self._exc_info_to_string(err, test)))
57
58 def addSuccess(self, test):
59 "Called when a test has completed successfully"
60 pass
61
62 def addSkip(self, test, reason):
63 """Called when a test is skipped."""
64 self.skipped.append((test, reason))
65
66 def addExpectedFailure(self, test, err):
67 """Called when an expected failure/error occured."""
68 self.expectedFailures.append(
69 (test, self._exc_info_to_string(err, test)))
70
71 def addUnexpectedSuccess(self, test):
72 """Called when a test was expected to fail, but succeed."""
73 self.unexpectedSuccesses.append(test)
74
75 def wasSuccessful(self):
76 "Tells whether or not this result was a success"
77 return len(self.failures) == len(self.errors) == 0
78
79 def stop(self):
80 "Indicates that the tests should be aborted"
81 self.shouldStop = True
82
83 def _exc_info_to_string(self, err, test):
84 """Converts a sys.exc_info()-style tuple of values into a string."""
85 exctype, value, tb = err
86 # Skip test runner traceback levels
87 while tb and self._is_relevant_tb_level(tb):
88 tb = tb.tb_next
89 if exctype is test.failureException:
90 # Skip assert*() traceback levels
91 length = self._count_relevant_tb_levels(tb)
92 return ''.join(traceback.format_exception(exctype, value, tb, length))
93 return ''.join(traceback.format_exception(exctype, value, tb))
94
95 def _is_relevant_tb_level(self, tb):
96 globs = tb.tb_frame.f_globals
97 is_relevant = '__name__' in globs and \
98 globs["__name__"].startswith("unittest")
99 del globs
100 return is_relevant
101
102 def _count_relevant_tb_levels(self, tb):
103 length = 0
104 while tb and not self._is_relevant_tb_level(tb):
105 length += 1
106 tb = tb.tb_next
107 return length
108
109 def __repr__(self):
110 return "<%s run=%i errors=%i failures=%i>" % \
111 (util.strclass(self.__class__), self.testsRun, len(self.errors),
112 len(self.failures))