1 | n/a | import io |
---|
2 | n/a | import sys |
---|
3 | n/a | import textwrap |
---|
4 | n/a | |
---|
5 | n/a | from test import support |
---|
6 | n/a | |
---|
7 | n/a | import traceback |
---|
8 | n/a | import unittest |
---|
9 | n/a | |
---|
10 | n/a | |
---|
11 | n/a | class MockTraceback(object): |
---|
12 | n/a | class TracebackException: |
---|
13 | n/a | def __init__(self, *args, **kwargs): |
---|
14 | n/a | self.capture_locals = kwargs.get('capture_locals', False) |
---|
15 | n/a | def format(self): |
---|
16 | n/a | result = ['A traceback'] |
---|
17 | n/a | if self.capture_locals: |
---|
18 | n/a | result.append('locals') |
---|
19 | n/a | return result |
---|
20 | n/a | |
---|
21 | n/a | def restore_traceback(): |
---|
22 | n/a | unittest.result.traceback = traceback |
---|
23 | n/a | |
---|
24 | n/a | |
---|
25 | n/a | class Test_TestResult(unittest.TestCase): |
---|
26 | n/a | # Note: there are not separate tests for TestResult.wasSuccessful(), |
---|
27 | n/a | # TestResult.errors, TestResult.failures, TestResult.testsRun or |
---|
28 | n/a | # TestResult.shouldStop because these only have meaning in terms of |
---|
29 | n/a | # other TestResult methods. |
---|
30 | n/a | # |
---|
31 | n/a | # Accordingly, tests for the aforenamed attributes are incorporated |
---|
32 | n/a | # in with the tests for the defining methods. |
---|
33 | n/a | ################################################################ |
---|
34 | n/a | |
---|
35 | n/a | def test_init(self): |
---|
36 | n/a | result = unittest.TestResult() |
---|
37 | n/a | |
---|
38 | n/a | self.assertTrue(result.wasSuccessful()) |
---|
39 | n/a | self.assertEqual(len(result.errors), 0) |
---|
40 | n/a | self.assertEqual(len(result.failures), 0) |
---|
41 | n/a | self.assertEqual(result.testsRun, 0) |
---|
42 | n/a | self.assertEqual(result.shouldStop, False) |
---|
43 | n/a | self.assertIsNone(result._stdout_buffer) |
---|
44 | n/a | self.assertIsNone(result._stderr_buffer) |
---|
45 | n/a | |
---|
46 | n/a | # "This method can be called to signal that the set of tests being |
---|
47 | n/a | # run should be aborted by setting the TestResult's shouldStop |
---|
48 | n/a | # attribute to True." |
---|
49 | n/a | def test_stop(self): |
---|
50 | n/a | result = unittest.TestResult() |
---|
51 | n/a | |
---|
52 | n/a | result.stop() |
---|
53 | n/a | |
---|
54 | n/a | self.assertEqual(result.shouldStop, True) |
---|
55 | n/a | |
---|
56 | n/a | # "Called when the test case test is about to be run. The default |
---|
57 | n/a | # implementation simply increments the instance's testsRun counter." |
---|
58 | n/a | def test_startTest(self): |
---|
59 | n/a | class Foo(unittest.TestCase): |
---|
60 | n/a | def test_1(self): |
---|
61 | n/a | pass |
---|
62 | n/a | |
---|
63 | n/a | test = Foo('test_1') |
---|
64 | n/a | |
---|
65 | n/a | result = unittest.TestResult() |
---|
66 | n/a | |
---|
67 | n/a | result.startTest(test) |
---|
68 | n/a | |
---|
69 | n/a | self.assertTrue(result.wasSuccessful()) |
---|
70 | n/a | self.assertEqual(len(result.errors), 0) |
---|
71 | n/a | self.assertEqual(len(result.failures), 0) |
---|
72 | n/a | self.assertEqual(result.testsRun, 1) |
---|
73 | n/a | self.assertEqual(result.shouldStop, False) |
---|
74 | n/a | |
---|
75 | n/a | result.stopTest(test) |
---|
76 | n/a | |
---|
77 | n/a | # "Called after the test case test has been executed, regardless of |
---|
78 | n/a | # the outcome. The default implementation does nothing." |
---|
79 | n/a | def test_stopTest(self): |
---|
80 | n/a | class Foo(unittest.TestCase): |
---|
81 | n/a | def test_1(self): |
---|
82 | n/a | pass |
---|
83 | n/a | |
---|
84 | n/a | test = Foo('test_1') |
---|
85 | n/a | |
---|
86 | n/a | result = unittest.TestResult() |
---|
87 | n/a | |
---|
88 | n/a | result.startTest(test) |
---|
89 | n/a | |
---|
90 | n/a | self.assertTrue(result.wasSuccessful()) |
---|
91 | n/a | self.assertEqual(len(result.errors), 0) |
---|
92 | n/a | self.assertEqual(len(result.failures), 0) |
---|
93 | n/a | self.assertEqual(result.testsRun, 1) |
---|
94 | n/a | self.assertEqual(result.shouldStop, False) |
---|
95 | n/a | |
---|
96 | n/a | result.stopTest(test) |
---|
97 | n/a | |
---|
98 | n/a | # Same tests as above; make sure nothing has changed |
---|
99 | n/a | self.assertTrue(result.wasSuccessful()) |
---|
100 | n/a | self.assertEqual(len(result.errors), 0) |
---|
101 | n/a | self.assertEqual(len(result.failures), 0) |
---|
102 | n/a | self.assertEqual(result.testsRun, 1) |
---|
103 | n/a | self.assertEqual(result.shouldStop, False) |
---|
104 | n/a | |
---|
105 | n/a | # "Called before and after tests are run. The default implementation does nothing." |
---|
106 | n/a | def test_startTestRun_stopTestRun(self): |
---|
107 | n/a | result = unittest.TestResult() |
---|
108 | n/a | result.startTestRun() |
---|
109 | n/a | result.stopTestRun() |
---|
110 | n/a | |
---|
111 | n/a | # "addSuccess(test)" |
---|
112 | n/a | # ... |
---|
113 | n/a | # "Called when the test case test succeeds" |
---|
114 | n/a | # ... |
---|
115 | n/a | # "wasSuccessful() - Returns True if all tests run so far have passed, |
---|
116 | n/a | # otherwise returns False" |
---|
117 | n/a | # ... |
---|
118 | n/a | # "testsRun - The total number of tests run so far." |
---|
119 | n/a | # ... |
---|
120 | n/a | # "errors - A list containing 2-tuples of TestCase instances and |
---|
121 | n/a | # formatted tracebacks. Each tuple represents a test which raised an |
---|
122 | n/a | # unexpected exception. Contains formatted |
---|
123 | n/a | # tracebacks instead of sys.exc_info() results." |
---|
124 | n/a | # ... |
---|
125 | n/a | # "failures - A list containing 2-tuples of TestCase instances and |
---|
126 | n/a | # formatted tracebacks. Each tuple represents a test where a failure was |
---|
127 | n/a | # explicitly signalled using the TestCase.fail*() or TestCase.assert*() |
---|
128 | n/a | # methods. Contains formatted tracebacks instead |
---|
129 | n/a | # of sys.exc_info() results." |
---|
130 | n/a | def test_addSuccess(self): |
---|
131 | n/a | class Foo(unittest.TestCase): |
---|
132 | n/a | def test_1(self): |
---|
133 | n/a | pass |
---|
134 | n/a | |
---|
135 | n/a | test = Foo('test_1') |
---|
136 | n/a | |
---|
137 | n/a | result = unittest.TestResult() |
---|
138 | n/a | |
---|
139 | n/a | result.startTest(test) |
---|
140 | n/a | result.addSuccess(test) |
---|
141 | n/a | result.stopTest(test) |
---|
142 | n/a | |
---|
143 | n/a | self.assertTrue(result.wasSuccessful()) |
---|
144 | n/a | self.assertEqual(len(result.errors), 0) |
---|
145 | n/a | self.assertEqual(len(result.failures), 0) |
---|
146 | n/a | self.assertEqual(result.testsRun, 1) |
---|
147 | n/a | self.assertEqual(result.shouldStop, False) |
---|
148 | n/a | |
---|
149 | n/a | # "addFailure(test, err)" |
---|
150 | n/a | # ... |
---|
151 | n/a | # "Called when the test case test signals a failure. err is a tuple of |
---|
152 | n/a | # the form returned by sys.exc_info(): (type, value, traceback)" |
---|
153 | n/a | # ... |
---|
154 | n/a | # "wasSuccessful() - Returns True if all tests run so far have passed, |
---|
155 | n/a | # otherwise returns False" |
---|
156 | n/a | # ... |
---|
157 | n/a | # "testsRun - The total number of tests run so far." |
---|
158 | n/a | # ... |
---|
159 | n/a | # "errors - A list containing 2-tuples of TestCase instances and |
---|
160 | n/a | # formatted tracebacks. Each tuple represents a test which raised an |
---|
161 | n/a | # unexpected exception. Contains formatted |
---|
162 | n/a | # tracebacks instead of sys.exc_info() results." |
---|
163 | n/a | # ... |
---|
164 | n/a | # "failures - A list containing 2-tuples of TestCase instances and |
---|
165 | n/a | # formatted tracebacks. Each tuple represents a test where a failure was |
---|
166 | n/a | # explicitly signalled using the TestCase.fail*() or TestCase.assert*() |
---|
167 | n/a | # methods. Contains formatted tracebacks instead |
---|
168 | n/a | # of sys.exc_info() results." |
---|
169 | n/a | def test_addFailure(self): |
---|
170 | n/a | class Foo(unittest.TestCase): |
---|
171 | n/a | def test_1(self): |
---|
172 | n/a | pass |
---|
173 | n/a | |
---|
174 | n/a | test = Foo('test_1') |
---|
175 | n/a | try: |
---|
176 | n/a | test.fail("foo") |
---|
177 | n/a | except: |
---|
178 | n/a | exc_info_tuple = sys.exc_info() |
---|
179 | n/a | |
---|
180 | n/a | result = unittest.TestResult() |
---|
181 | n/a | |
---|
182 | n/a | result.startTest(test) |
---|
183 | n/a | result.addFailure(test, exc_info_tuple) |
---|
184 | n/a | result.stopTest(test) |
---|
185 | n/a | |
---|
186 | n/a | self.assertFalse(result.wasSuccessful()) |
---|
187 | n/a | self.assertEqual(len(result.errors), 0) |
---|
188 | n/a | self.assertEqual(len(result.failures), 1) |
---|
189 | n/a | self.assertEqual(result.testsRun, 1) |
---|
190 | n/a | self.assertEqual(result.shouldStop, False) |
---|
191 | n/a | |
---|
192 | n/a | test_case, formatted_exc = result.failures[0] |
---|
193 | n/a | self.assertIs(test_case, test) |
---|
194 | n/a | self.assertIsInstance(formatted_exc, str) |
---|
195 | n/a | |
---|
196 | n/a | # "addError(test, err)" |
---|
197 | n/a | # ... |
---|
198 | n/a | # "Called when the test case test raises an unexpected exception err |
---|
199 | n/a | # is a tuple of the form returned by sys.exc_info(): |
---|
200 | n/a | # (type, value, traceback)" |
---|
201 | n/a | # ... |
---|
202 | n/a | # "wasSuccessful() - Returns True if all tests run so far have passed, |
---|
203 | n/a | # otherwise returns False" |
---|
204 | n/a | # ... |
---|
205 | n/a | # "testsRun - The total number of tests run so far." |
---|
206 | n/a | # ... |
---|
207 | n/a | # "errors - A list containing 2-tuples of TestCase instances and |
---|
208 | n/a | # formatted tracebacks. Each tuple represents a test which raised an |
---|
209 | n/a | # unexpected exception. Contains formatted |
---|
210 | n/a | # tracebacks instead of sys.exc_info() results." |
---|
211 | n/a | # ... |
---|
212 | n/a | # "failures - A list containing 2-tuples of TestCase instances and |
---|
213 | n/a | # formatted tracebacks. Each tuple represents a test where a failure was |
---|
214 | n/a | # explicitly signalled using the TestCase.fail*() or TestCase.assert*() |
---|
215 | n/a | # methods. Contains formatted tracebacks instead |
---|
216 | n/a | # of sys.exc_info() results." |
---|
217 | n/a | def test_addError(self): |
---|
218 | n/a | class Foo(unittest.TestCase): |
---|
219 | n/a | def test_1(self): |
---|
220 | n/a | pass |
---|
221 | n/a | |
---|
222 | n/a | test = Foo('test_1') |
---|
223 | n/a | try: |
---|
224 | n/a | raise TypeError() |
---|
225 | n/a | except: |
---|
226 | n/a | exc_info_tuple = sys.exc_info() |
---|
227 | n/a | |
---|
228 | n/a | result = unittest.TestResult() |
---|
229 | n/a | |
---|
230 | n/a | result.startTest(test) |
---|
231 | n/a | result.addError(test, exc_info_tuple) |
---|
232 | n/a | result.stopTest(test) |
---|
233 | n/a | |
---|
234 | n/a | self.assertFalse(result.wasSuccessful()) |
---|
235 | n/a | self.assertEqual(len(result.errors), 1) |
---|
236 | n/a | self.assertEqual(len(result.failures), 0) |
---|
237 | n/a | self.assertEqual(result.testsRun, 1) |
---|
238 | n/a | self.assertEqual(result.shouldStop, False) |
---|
239 | n/a | |
---|
240 | n/a | test_case, formatted_exc = result.errors[0] |
---|
241 | n/a | self.assertIs(test_case, test) |
---|
242 | n/a | self.assertIsInstance(formatted_exc, str) |
---|
243 | n/a | |
---|
244 | n/a | def test_addError_locals(self): |
---|
245 | n/a | class Foo(unittest.TestCase): |
---|
246 | n/a | def test_1(self): |
---|
247 | n/a | 1/0 |
---|
248 | n/a | |
---|
249 | n/a | test = Foo('test_1') |
---|
250 | n/a | result = unittest.TestResult() |
---|
251 | n/a | result.tb_locals = True |
---|
252 | n/a | |
---|
253 | n/a | unittest.result.traceback = MockTraceback |
---|
254 | n/a | self.addCleanup(restore_traceback) |
---|
255 | n/a | result.startTestRun() |
---|
256 | n/a | test.run(result) |
---|
257 | n/a | result.stopTestRun() |
---|
258 | n/a | |
---|
259 | n/a | self.assertEqual(len(result.errors), 1) |
---|
260 | n/a | test_case, formatted_exc = result.errors[0] |
---|
261 | n/a | self.assertEqual('A tracebacklocals', formatted_exc) |
---|
262 | n/a | |
---|
263 | n/a | def test_addSubTest(self): |
---|
264 | n/a | class Foo(unittest.TestCase): |
---|
265 | n/a | def test_1(self): |
---|
266 | n/a | nonlocal subtest |
---|
267 | n/a | with self.subTest(foo=1): |
---|
268 | n/a | subtest = self._subtest |
---|
269 | n/a | try: |
---|
270 | n/a | 1/0 |
---|
271 | n/a | except ZeroDivisionError: |
---|
272 | n/a | exc_info_tuple = sys.exc_info() |
---|
273 | n/a | # Register an error by hand (to check the API) |
---|
274 | n/a | result.addSubTest(test, subtest, exc_info_tuple) |
---|
275 | n/a | # Now trigger a failure |
---|
276 | n/a | self.fail("some recognizable failure") |
---|
277 | n/a | |
---|
278 | n/a | subtest = None |
---|
279 | n/a | test = Foo('test_1') |
---|
280 | n/a | result = unittest.TestResult() |
---|
281 | n/a | |
---|
282 | n/a | test.run(result) |
---|
283 | n/a | |
---|
284 | n/a | self.assertFalse(result.wasSuccessful()) |
---|
285 | n/a | self.assertEqual(len(result.errors), 1) |
---|
286 | n/a | self.assertEqual(len(result.failures), 1) |
---|
287 | n/a | self.assertEqual(result.testsRun, 1) |
---|
288 | n/a | self.assertEqual(result.shouldStop, False) |
---|
289 | n/a | |
---|
290 | n/a | test_case, formatted_exc = result.errors[0] |
---|
291 | n/a | self.assertIs(test_case, subtest) |
---|
292 | n/a | self.assertIn("ZeroDivisionError", formatted_exc) |
---|
293 | n/a | test_case, formatted_exc = result.failures[0] |
---|
294 | n/a | self.assertIs(test_case, subtest) |
---|
295 | n/a | self.assertIn("some recognizable failure", formatted_exc) |
---|
296 | n/a | |
---|
297 | n/a | def testGetDescriptionWithoutDocstring(self): |
---|
298 | n/a | result = unittest.TextTestResult(None, True, 1) |
---|
299 | n/a | self.assertEqual( |
---|
300 | n/a | result.getDescription(self), |
---|
301 | n/a | 'testGetDescriptionWithoutDocstring (' + __name__ + |
---|
302 | n/a | '.Test_TestResult)') |
---|
303 | n/a | |
---|
304 | n/a | def testGetSubTestDescriptionWithoutDocstring(self): |
---|
305 | n/a | with self.subTest(foo=1, bar=2): |
---|
306 | n/a | result = unittest.TextTestResult(None, True, 1) |
---|
307 | n/a | self.assertEqual( |
---|
308 | n/a | result.getDescription(self._subtest), |
---|
309 | n/a | 'testGetSubTestDescriptionWithoutDocstring (' + __name__ + |
---|
310 | n/a | '.Test_TestResult) (bar=2, foo=1)') |
---|
311 | n/a | with self.subTest('some message'): |
---|
312 | n/a | result = unittest.TextTestResult(None, True, 1) |
---|
313 | n/a | self.assertEqual( |
---|
314 | n/a | result.getDescription(self._subtest), |
---|
315 | n/a | 'testGetSubTestDescriptionWithoutDocstring (' + __name__ + |
---|
316 | n/a | '.Test_TestResult) [some message]') |
---|
317 | n/a | |
---|
318 | n/a | def testGetSubTestDescriptionWithoutDocstringAndParams(self): |
---|
319 | n/a | with self.subTest(): |
---|
320 | n/a | result = unittest.TextTestResult(None, True, 1) |
---|
321 | n/a | self.assertEqual( |
---|
322 | n/a | result.getDescription(self._subtest), |
---|
323 | n/a | 'testGetSubTestDescriptionWithoutDocstringAndParams ' |
---|
324 | n/a | '(' + __name__ + '.Test_TestResult) (<subtest>)') |
---|
325 | n/a | |
---|
326 | n/a | def testGetSubTestDescriptionForFalsyValues(self): |
---|
327 | n/a | expected = 'testGetSubTestDescriptionForFalsyValues (%s.Test_TestResult) [%s]' |
---|
328 | n/a | result = unittest.TextTestResult(None, True, 1) |
---|
329 | n/a | for arg in [0, None, []]: |
---|
330 | n/a | with self.subTest(arg): |
---|
331 | n/a | self.assertEqual( |
---|
332 | n/a | result.getDescription(self._subtest), |
---|
333 | n/a | expected % (__name__, arg) |
---|
334 | n/a | ) |
---|
335 | n/a | |
---|
336 | n/a | def testGetNestedSubTestDescriptionWithoutDocstring(self): |
---|
337 | n/a | with self.subTest(foo=1): |
---|
338 | n/a | with self.subTest(bar=2): |
---|
339 | n/a | result = unittest.TextTestResult(None, True, 1) |
---|
340 | n/a | self.assertEqual( |
---|
341 | n/a | result.getDescription(self._subtest), |
---|
342 | n/a | 'testGetNestedSubTestDescriptionWithoutDocstring ' |
---|
343 | n/a | '(' + __name__ + '.Test_TestResult) (bar=2, foo=1)') |
---|
344 | n/a | |
---|
345 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
346 | n/a | "Docstrings are omitted with -O2 and above") |
---|
347 | n/a | def testGetDescriptionWithOneLineDocstring(self): |
---|
348 | n/a | """Tests getDescription() for a method with a docstring.""" |
---|
349 | n/a | result = unittest.TextTestResult(None, True, 1) |
---|
350 | n/a | self.assertEqual( |
---|
351 | n/a | result.getDescription(self), |
---|
352 | n/a | ('testGetDescriptionWithOneLineDocstring ' |
---|
353 | n/a | '(' + __name__ + '.Test_TestResult)\n' |
---|
354 | n/a | 'Tests getDescription() for a method with a docstring.')) |
---|
355 | n/a | |
---|
356 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
357 | n/a | "Docstrings are omitted with -O2 and above") |
---|
358 | n/a | def testGetSubTestDescriptionWithOneLineDocstring(self): |
---|
359 | n/a | """Tests getDescription() for a method with a docstring.""" |
---|
360 | n/a | result = unittest.TextTestResult(None, True, 1) |
---|
361 | n/a | with self.subTest(foo=1, bar=2): |
---|
362 | n/a | self.assertEqual( |
---|
363 | n/a | result.getDescription(self._subtest), |
---|
364 | n/a | ('testGetSubTestDescriptionWithOneLineDocstring ' |
---|
365 | n/a | '(' + __name__ + '.Test_TestResult) (bar=2, foo=1)\n' |
---|
366 | n/a | 'Tests getDescription() for a method with a docstring.')) |
---|
367 | n/a | |
---|
368 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
369 | n/a | "Docstrings are omitted with -O2 and above") |
---|
370 | n/a | def testGetDescriptionWithMultiLineDocstring(self): |
---|
371 | n/a | """Tests getDescription() for a method with a longer docstring. |
---|
372 | n/a | The second line of the docstring. |
---|
373 | n/a | """ |
---|
374 | n/a | result = unittest.TextTestResult(None, True, 1) |
---|
375 | n/a | self.assertEqual( |
---|
376 | n/a | result.getDescription(self), |
---|
377 | n/a | ('testGetDescriptionWithMultiLineDocstring ' |
---|
378 | n/a | '(' + __name__ + '.Test_TestResult)\n' |
---|
379 | n/a | 'Tests getDescription() for a method with a longer ' |
---|
380 | n/a | 'docstring.')) |
---|
381 | n/a | |
---|
382 | n/a | @unittest.skipIf(sys.flags.optimize >= 2, |
---|
383 | n/a | "Docstrings are omitted with -O2 and above") |
---|
384 | n/a | def testGetSubTestDescriptionWithMultiLineDocstring(self): |
---|
385 | n/a | """Tests getDescription() for a method with a longer docstring. |
---|
386 | n/a | The second line of the docstring. |
---|
387 | n/a | """ |
---|
388 | n/a | result = unittest.TextTestResult(None, True, 1) |
---|
389 | n/a | with self.subTest(foo=1, bar=2): |
---|
390 | n/a | self.assertEqual( |
---|
391 | n/a | result.getDescription(self._subtest), |
---|
392 | n/a | ('testGetSubTestDescriptionWithMultiLineDocstring ' |
---|
393 | n/a | '(' + __name__ + '.Test_TestResult) (bar=2, foo=1)\n' |
---|
394 | n/a | 'Tests getDescription() for a method with a longer ' |
---|
395 | n/a | 'docstring.')) |
---|
396 | n/a | |
---|
397 | n/a | def testStackFrameTrimming(self): |
---|
398 | n/a | class Frame(object): |
---|
399 | n/a | class tb_frame(object): |
---|
400 | n/a | f_globals = {} |
---|
401 | n/a | result = unittest.TestResult() |
---|
402 | n/a | self.assertFalse(result._is_relevant_tb_level(Frame)) |
---|
403 | n/a | |
---|
404 | n/a | Frame.tb_frame.f_globals['__unittest'] = True |
---|
405 | n/a | self.assertTrue(result._is_relevant_tb_level(Frame)) |
---|
406 | n/a | |
---|
407 | n/a | def testFailFast(self): |
---|
408 | n/a | result = unittest.TestResult() |
---|
409 | n/a | result._exc_info_to_string = lambda *_: '' |
---|
410 | n/a | result.failfast = True |
---|
411 | n/a | result.addError(None, None) |
---|
412 | n/a | self.assertTrue(result.shouldStop) |
---|
413 | n/a | |
---|
414 | n/a | result = unittest.TestResult() |
---|
415 | n/a | result._exc_info_to_string = lambda *_: '' |
---|
416 | n/a | result.failfast = True |
---|
417 | n/a | result.addFailure(None, None) |
---|
418 | n/a | self.assertTrue(result.shouldStop) |
---|
419 | n/a | |
---|
420 | n/a | result = unittest.TestResult() |
---|
421 | n/a | result._exc_info_to_string = lambda *_: '' |
---|
422 | n/a | result.failfast = True |
---|
423 | n/a | result.addUnexpectedSuccess(None) |
---|
424 | n/a | self.assertTrue(result.shouldStop) |
---|
425 | n/a | |
---|
426 | n/a | def testFailFastSetByRunner(self): |
---|
427 | n/a | runner = unittest.TextTestRunner(stream=io.StringIO(), failfast=True) |
---|
428 | n/a | def test(result): |
---|
429 | n/a | self.assertTrue(result.failfast) |
---|
430 | n/a | result = runner.run(test) |
---|
431 | n/a | |
---|
432 | n/a | |
---|
433 | n/a | classDict = dict(unittest.TestResult.__dict__) |
---|
434 | n/a | for m in ('addSkip', 'addExpectedFailure', 'addUnexpectedSuccess', |
---|
435 | n/a | '__init__'): |
---|
436 | n/a | del classDict[m] |
---|
437 | n/a | |
---|
438 | n/a | def __init__(self, stream=None, descriptions=None, verbosity=None): |
---|
439 | n/a | self.failures = [] |
---|
440 | n/a | self.errors = [] |
---|
441 | n/a | self.testsRun = 0 |
---|
442 | n/a | self.shouldStop = False |
---|
443 | n/a | self.buffer = False |
---|
444 | n/a | self.tb_locals = False |
---|
445 | n/a | |
---|
446 | n/a | classDict['__init__'] = __init__ |
---|
447 | n/a | OldResult = type('OldResult', (object,), classDict) |
---|
448 | n/a | |
---|
449 | n/a | class Test_OldTestResult(unittest.TestCase): |
---|
450 | n/a | |
---|
451 | n/a | def assertOldResultWarning(self, test, failures): |
---|
452 | n/a | with support.check_warnings(("TestResult has no add.+ method,", |
---|
453 | n/a | RuntimeWarning)): |
---|
454 | n/a | result = OldResult() |
---|
455 | n/a | test.run(result) |
---|
456 | n/a | self.assertEqual(len(result.failures), failures) |
---|
457 | n/a | |
---|
458 | n/a | def testOldTestResult(self): |
---|
459 | n/a | class Test(unittest.TestCase): |
---|
460 | n/a | def testSkip(self): |
---|
461 | n/a | self.skipTest('foobar') |
---|
462 | n/a | @unittest.expectedFailure |
---|
463 | n/a | def testExpectedFail(self): |
---|
464 | n/a | raise TypeError |
---|
465 | n/a | @unittest.expectedFailure |
---|
466 | n/a | def testUnexpectedSuccess(self): |
---|
467 | n/a | pass |
---|
468 | n/a | |
---|
469 | n/a | for test_name, should_pass in (('testSkip', True), |
---|
470 | n/a | ('testExpectedFail', True), |
---|
471 | n/a | ('testUnexpectedSuccess', False)): |
---|
472 | n/a | test = Test(test_name) |
---|
473 | n/a | self.assertOldResultWarning(test, int(not should_pass)) |
---|
474 | n/a | |
---|
475 | n/a | def testOldTestTesultSetup(self): |
---|
476 | n/a | class Test(unittest.TestCase): |
---|
477 | n/a | def setUp(self): |
---|
478 | n/a | self.skipTest('no reason') |
---|
479 | n/a | def testFoo(self): |
---|
480 | n/a | pass |
---|
481 | n/a | self.assertOldResultWarning(Test('testFoo'), 0) |
---|
482 | n/a | |
---|
483 | n/a | def testOldTestResultClass(self): |
---|
484 | n/a | @unittest.skip('no reason') |
---|
485 | n/a | class Test(unittest.TestCase): |
---|
486 | n/a | def testFoo(self): |
---|
487 | n/a | pass |
---|
488 | n/a | self.assertOldResultWarning(Test('testFoo'), 0) |
---|
489 | n/a | |
---|
490 | n/a | def testOldResultWithRunner(self): |
---|
491 | n/a | class Test(unittest.TestCase): |
---|
492 | n/a | def testFoo(self): |
---|
493 | n/a | pass |
---|
494 | n/a | runner = unittest.TextTestRunner(resultclass=OldResult, |
---|
495 | n/a | stream=io.StringIO()) |
---|
496 | n/a | # This will raise an exception if TextTestRunner can't handle old |
---|
497 | n/a | # test result objects |
---|
498 | n/a | runner.run(Test('testFoo')) |
---|
499 | n/a | |
---|
500 | n/a | |
---|
501 | n/a | class TestOutputBuffering(unittest.TestCase): |
---|
502 | n/a | |
---|
503 | n/a | def setUp(self): |
---|
504 | n/a | self._real_out = sys.stdout |
---|
505 | n/a | self._real_err = sys.stderr |
---|
506 | n/a | |
---|
507 | n/a | def tearDown(self): |
---|
508 | n/a | sys.stdout = self._real_out |
---|
509 | n/a | sys.stderr = self._real_err |
---|
510 | n/a | |
---|
511 | n/a | def testBufferOutputOff(self): |
---|
512 | n/a | real_out = self._real_out |
---|
513 | n/a | real_err = self._real_err |
---|
514 | n/a | |
---|
515 | n/a | result = unittest.TestResult() |
---|
516 | n/a | self.assertFalse(result.buffer) |
---|
517 | n/a | |
---|
518 | n/a | self.assertIs(real_out, sys.stdout) |
---|
519 | n/a | self.assertIs(real_err, sys.stderr) |
---|
520 | n/a | |
---|
521 | n/a | result.startTest(self) |
---|
522 | n/a | |
---|
523 | n/a | self.assertIs(real_out, sys.stdout) |
---|
524 | n/a | self.assertIs(real_err, sys.stderr) |
---|
525 | n/a | |
---|
526 | n/a | def testBufferOutputStartTestAddSuccess(self): |
---|
527 | n/a | real_out = self._real_out |
---|
528 | n/a | real_err = self._real_err |
---|
529 | n/a | |
---|
530 | n/a | result = unittest.TestResult() |
---|
531 | n/a | self.assertFalse(result.buffer) |
---|
532 | n/a | |
---|
533 | n/a | result.buffer = True |
---|
534 | n/a | |
---|
535 | n/a | self.assertIs(real_out, sys.stdout) |
---|
536 | n/a | self.assertIs(real_err, sys.stderr) |
---|
537 | n/a | |
---|
538 | n/a | result.startTest(self) |
---|
539 | n/a | |
---|
540 | n/a | self.assertIsNot(real_out, sys.stdout) |
---|
541 | n/a | self.assertIsNot(real_err, sys.stderr) |
---|
542 | n/a | self.assertIsInstance(sys.stdout, io.StringIO) |
---|
543 | n/a | self.assertIsInstance(sys.stderr, io.StringIO) |
---|
544 | n/a | self.assertIsNot(sys.stdout, sys.stderr) |
---|
545 | n/a | |
---|
546 | n/a | out_stream = sys.stdout |
---|
547 | n/a | err_stream = sys.stderr |
---|
548 | n/a | |
---|
549 | n/a | result._original_stdout = io.StringIO() |
---|
550 | n/a | result._original_stderr = io.StringIO() |
---|
551 | n/a | |
---|
552 | n/a | print('foo') |
---|
553 | n/a | print('bar', file=sys.stderr) |
---|
554 | n/a | |
---|
555 | n/a | self.assertEqual(out_stream.getvalue(), 'foo\n') |
---|
556 | n/a | self.assertEqual(err_stream.getvalue(), 'bar\n') |
---|
557 | n/a | |
---|
558 | n/a | self.assertEqual(result._original_stdout.getvalue(), '') |
---|
559 | n/a | self.assertEqual(result._original_stderr.getvalue(), '') |
---|
560 | n/a | |
---|
561 | n/a | result.addSuccess(self) |
---|
562 | n/a | result.stopTest(self) |
---|
563 | n/a | |
---|
564 | n/a | self.assertIs(sys.stdout, result._original_stdout) |
---|
565 | n/a | self.assertIs(sys.stderr, result._original_stderr) |
---|
566 | n/a | |
---|
567 | n/a | self.assertEqual(result._original_stdout.getvalue(), '') |
---|
568 | n/a | self.assertEqual(result._original_stderr.getvalue(), '') |
---|
569 | n/a | |
---|
570 | n/a | self.assertEqual(out_stream.getvalue(), '') |
---|
571 | n/a | self.assertEqual(err_stream.getvalue(), '') |
---|
572 | n/a | |
---|
573 | n/a | |
---|
574 | n/a | def getStartedResult(self): |
---|
575 | n/a | result = unittest.TestResult() |
---|
576 | n/a | result.buffer = True |
---|
577 | n/a | result.startTest(self) |
---|
578 | n/a | return result |
---|
579 | n/a | |
---|
580 | n/a | def testBufferOutputAddErrorOrFailure(self): |
---|
581 | n/a | unittest.result.traceback = MockTraceback |
---|
582 | n/a | self.addCleanup(restore_traceback) |
---|
583 | n/a | |
---|
584 | n/a | for message_attr, add_attr, include_error in [ |
---|
585 | n/a | ('errors', 'addError', True), |
---|
586 | n/a | ('failures', 'addFailure', False), |
---|
587 | n/a | ('errors', 'addError', True), |
---|
588 | n/a | ('failures', 'addFailure', False) |
---|
589 | n/a | ]: |
---|
590 | n/a | result = self.getStartedResult() |
---|
591 | n/a | buffered_out = sys.stdout |
---|
592 | n/a | buffered_err = sys.stderr |
---|
593 | n/a | result._original_stdout = io.StringIO() |
---|
594 | n/a | result._original_stderr = io.StringIO() |
---|
595 | n/a | |
---|
596 | n/a | print('foo', file=sys.stdout) |
---|
597 | n/a | if include_error: |
---|
598 | n/a | print('bar', file=sys.stderr) |
---|
599 | n/a | |
---|
600 | n/a | |
---|
601 | n/a | addFunction = getattr(result, add_attr) |
---|
602 | n/a | addFunction(self, (None, None, None)) |
---|
603 | n/a | result.stopTest(self) |
---|
604 | n/a | |
---|
605 | n/a | result_list = getattr(result, message_attr) |
---|
606 | n/a | self.assertEqual(len(result_list), 1) |
---|
607 | n/a | |
---|
608 | n/a | test, message = result_list[0] |
---|
609 | n/a | expectedOutMessage = textwrap.dedent(""" |
---|
610 | n/a | Stdout: |
---|
611 | n/a | foo |
---|
612 | n/a | """) |
---|
613 | n/a | expectedErrMessage = '' |
---|
614 | n/a | if include_error: |
---|
615 | n/a | expectedErrMessage = textwrap.dedent(""" |
---|
616 | n/a | Stderr: |
---|
617 | n/a | bar |
---|
618 | n/a | """) |
---|
619 | n/a | |
---|
620 | n/a | expectedFullMessage = 'A traceback%s%s' % (expectedOutMessage, expectedErrMessage) |
---|
621 | n/a | |
---|
622 | n/a | self.assertIs(test, self) |
---|
623 | n/a | self.assertEqual(result._original_stdout.getvalue(), expectedOutMessage) |
---|
624 | n/a | self.assertEqual(result._original_stderr.getvalue(), expectedErrMessage) |
---|
625 | n/a | self.assertMultiLineEqual(message, expectedFullMessage) |
---|
626 | n/a | |
---|
627 | n/a | def testBufferSetupClass(self): |
---|
628 | n/a | result = unittest.TestResult() |
---|
629 | n/a | result.buffer = True |
---|
630 | n/a | |
---|
631 | n/a | class Foo(unittest.TestCase): |
---|
632 | n/a | @classmethod |
---|
633 | n/a | def setUpClass(cls): |
---|
634 | n/a | 1/0 |
---|
635 | n/a | def test_foo(self): |
---|
636 | n/a | pass |
---|
637 | n/a | suite = unittest.TestSuite([Foo('test_foo')]) |
---|
638 | n/a | suite(result) |
---|
639 | n/a | self.assertEqual(len(result.errors), 1) |
---|
640 | n/a | |
---|
641 | n/a | def testBufferTearDownClass(self): |
---|
642 | n/a | result = unittest.TestResult() |
---|
643 | n/a | result.buffer = True |
---|
644 | n/a | |
---|
645 | n/a | class Foo(unittest.TestCase): |
---|
646 | n/a | @classmethod |
---|
647 | n/a | def tearDownClass(cls): |
---|
648 | n/a | 1/0 |
---|
649 | n/a | def test_foo(self): |
---|
650 | n/a | pass |
---|
651 | n/a | suite = unittest.TestSuite([Foo('test_foo')]) |
---|
652 | n/a | suite(result) |
---|
653 | n/a | self.assertEqual(len(result.errors), 1) |
---|
654 | n/a | |
---|
655 | n/a | def testBufferSetUpModule(self): |
---|
656 | n/a | result = unittest.TestResult() |
---|
657 | n/a | result.buffer = True |
---|
658 | n/a | |
---|
659 | n/a | class Foo(unittest.TestCase): |
---|
660 | n/a | def test_foo(self): |
---|
661 | n/a | pass |
---|
662 | n/a | class Module(object): |
---|
663 | n/a | @staticmethod |
---|
664 | n/a | def setUpModule(): |
---|
665 | n/a | 1/0 |
---|
666 | n/a | |
---|
667 | n/a | Foo.__module__ = 'Module' |
---|
668 | n/a | sys.modules['Module'] = Module |
---|
669 | n/a | self.addCleanup(sys.modules.pop, 'Module') |
---|
670 | n/a | suite = unittest.TestSuite([Foo('test_foo')]) |
---|
671 | n/a | suite(result) |
---|
672 | n/a | self.assertEqual(len(result.errors), 1) |
---|
673 | n/a | |
---|
674 | n/a | def testBufferTearDownModule(self): |
---|
675 | n/a | result = unittest.TestResult() |
---|
676 | n/a | result.buffer = True |
---|
677 | n/a | |
---|
678 | n/a | class Foo(unittest.TestCase): |
---|
679 | n/a | def test_foo(self): |
---|
680 | n/a | pass |
---|
681 | n/a | class Module(object): |
---|
682 | n/a | @staticmethod |
---|
683 | n/a | def tearDownModule(): |
---|
684 | n/a | 1/0 |
---|
685 | n/a | |
---|
686 | n/a | Foo.__module__ = 'Module' |
---|
687 | n/a | sys.modules['Module'] = Module |
---|
688 | n/a | self.addCleanup(sys.modules.pop, 'Module') |
---|
689 | n/a | suite = unittest.TestSuite([Foo('test_foo')]) |
---|
690 | n/a | suite(result) |
---|
691 | n/a | self.assertEqual(len(result.errors), 1) |
---|
692 | n/a | |
---|
693 | n/a | |
---|
694 | n/a | if __name__ == '__main__': |
---|
695 | n/a | unittest.main() |
---|