1 | n/a | """Test result object""" |
---|
2 | n/a | |
---|
3 | n/a | import io |
---|
4 | n/a | import sys |
---|
5 | n/a | import traceback |
---|
6 | n/a | |
---|
7 | n/a | from . import util |
---|
8 | n/a | from functools import wraps |
---|
9 | n/a | |
---|
10 | n/a | __unittest = True |
---|
11 | n/a | |
---|
12 | n/a | def failfast(method): |
---|
13 | n/a | @wraps(method) |
---|
14 | n/a | def inner(self, *args, **kw): |
---|
15 | n/a | if getattr(self, 'failfast', False): |
---|
16 | n/a | self.stop() |
---|
17 | n/a | return method(self, *args, **kw) |
---|
18 | n/a | return inner |
---|
19 | n/a | |
---|
20 | n/a | STDOUT_LINE = '\nStdout:\n%s' |
---|
21 | n/a | STDERR_LINE = '\nStderr:\n%s' |
---|
22 | n/a | |
---|
23 | n/a | |
---|
24 | n/a | class TestResult(object): |
---|
25 | n/a | """Holder for test result information. |
---|
26 | n/a | |
---|
27 | n/a | Test results are automatically managed by the TestCase and TestSuite |
---|
28 | n/a | classes, and do not need to be explicitly manipulated by writers of tests. |
---|
29 | n/a | |
---|
30 | n/a | Each instance holds the total number of tests run, and collections of |
---|
31 | n/a | failures and errors that occurred among those test runs. The collections |
---|
32 | n/a | contain tuples of (testcase, exceptioninfo), where exceptioninfo is the |
---|
33 | n/a | formatted traceback of the error that occurred. |
---|
34 | n/a | """ |
---|
35 | n/a | _previousTestClass = None |
---|
36 | n/a | _testRunEntered = False |
---|
37 | n/a | _moduleSetUpFailed = False |
---|
38 | n/a | def __init__(self, stream=None, descriptions=None, verbosity=None): |
---|
39 | n/a | self.failfast = False |
---|
40 | n/a | self.failures = [] |
---|
41 | n/a | self.errors = [] |
---|
42 | n/a | self.testsRun = 0 |
---|
43 | n/a | self.skipped = [] |
---|
44 | n/a | self.expectedFailures = [] |
---|
45 | n/a | self.unexpectedSuccesses = [] |
---|
46 | n/a | self.shouldStop = False |
---|
47 | n/a | self.buffer = False |
---|
48 | n/a | self.tb_locals = False |
---|
49 | n/a | self._stdout_buffer = None |
---|
50 | n/a | self._stderr_buffer = None |
---|
51 | n/a | self._original_stdout = sys.stdout |
---|
52 | n/a | self._original_stderr = sys.stderr |
---|
53 | n/a | self._mirrorOutput = False |
---|
54 | n/a | |
---|
55 | n/a | def printErrors(self): |
---|
56 | n/a | "Called by TestRunner after test run" |
---|
57 | n/a | |
---|
58 | n/a | def startTest(self, test): |
---|
59 | n/a | "Called when the given test is about to be run" |
---|
60 | n/a | self.testsRun += 1 |
---|
61 | n/a | self._mirrorOutput = False |
---|
62 | n/a | self._setupStdout() |
---|
63 | n/a | |
---|
64 | n/a | def _setupStdout(self): |
---|
65 | n/a | if self.buffer: |
---|
66 | n/a | if self._stderr_buffer is None: |
---|
67 | n/a | self._stderr_buffer = io.StringIO() |
---|
68 | n/a | self._stdout_buffer = io.StringIO() |
---|
69 | n/a | sys.stdout = self._stdout_buffer |
---|
70 | n/a | sys.stderr = self._stderr_buffer |
---|
71 | n/a | |
---|
72 | n/a | def startTestRun(self): |
---|
73 | n/a | """Called once before any tests are executed. |
---|
74 | n/a | |
---|
75 | n/a | See startTest for a method called before each test. |
---|
76 | n/a | """ |
---|
77 | n/a | |
---|
78 | n/a | def stopTest(self, test): |
---|
79 | n/a | """Called when the given test has been run""" |
---|
80 | n/a | self._restoreStdout() |
---|
81 | n/a | self._mirrorOutput = False |
---|
82 | n/a | |
---|
83 | n/a | def _restoreStdout(self): |
---|
84 | n/a | if self.buffer: |
---|
85 | n/a | if self._mirrorOutput: |
---|
86 | n/a | output = sys.stdout.getvalue() |
---|
87 | n/a | error = sys.stderr.getvalue() |
---|
88 | n/a | if output: |
---|
89 | n/a | if not output.endswith('\n'): |
---|
90 | n/a | output += '\n' |
---|
91 | n/a | self._original_stdout.write(STDOUT_LINE % output) |
---|
92 | n/a | if error: |
---|
93 | n/a | if not error.endswith('\n'): |
---|
94 | n/a | error += '\n' |
---|
95 | n/a | self._original_stderr.write(STDERR_LINE % error) |
---|
96 | n/a | |
---|
97 | n/a | sys.stdout = self._original_stdout |
---|
98 | n/a | sys.stderr = self._original_stderr |
---|
99 | n/a | self._stdout_buffer.seek(0) |
---|
100 | n/a | self._stdout_buffer.truncate() |
---|
101 | n/a | self._stderr_buffer.seek(0) |
---|
102 | n/a | self._stderr_buffer.truncate() |
---|
103 | n/a | |
---|
104 | n/a | def stopTestRun(self): |
---|
105 | n/a | """Called once after all tests are executed. |
---|
106 | n/a | |
---|
107 | n/a | See stopTest for a method called after each test. |
---|
108 | n/a | """ |
---|
109 | n/a | |
---|
110 | n/a | @failfast |
---|
111 | n/a | def addError(self, test, err): |
---|
112 | n/a | """Called when an error has occurred. 'err' is a tuple of values as |
---|
113 | n/a | returned by sys.exc_info(). |
---|
114 | n/a | """ |
---|
115 | n/a | self.errors.append((test, self._exc_info_to_string(err, test))) |
---|
116 | n/a | self._mirrorOutput = True |
---|
117 | n/a | |
---|
118 | n/a | @failfast |
---|
119 | n/a | def addFailure(self, test, err): |
---|
120 | n/a | """Called when an error has occurred. 'err' is a tuple of values as |
---|
121 | n/a | returned by sys.exc_info().""" |
---|
122 | n/a | self.failures.append((test, self._exc_info_to_string(err, test))) |
---|
123 | n/a | self._mirrorOutput = True |
---|
124 | n/a | |
---|
125 | n/a | def addSubTest(self, test, subtest, err): |
---|
126 | n/a | """Called at the end of a subtest. |
---|
127 | n/a | 'err' is None if the subtest ended successfully, otherwise it's a |
---|
128 | n/a | tuple of values as returned by sys.exc_info(). |
---|
129 | n/a | """ |
---|
130 | n/a | # By default, we don't do anything with successful subtests, but |
---|
131 | n/a | # more sophisticated test results might want to record them. |
---|
132 | n/a | if err is not None: |
---|
133 | n/a | if getattr(self, 'failfast', False): |
---|
134 | n/a | self.stop() |
---|
135 | n/a | if issubclass(err[0], test.failureException): |
---|
136 | n/a | errors = self.failures |
---|
137 | n/a | else: |
---|
138 | n/a | errors = self.errors |
---|
139 | n/a | errors.append((subtest, self._exc_info_to_string(err, test))) |
---|
140 | n/a | self._mirrorOutput = True |
---|
141 | n/a | |
---|
142 | n/a | def addSuccess(self, test): |
---|
143 | n/a | "Called when a test has completed successfully" |
---|
144 | n/a | pass |
---|
145 | n/a | |
---|
146 | n/a | def addSkip(self, test, reason): |
---|
147 | n/a | """Called when a test is skipped.""" |
---|
148 | n/a | self.skipped.append((test, reason)) |
---|
149 | n/a | |
---|
150 | n/a | def addExpectedFailure(self, test, err): |
---|
151 | n/a | """Called when an expected failure/error occurred.""" |
---|
152 | n/a | self.expectedFailures.append( |
---|
153 | n/a | (test, self._exc_info_to_string(err, test))) |
---|
154 | n/a | |
---|
155 | n/a | @failfast |
---|
156 | n/a | def addUnexpectedSuccess(self, test): |
---|
157 | n/a | """Called when a test was expected to fail, but succeed.""" |
---|
158 | n/a | self.unexpectedSuccesses.append(test) |
---|
159 | n/a | |
---|
160 | n/a | def wasSuccessful(self): |
---|
161 | n/a | """Tells whether or not this result was a success.""" |
---|
162 | n/a | # The hasattr check is for test_result's OldResult test. That |
---|
163 | n/a | # way this method works on objects that lack the attribute. |
---|
164 | n/a | # (where would such result intances come from? old stored pickles?) |
---|
165 | n/a | return ((len(self.failures) == len(self.errors) == 0) and |
---|
166 | n/a | (not hasattr(self, 'unexpectedSuccesses') or |
---|
167 | n/a | len(self.unexpectedSuccesses) == 0)) |
---|
168 | n/a | |
---|
169 | n/a | def stop(self): |
---|
170 | n/a | """Indicates that the tests should be aborted.""" |
---|
171 | n/a | self.shouldStop = True |
---|
172 | n/a | |
---|
173 | n/a | def _exc_info_to_string(self, err, test): |
---|
174 | n/a | """Converts a sys.exc_info()-style tuple of values into a string.""" |
---|
175 | n/a | exctype, value, tb = err |
---|
176 | n/a | # Skip test runner traceback levels |
---|
177 | n/a | while tb and self._is_relevant_tb_level(tb): |
---|
178 | n/a | tb = tb.tb_next |
---|
179 | n/a | |
---|
180 | n/a | if exctype is test.failureException: |
---|
181 | n/a | # Skip assert*() traceback levels |
---|
182 | n/a | length = self._count_relevant_tb_levels(tb) |
---|
183 | n/a | else: |
---|
184 | n/a | length = None |
---|
185 | n/a | tb_e = traceback.TracebackException( |
---|
186 | n/a | exctype, value, tb, limit=length, capture_locals=self.tb_locals) |
---|
187 | n/a | msgLines = list(tb_e.format()) |
---|
188 | n/a | |
---|
189 | n/a | if self.buffer: |
---|
190 | n/a | output = sys.stdout.getvalue() |
---|
191 | n/a | error = sys.stderr.getvalue() |
---|
192 | n/a | if output: |
---|
193 | n/a | if not output.endswith('\n'): |
---|
194 | n/a | output += '\n' |
---|
195 | n/a | msgLines.append(STDOUT_LINE % output) |
---|
196 | n/a | if error: |
---|
197 | n/a | if not error.endswith('\n'): |
---|
198 | n/a | error += '\n' |
---|
199 | n/a | msgLines.append(STDERR_LINE % error) |
---|
200 | n/a | return ''.join(msgLines) |
---|
201 | n/a | |
---|
202 | n/a | |
---|
203 | n/a | def _is_relevant_tb_level(self, tb): |
---|
204 | n/a | return '__unittest' in tb.tb_frame.f_globals |
---|
205 | n/a | |
---|
206 | n/a | def _count_relevant_tb_levels(self, tb): |
---|
207 | n/a | length = 0 |
---|
208 | n/a | while tb and not self._is_relevant_tb_level(tb): |
---|
209 | n/a | length += 1 |
---|
210 | n/a | tb = tb.tb_next |
---|
211 | n/a | return length |
---|
212 | n/a | |
---|
213 | n/a | def __repr__(self): |
---|
214 | n/a | return ("<%s run=%i errors=%i failures=%i>" % |
---|
215 | n/a | (util.strclass(self.__class__), self.testsRun, len(self.errors), |
---|
216 | n/a | len(self.failures))) |
---|