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