1 """Test result object"""
8 class TestResult(object):
9 """Holder for test result information.
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.
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.
24 self
.expectedFailures
= []
25 self
.unexpectedSuccesses
= []
26 self
.shouldStop
= False
28 def startTest(self
, test
):
29 "Called when the given test is about to be run"
30 self
.testsRun
= self
.testsRun
+ 1
32 def startTestRun(self
):
33 """Called once before any tests are executed.
35 See startTest for a method called before each test.
38 def stopTest(self
, test
):
39 "Called when the given test has been run"
42 def stopTestRun(self
):
43 """Called once after all tests are executed.
45 See stopTest for a method called after each test.
48 def addError(self
, test
, err
):
49 """Called when an error has occurred. 'err' is a tuple of values as
50 returned by sys.exc_info().
52 self
.errors
.append((test
, self
._exc
_info
_to
_string
(err
, test
)))
54 def addFailure(self
, test
, err
):
55 """Called when an error has occurred. 'err' is a tuple of values as
56 returned by sys.exc_info()."""
57 self
.failures
.append((test
, self
._exc
_info
_to
_string
(err
, test
)))
59 def addSuccess(self
, test
):
60 "Called when a test has completed successfully"
63 def addSkip(self
, test
, reason
):
64 """Called when a test is skipped."""
65 self
.skipped
.append((test
, reason
))
67 def addExpectedFailure(self
, test
, err
):
68 """Called when an expected failure/error occured."""
69 self
.expectedFailures
.append(
70 (test
, self
._exc
_info
_to
_string
(err
, test
)))
72 def addUnexpectedSuccess(self
, test
):
73 """Called when a test was expected to fail, but succeed."""
74 self
.unexpectedSuccesses
.append(test
)
76 def wasSuccessful(self
):
77 "Tells whether or not this result was a success"
78 return len(self
.failures
) == len(self
.errors
) == 0
81 "Indicates that the tests should be aborted"
82 self
.shouldStop
= True
84 def _exc_info_to_string(self
, err
, test
):
85 """Converts a sys.exc_info()-style tuple of values into a string."""
86 exctype
, value
, tb
= err
87 # Skip test runner traceback levels
88 while tb
and self
._is
_relevant
_tb
_level
(tb
):
90 if exctype
is test
.failureException
:
91 # Skip assert*() traceback levels
92 length
= self
._count
_relevant
_tb
_levels
(tb
)
93 return ''.join(traceback
.format_exception(exctype
, value
, tb
, length
))
94 return ''.join(traceback
.format_exception(exctype
, value
, tb
))
96 def _is_relevant_tb_level(self
, tb
):
97 globs
= tb
.tb_frame
.f_globals
98 is_relevant
= '__name__' in globs
and \
99 globs
["__name__"].startswith("unittest")
103 def _count_relevant_tb_levels(self
, tb
):
105 while tb
and not self
._is
_relevant
_tb
_level
(tb
):
111 return "<%s run=%i errors=%i failures=%i>" % \
112 (util
.strclass(self
.__class
__), self
.testsRun
, len(self
.errors
),