blob: 91cf2183436936bb9153efa5efa0f6d0b5d665a1 [file] [log] [blame]
Benjamin Petersonbed7d042009-07-19 21:01:52 +00001"""Test result object"""
2
3import traceback
4
5from . import util
6
Benjamin Petersondccc1fc2010-03-22 00:15:53 +00007__unittest = True
8
Benjamin Petersonbed7d042009-07-19 21:01:52 +00009
10class TestResult(object):
11 """Holder for test result information.
12
13 Test results are automatically managed by the TestCase and TestSuite
14 classes, and do not need to be explicitly manipulated by writers of tests.
15
16 Each instance holds the total number of tests run, and collections of
17 failures and errors that occurred among those test runs. The collections
18 contain tuples of (testcase, exceptioninfo), where exceptioninfo is the
19 formatted traceback of the error that occurred.
20 """
Benjamin Peterson847a4112010-03-14 15:04:17 +000021 _previousTestClass = None
22 _moduleSetUpFailed = False
23 def __init__(self, stream=None, descriptions=None, verbosity=None):
Benjamin Petersonbed7d042009-07-19 21:01:52 +000024 self.failures = []
25 self.errors = []
26 self.testsRun = 0
27 self.skipped = []
28 self.expectedFailures = []
29 self.unexpectedSuccesses = []
30 self.shouldStop = False
31
Benjamin Peterson847a4112010-03-14 15:04:17 +000032 def printErrors(self):
33 "Called by TestRunner after test run"
34
Benjamin Petersonbed7d042009-07-19 21:01:52 +000035 def startTest(self, test):
36 "Called when the given test is about to be run"
Michael Foord34c94622010-02-10 15:51:42 +000037 self.testsRun += 1
Benjamin Petersonbed7d042009-07-19 21:01:52 +000038
39 def startTestRun(self):
40 """Called once before any tests are executed.
41
42 See startTest for a method called before each test.
43 """
44
45 def stopTest(self, test):
Michael Foord34c94622010-02-10 15:51:42 +000046 """Called when the given test has been run"""
Benjamin Petersonbed7d042009-07-19 21:01:52 +000047
48 def stopTestRun(self):
49 """Called once after all tests are executed.
50
51 See stopTest for a method called after each test.
52 """
53
54 def addError(self, test, err):
55 """Called when an error has occurred. 'err' is a tuple of values as
56 returned by sys.exc_info().
57 """
58 self.errors.append((test, self._exc_info_to_string(err, test)))
59
60 def addFailure(self, test, err):
61 """Called when an error has occurred. 'err' is a tuple of values as
62 returned by sys.exc_info()."""
63 self.failures.append((test, self._exc_info_to_string(err, test)))
64
65 def addSuccess(self, test):
66 "Called when a test has completed successfully"
67 pass
68
69 def addSkip(self, test, reason):
70 """Called when a test is skipped."""
71 self.skipped.append((test, reason))
72
73 def addExpectedFailure(self, test, err):
74 """Called when an expected failure/error occured."""
75 self.expectedFailures.append(
76 (test, self._exc_info_to_string(err, test)))
77
78 def addUnexpectedSuccess(self, test):
79 """Called when a test was expected to fail, but succeed."""
80 self.unexpectedSuccesses.append(test)
81
82 def wasSuccessful(self):
83 "Tells whether or not this result was a success"
84 return len(self.failures) == len(self.errors) == 0
85
86 def stop(self):
87 "Indicates that the tests should be aborted"
88 self.shouldStop = True
89
90 def _exc_info_to_string(self, err, test):
91 """Converts a sys.exc_info()-style tuple of values into a string."""
92 exctype, value, tb = err
93 # Skip test runner traceback levels
94 while tb and self._is_relevant_tb_level(tb):
95 tb = tb.tb_next
96 if exctype is test.failureException:
97 # Skip assert*() traceback levels
98 length = self._count_relevant_tb_levels(tb)
99 return ''.join(traceback.format_exception(exctype, value, tb, length))
100 return ''.join(traceback.format_exception(exctype, value, tb))
101
102 def _is_relevant_tb_level(self, tb):
Benjamin Petersondccc1fc2010-03-22 00:15:53 +0000103 return '__unittest' in tb.tb_frame.f_globals
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000104
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):
Benjamin Peterson847a4112010-03-14 15:04:17 +0000113 return ("<%s run=%i errors=%i failures=%i>" %
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000114 (util.strclass(self.__class__), self.testsRun, len(self.errors),
Benjamin Peterson847a4112010-03-14 15:04:17 +0000115 len(self.failures)))