• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import io
2import sys
3import textwrap
4
5from test.support import warnings_helper, captured_stdout, captured_stderr
6
7import traceback
8import unittest
9from unittest.util import strclass
10
11
12class MockTraceback(object):
13    class TracebackException:
14        def __init__(self, *args, **kwargs):
15            self.capture_locals = kwargs.get('capture_locals', False)
16        def format(self):
17            result = ['A traceback']
18            if self.capture_locals:
19                result.append('locals')
20            return result
21
22def restore_traceback():
23    unittest.result.traceback = traceback
24
25
26def bad_cleanup1():
27    print('do cleanup1')
28    raise TypeError('bad cleanup1')
29
30
31def bad_cleanup2():
32    print('do cleanup2')
33    raise ValueError('bad cleanup2')
34
35
36class BufferedWriter:
37    def __init__(self):
38        self.result = ''
39        self.buffer = ''
40
41    def write(self, arg):
42        self.buffer += arg
43
44    def flush(self):
45        self.result += self.buffer
46        self.buffer = ''
47
48    def getvalue(self):
49        return self.result
50
51
52class Test_TestResult(unittest.TestCase):
53    # Note: there are not separate tests for TestResult.wasSuccessful(),
54    # TestResult.errors, TestResult.failures, TestResult.testsRun or
55    # TestResult.shouldStop because these only have meaning in terms of
56    # other TestResult methods.
57    #
58    # Accordingly, tests for the aforenamed attributes are incorporated
59    # in with the tests for the defining methods.
60    ################################################################
61
62    def test_init(self):
63        result = unittest.TestResult()
64
65        self.assertTrue(result.wasSuccessful())
66        self.assertEqual(len(result.errors), 0)
67        self.assertEqual(len(result.failures), 0)
68        self.assertEqual(result.testsRun, 0)
69        self.assertEqual(result.shouldStop, False)
70        self.assertIsNone(result._stdout_buffer)
71        self.assertIsNone(result._stderr_buffer)
72
73    # "This method can be called to signal that the set of tests being
74    # run should be aborted by setting the TestResult's shouldStop
75    # attribute to True."
76    def test_stop(self):
77        result = unittest.TestResult()
78
79        result.stop()
80
81        self.assertEqual(result.shouldStop, True)
82
83    # "Called when the test case test is about to be run. The default
84    # implementation simply increments the instance's testsRun counter."
85    def test_startTest(self):
86        class Foo(unittest.TestCase):
87            def test_1(self):
88                pass
89
90        test = Foo('test_1')
91
92        result = unittest.TestResult()
93
94        result.startTest(test)
95
96        self.assertTrue(result.wasSuccessful())
97        self.assertEqual(len(result.errors), 0)
98        self.assertEqual(len(result.failures), 0)
99        self.assertEqual(result.testsRun, 1)
100        self.assertEqual(result.shouldStop, False)
101
102        result.stopTest(test)
103
104    # "Called after the test case test has been executed, regardless of
105    # the outcome. The default implementation does nothing."
106    def test_stopTest(self):
107        class Foo(unittest.TestCase):
108            def test_1(self):
109                pass
110
111        test = Foo('test_1')
112
113        result = unittest.TestResult()
114
115        result.startTest(test)
116
117        self.assertTrue(result.wasSuccessful())
118        self.assertEqual(len(result.errors), 0)
119        self.assertEqual(len(result.failures), 0)
120        self.assertEqual(result.testsRun, 1)
121        self.assertEqual(result.shouldStop, False)
122
123        result.stopTest(test)
124
125        # Same tests as above; make sure nothing has changed
126        self.assertTrue(result.wasSuccessful())
127        self.assertEqual(len(result.errors), 0)
128        self.assertEqual(len(result.failures), 0)
129        self.assertEqual(result.testsRun, 1)
130        self.assertEqual(result.shouldStop, False)
131
132    # "Called before and after tests are run. The default implementation does nothing."
133    def test_startTestRun_stopTestRun(self):
134        result = unittest.TestResult()
135        result.startTestRun()
136        result.stopTestRun()
137
138    # "addSuccess(test)"
139    # ...
140    # "Called when the test case test succeeds"
141    # ...
142    # "wasSuccessful() - Returns True if all tests run so far have passed,
143    # otherwise returns False"
144    # ...
145    # "testsRun - The total number of tests run so far."
146    # ...
147    # "errors - A list containing 2-tuples of TestCase instances and
148    # formatted tracebacks. Each tuple represents a test which raised an
149    # unexpected exception. Contains formatted
150    # tracebacks instead of sys.exc_info() results."
151    # ...
152    # "failures - A list containing 2-tuples of TestCase instances and
153    # formatted tracebacks. Each tuple represents a test where a failure was
154    # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
155    # methods. Contains formatted tracebacks instead
156    # of sys.exc_info() results."
157    def test_addSuccess(self):
158        class Foo(unittest.TestCase):
159            def test_1(self):
160                pass
161
162        test = Foo('test_1')
163
164        result = unittest.TestResult()
165
166        result.startTest(test)
167        result.addSuccess(test)
168        result.stopTest(test)
169
170        self.assertTrue(result.wasSuccessful())
171        self.assertEqual(len(result.errors), 0)
172        self.assertEqual(len(result.failures), 0)
173        self.assertEqual(result.testsRun, 1)
174        self.assertEqual(result.shouldStop, False)
175
176    # "addFailure(test, err)"
177    # ...
178    # "Called when the test case test signals a failure. err is a tuple of
179    # the form returned by sys.exc_info(): (type, value, traceback)"
180    # ...
181    # "wasSuccessful() - Returns True if all tests run so far have passed,
182    # otherwise returns False"
183    # ...
184    # "testsRun - The total number of tests run so far."
185    # ...
186    # "errors - A list containing 2-tuples of TestCase instances and
187    # formatted tracebacks. Each tuple represents a test which raised an
188    # unexpected exception. Contains formatted
189    # tracebacks instead of sys.exc_info() results."
190    # ...
191    # "failures - A list containing 2-tuples of TestCase instances and
192    # formatted tracebacks. Each tuple represents a test where a failure was
193    # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
194    # methods. Contains formatted tracebacks instead
195    # of sys.exc_info() results."
196    def test_addFailure(self):
197        class Foo(unittest.TestCase):
198            def test_1(self):
199                pass
200
201        test = Foo('test_1')
202        try:
203            test.fail("foo")
204        except:
205            exc_info_tuple = sys.exc_info()
206
207        result = unittest.TestResult()
208
209        result.startTest(test)
210        result.addFailure(test, exc_info_tuple)
211        result.stopTest(test)
212
213        self.assertFalse(result.wasSuccessful())
214        self.assertEqual(len(result.errors), 0)
215        self.assertEqual(len(result.failures), 1)
216        self.assertEqual(result.testsRun, 1)
217        self.assertEqual(result.shouldStop, False)
218
219        test_case, formatted_exc = result.failures[0]
220        self.assertIs(test_case, test)
221        self.assertIsInstance(formatted_exc, str)
222
223    # "addError(test, err)"
224    # ...
225    # "Called when the test case test raises an unexpected exception err
226    # is a tuple of the form returned by sys.exc_info():
227    # (type, value, traceback)"
228    # ...
229    # "wasSuccessful() - Returns True if all tests run so far have passed,
230    # otherwise returns False"
231    # ...
232    # "testsRun - The total number of tests run so far."
233    # ...
234    # "errors - A list containing 2-tuples of TestCase instances and
235    # formatted tracebacks. Each tuple represents a test which raised an
236    # unexpected exception. Contains formatted
237    # tracebacks instead of sys.exc_info() results."
238    # ...
239    # "failures - A list containing 2-tuples of TestCase instances and
240    # formatted tracebacks. Each tuple represents a test where a failure was
241    # explicitly signalled using the TestCase.fail*() or TestCase.assert*()
242    # methods. Contains formatted tracebacks instead
243    # of sys.exc_info() results."
244    def test_addError(self):
245        class Foo(unittest.TestCase):
246            def test_1(self):
247                pass
248
249        test = Foo('test_1')
250        try:
251            raise TypeError()
252        except:
253            exc_info_tuple = sys.exc_info()
254
255        result = unittest.TestResult()
256
257        result.startTest(test)
258        result.addError(test, exc_info_tuple)
259        result.stopTest(test)
260
261        self.assertFalse(result.wasSuccessful())
262        self.assertEqual(len(result.errors), 1)
263        self.assertEqual(len(result.failures), 0)
264        self.assertEqual(result.testsRun, 1)
265        self.assertEqual(result.shouldStop, False)
266
267        test_case, formatted_exc = result.errors[0]
268        self.assertIs(test_case, test)
269        self.assertIsInstance(formatted_exc, str)
270
271    def test_addError_locals(self):
272        class Foo(unittest.TestCase):
273            def test_1(self):
274                1/0
275
276        test = Foo('test_1')
277        result = unittest.TestResult()
278        result.tb_locals = True
279
280        unittest.result.traceback = MockTraceback
281        self.addCleanup(restore_traceback)
282        result.startTestRun()
283        test.run(result)
284        result.stopTestRun()
285
286        self.assertEqual(len(result.errors), 1)
287        test_case, formatted_exc = result.errors[0]
288        self.assertEqual('A tracebacklocals', formatted_exc)
289
290    def test_addSubTest(self):
291        class Foo(unittest.TestCase):
292            def test_1(self):
293                nonlocal subtest
294                with self.subTest(foo=1):
295                    subtest = self._subtest
296                    try:
297                        1/0
298                    except ZeroDivisionError:
299                        exc_info_tuple = sys.exc_info()
300                    # Register an error by hand (to check the API)
301                    result.addSubTest(test, subtest, exc_info_tuple)
302                    # Now trigger a failure
303                    self.fail("some recognizable failure")
304
305        subtest = None
306        test = Foo('test_1')
307        result = unittest.TestResult()
308
309        test.run(result)
310
311        self.assertFalse(result.wasSuccessful())
312        self.assertEqual(len(result.errors), 1)
313        self.assertEqual(len(result.failures), 1)
314        self.assertEqual(result.testsRun, 1)
315        self.assertEqual(result.shouldStop, False)
316
317        test_case, formatted_exc = result.errors[0]
318        self.assertIs(test_case, subtest)
319        self.assertIn("ZeroDivisionError", formatted_exc)
320        test_case, formatted_exc = result.failures[0]
321        self.assertIs(test_case, subtest)
322        self.assertIn("some recognizable failure", formatted_exc)
323
324    def testGetDescriptionWithoutDocstring(self):
325        result = unittest.TextTestResult(None, True, 1)
326        self.assertEqual(
327                result.getDescription(self),
328                'testGetDescriptionWithoutDocstring (' + __name__ +
329                '.Test_TestResult)')
330
331    def testGetSubTestDescriptionWithoutDocstring(self):
332        with self.subTest(foo=1, bar=2):
333            result = unittest.TextTestResult(None, True, 1)
334            self.assertEqual(
335                    result.getDescription(self._subtest),
336                    'testGetSubTestDescriptionWithoutDocstring (' + __name__ +
337                    '.Test_TestResult) (foo=1, bar=2)')
338        with self.subTest('some message'):
339            result = unittest.TextTestResult(None, True, 1)
340            self.assertEqual(
341                    result.getDescription(self._subtest),
342                    'testGetSubTestDescriptionWithoutDocstring (' + __name__ +
343                    '.Test_TestResult) [some message]')
344
345    def testGetSubTestDescriptionWithoutDocstringAndParams(self):
346        with self.subTest():
347            result = unittest.TextTestResult(None, True, 1)
348            self.assertEqual(
349                    result.getDescription(self._subtest),
350                    'testGetSubTestDescriptionWithoutDocstringAndParams '
351                    '(' + __name__ + '.Test_TestResult) (<subtest>)')
352
353    def testGetSubTestDescriptionForFalsyValues(self):
354        expected = 'testGetSubTestDescriptionForFalsyValues (%s.Test_TestResult) [%s]'
355        result = unittest.TextTestResult(None, True, 1)
356        for arg in [0, None, []]:
357            with self.subTest(arg):
358                self.assertEqual(
359                    result.getDescription(self._subtest),
360                    expected % (__name__, arg)
361                )
362
363    def testGetNestedSubTestDescriptionWithoutDocstring(self):
364        with self.subTest(foo=1):
365            with self.subTest(baz=2, bar=3):
366                result = unittest.TextTestResult(None, True, 1)
367                self.assertEqual(
368                        result.getDescription(self._subtest),
369                        'testGetNestedSubTestDescriptionWithoutDocstring '
370                        '(' + __name__ + '.Test_TestResult) (baz=2, bar=3, foo=1)')
371
372    def testGetDuplicatedNestedSubTestDescriptionWithoutDocstring(self):
373        with self.subTest(foo=1, bar=2):
374            with self.subTest(baz=3, bar=4):
375                result = unittest.TextTestResult(None, True, 1)
376                self.assertEqual(
377                        result.getDescription(self._subtest),
378                        'testGetDuplicatedNestedSubTestDescriptionWithoutDocstring '
379                        '(' + __name__ + '.Test_TestResult) (baz=3, bar=4, foo=1)')
380
381    @unittest.skipIf(sys.flags.optimize >= 2,
382                     "Docstrings are omitted with -O2 and above")
383    def testGetDescriptionWithOneLineDocstring(self):
384        """Tests getDescription() for a method with a docstring."""
385        result = unittest.TextTestResult(None, True, 1)
386        self.assertEqual(
387                result.getDescription(self),
388               ('testGetDescriptionWithOneLineDocstring '
389                '(' + __name__ + '.Test_TestResult)\n'
390                'Tests getDescription() for a method with a docstring.'))
391
392    @unittest.skipIf(sys.flags.optimize >= 2,
393                     "Docstrings are omitted with -O2 and above")
394    def testGetSubTestDescriptionWithOneLineDocstring(self):
395        """Tests getDescription() for a method with a docstring."""
396        result = unittest.TextTestResult(None, True, 1)
397        with self.subTest(foo=1, bar=2):
398            self.assertEqual(
399                result.getDescription(self._subtest),
400               ('testGetSubTestDescriptionWithOneLineDocstring '
401                '(' + __name__ + '.Test_TestResult) (foo=1, bar=2)\n'
402                'Tests getDescription() for a method with a docstring.'))
403
404    @unittest.skipIf(sys.flags.optimize >= 2,
405                     "Docstrings are omitted with -O2 and above")
406    def testGetDescriptionWithMultiLineDocstring(self):
407        """Tests getDescription() for a method with a longer docstring.
408        The second line of the docstring.
409        """
410        result = unittest.TextTestResult(None, True, 1)
411        self.assertEqual(
412                result.getDescription(self),
413               ('testGetDescriptionWithMultiLineDocstring '
414                '(' + __name__ + '.Test_TestResult)\n'
415                'Tests getDescription() for a method with a longer '
416                'docstring.'))
417
418    @unittest.skipIf(sys.flags.optimize >= 2,
419                     "Docstrings are omitted with -O2 and above")
420    def testGetSubTestDescriptionWithMultiLineDocstring(self):
421        """Tests getDescription() for a method with a longer docstring.
422        The second line of the docstring.
423        """
424        result = unittest.TextTestResult(None, True, 1)
425        with self.subTest(foo=1, bar=2):
426            self.assertEqual(
427                result.getDescription(self._subtest),
428               ('testGetSubTestDescriptionWithMultiLineDocstring '
429                '(' + __name__ + '.Test_TestResult) (foo=1, bar=2)\n'
430                'Tests getDescription() for a method with a longer '
431                'docstring.'))
432
433    def testStackFrameTrimming(self):
434        class Frame(object):
435            class tb_frame(object):
436                f_globals = {}
437        result = unittest.TestResult()
438        self.assertFalse(result._is_relevant_tb_level(Frame))
439
440        Frame.tb_frame.f_globals['__unittest'] = True
441        self.assertTrue(result._is_relevant_tb_level(Frame))
442
443    def testFailFast(self):
444        result = unittest.TestResult()
445        result._exc_info_to_string = lambda *_: ''
446        result.failfast = True
447        result.addError(None, None)
448        self.assertTrue(result.shouldStop)
449
450        result = unittest.TestResult()
451        result._exc_info_to_string = lambda *_: ''
452        result.failfast = True
453        result.addFailure(None, None)
454        self.assertTrue(result.shouldStop)
455
456        result = unittest.TestResult()
457        result._exc_info_to_string = lambda *_: ''
458        result.failfast = True
459        result.addUnexpectedSuccess(None)
460        self.assertTrue(result.shouldStop)
461
462    def testFailFastSetByRunner(self):
463        stream = BufferedWriter()
464        runner = unittest.TextTestRunner(stream=stream, failfast=True)
465        def test(result):
466            self.assertTrue(result.failfast)
467        result = runner.run(test)
468        stream.flush()
469        self.assertTrue(stream.getvalue().endswith('\n\nOK\n'))
470
471
472classDict = dict(unittest.TestResult.__dict__)
473for m in ('addSkip', 'addExpectedFailure', 'addUnexpectedSuccess',
474           '__init__'):
475    del classDict[m]
476
477def __init__(self, stream=None, descriptions=None, verbosity=None):
478    self.failures = []
479    self.errors = []
480    self.testsRun = 0
481    self.shouldStop = False
482    self.buffer = False
483    self.tb_locals = False
484
485classDict['__init__'] = __init__
486OldResult = type('OldResult', (object,), classDict)
487
488class Test_OldTestResult(unittest.TestCase):
489
490    def assertOldResultWarning(self, test, failures):
491        with warnings_helper.check_warnings(
492                ("TestResult has no add.+ method,", RuntimeWarning)):
493            result = OldResult()
494            test.run(result)
495            self.assertEqual(len(result.failures), failures)
496
497    def testOldTestResult(self):
498        class Test(unittest.TestCase):
499            def testSkip(self):
500                self.skipTest('foobar')
501            @unittest.expectedFailure
502            def testExpectedFail(self):
503                raise TypeError
504            @unittest.expectedFailure
505            def testUnexpectedSuccess(self):
506                pass
507
508        for test_name, should_pass in (('testSkip', True),
509                                       ('testExpectedFail', True),
510                                       ('testUnexpectedSuccess', False)):
511            test = Test(test_name)
512            self.assertOldResultWarning(test, int(not should_pass))
513
514    def testOldTestTesultSetup(self):
515        class Test(unittest.TestCase):
516            def setUp(self):
517                self.skipTest('no reason')
518            def testFoo(self):
519                pass
520        self.assertOldResultWarning(Test('testFoo'), 0)
521
522    def testOldTestResultClass(self):
523        @unittest.skip('no reason')
524        class Test(unittest.TestCase):
525            def testFoo(self):
526                pass
527        self.assertOldResultWarning(Test('testFoo'), 0)
528
529    def testOldResultWithRunner(self):
530        class Test(unittest.TestCase):
531            def testFoo(self):
532                pass
533        runner = unittest.TextTestRunner(resultclass=OldResult,
534                                          stream=io.StringIO())
535        # This will raise an exception if TextTestRunner can't handle old
536        # test result objects
537        runner.run(Test('testFoo'))
538
539
540class TestOutputBuffering(unittest.TestCase):
541
542    def setUp(self):
543        self._real_out = sys.stdout
544        self._real_err = sys.stderr
545
546    def tearDown(self):
547        sys.stdout = self._real_out
548        sys.stderr = self._real_err
549
550    def testBufferOutputOff(self):
551        real_out = self._real_out
552        real_err = self._real_err
553
554        result = unittest.TestResult()
555        self.assertFalse(result.buffer)
556
557        self.assertIs(real_out, sys.stdout)
558        self.assertIs(real_err, sys.stderr)
559
560        result.startTest(self)
561
562        self.assertIs(real_out, sys.stdout)
563        self.assertIs(real_err, sys.stderr)
564
565    def testBufferOutputStartTestAddSuccess(self):
566        real_out = self._real_out
567        real_err = self._real_err
568
569        result = unittest.TestResult()
570        self.assertFalse(result.buffer)
571
572        result.buffer = True
573
574        self.assertIs(real_out, sys.stdout)
575        self.assertIs(real_err, sys.stderr)
576
577        result.startTest(self)
578
579        self.assertIsNot(real_out, sys.stdout)
580        self.assertIsNot(real_err, sys.stderr)
581        self.assertIsInstance(sys.stdout, io.StringIO)
582        self.assertIsInstance(sys.stderr, io.StringIO)
583        self.assertIsNot(sys.stdout, sys.stderr)
584
585        out_stream = sys.stdout
586        err_stream = sys.stderr
587
588        result._original_stdout = io.StringIO()
589        result._original_stderr = io.StringIO()
590
591        print('foo')
592        print('bar', file=sys.stderr)
593
594        self.assertEqual(out_stream.getvalue(), 'foo\n')
595        self.assertEqual(err_stream.getvalue(), 'bar\n')
596
597        self.assertEqual(result._original_stdout.getvalue(), '')
598        self.assertEqual(result._original_stderr.getvalue(), '')
599
600        result.addSuccess(self)
601        result.stopTest(self)
602
603        self.assertIs(sys.stdout, result._original_stdout)
604        self.assertIs(sys.stderr, result._original_stderr)
605
606        self.assertEqual(result._original_stdout.getvalue(), '')
607        self.assertEqual(result._original_stderr.getvalue(), '')
608
609        self.assertEqual(out_stream.getvalue(), '')
610        self.assertEqual(err_stream.getvalue(), '')
611
612
613    def getStartedResult(self):
614        result = unittest.TestResult()
615        result.buffer = True
616        result.startTest(self)
617        return result
618
619    def testBufferOutputAddErrorOrFailure(self):
620        unittest.result.traceback = MockTraceback
621        self.addCleanup(restore_traceback)
622
623        for message_attr, add_attr, include_error in [
624            ('errors', 'addError', True),
625            ('failures', 'addFailure', False),
626            ('errors', 'addError', True),
627            ('failures', 'addFailure', False)
628        ]:
629            result = self.getStartedResult()
630            buffered_out = sys.stdout
631            buffered_err = sys.stderr
632            result._original_stdout = io.StringIO()
633            result._original_stderr = io.StringIO()
634
635            print('foo', file=sys.stdout)
636            if include_error:
637                print('bar', file=sys.stderr)
638
639
640            addFunction = getattr(result, add_attr)
641            addFunction(self, (None, None, None))
642            result.stopTest(self)
643
644            result_list = getattr(result, message_attr)
645            self.assertEqual(len(result_list), 1)
646
647            test, message = result_list[0]
648            expectedOutMessage = textwrap.dedent("""
649                Stdout:
650                foo
651            """)
652            expectedErrMessage = ''
653            if include_error:
654                expectedErrMessage = textwrap.dedent("""
655                Stderr:
656                bar
657            """)
658
659            expectedFullMessage = 'A traceback%s%s' % (expectedOutMessage, expectedErrMessage)
660
661            self.assertIs(test, self)
662            self.assertEqual(result._original_stdout.getvalue(), expectedOutMessage)
663            self.assertEqual(result._original_stderr.getvalue(), expectedErrMessage)
664            self.assertMultiLineEqual(message, expectedFullMessage)
665
666    def testBufferSetUp(self):
667        with captured_stdout() as stdout:
668            result = unittest.TestResult()
669        result.buffer = True
670
671        class Foo(unittest.TestCase):
672            def setUp(self):
673                print('set up')
674                1/0
675            def test_foo(self):
676                pass
677        suite = unittest.TestSuite([Foo('test_foo')])
678        suite(result)
679        expected_out = '\nStdout:\nset up\n'
680        self.assertEqual(stdout.getvalue(), expected_out)
681        self.assertEqual(len(result.errors), 1)
682        description = f'test_foo ({strclass(Foo)})'
683        test_case, formatted_exc = result.errors[0]
684        self.assertEqual(str(test_case), description)
685        self.assertIn('ZeroDivisionError: division by zero', formatted_exc)
686        self.assertIn(expected_out, formatted_exc)
687
688    def testBufferTearDown(self):
689        with captured_stdout() as stdout:
690            result = unittest.TestResult()
691        result.buffer = True
692
693        class Foo(unittest.TestCase):
694            def tearDown(self):
695                print('tear down')
696                1/0
697            def test_foo(self):
698                pass
699        suite = unittest.TestSuite([Foo('test_foo')])
700        suite(result)
701        expected_out = '\nStdout:\ntear down\n'
702        self.assertEqual(stdout.getvalue(), expected_out)
703        self.assertEqual(len(result.errors), 1)
704        description = f'test_foo ({strclass(Foo)})'
705        test_case, formatted_exc = result.errors[0]
706        self.assertEqual(str(test_case), description)
707        self.assertIn('ZeroDivisionError: division by zero', formatted_exc)
708        self.assertIn(expected_out, formatted_exc)
709
710    def testBufferDoCleanups(self):
711        with captured_stdout() as stdout:
712            result = unittest.TestResult()
713        result.buffer = True
714
715        class Foo(unittest.TestCase):
716            def setUp(self):
717                print('set up')
718                self.addCleanup(bad_cleanup1)
719                self.addCleanup(bad_cleanup2)
720            def test_foo(self):
721                pass
722        suite = unittest.TestSuite([Foo('test_foo')])
723        suite(result)
724        expected_out = '\nStdout:\nset up\ndo cleanup2\ndo cleanup1\n'
725        self.assertEqual(stdout.getvalue(), expected_out)
726        self.assertEqual(len(result.errors), 2)
727        description = f'test_foo ({strclass(Foo)})'
728        test_case, formatted_exc = result.errors[0]
729        self.assertEqual(str(test_case), description)
730        self.assertIn('ValueError: bad cleanup2', formatted_exc)
731        self.assertNotIn('TypeError', formatted_exc)
732        self.assertIn(expected_out, formatted_exc)
733        test_case, formatted_exc = result.errors[1]
734        self.assertEqual(str(test_case), description)
735        self.assertIn('TypeError: bad cleanup1', formatted_exc)
736        self.assertNotIn('ValueError', formatted_exc)
737        self.assertIn(expected_out, formatted_exc)
738
739    def testBufferSetUp_DoCleanups(self):
740        with captured_stdout() as stdout:
741            result = unittest.TestResult()
742        result.buffer = True
743
744        class Foo(unittest.TestCase):
745            def setUp(self):
746                print('set up')
747                self.addCleanup(bad_cleanup1)
748                self.addCleanup(bad_cleanup2)
749                1/0
750            def test_foo(self):
751                pass
752        suite = unittest.TestSuite([Foo('test_foo')])
753        suite(result)
754        expected_out = '\nStdout:\nset up\ndo cleanup2\ndo cleanup1\n'
755        self.assertEqual(stdout.getvalue(), expected_out)
756        self.assertEqual(len(result.errors), 3)
757        description = f'test_foo ({strclass(Foo)})'
758        test_case, formatted_exc = result.errors[0]
759        self.assertEqual(str(test_case), description)
760        self.assertIn('ZeroDivisionError: division by zero', formatted_exc)
761        self.assertNotIn('ValueError', formatted_exc)
762        self.assertNotIn('TypeError', formatted_exc)
763        self.assertIn(expected_out, formatted_exc)
764        test_case, formatted_exc = result.errors[1]
765        self.assertEqual(str(test_case), description)
766        self.assertIn('ValueError: bad cleanup2', formatted_exc)
767        self.assertNotIn('ZeroDivisionError', formatted_exc)
768        self.assertNotIn('TypeError', formatted_exc)
769        self.assertIn(expected_out, formatted_exc)
770        test_case, formatted_exc = result.errors[2]
771        self.assertEqual(str(test_case), description)
772        self.assertIn('TypeError: bad cleanup1', formatted_exc)
773        self.assertNotIn('ZeroDivisionError', formatted_exc)
774        self.assertNotIn('ValueError', formatted_exc)
775        self.assertIn(expected_out, formatted_exc)
776
777    def testBufferTearDown_DoCleanups(self):
778        with captured_stdout() as stdout:
779            result = unittest.TestResult()
780        result.buffer = True
781
782        class Foo(unittest.TestCase):
783            def setUp(self):
784                print('set up')
785                self.addCleanup(bad_cleanup1)
786                self.addCleanup(bad_cleanup2)
787            def tearDown(self):
788                print('tear down')
789                1/0
790            def test_foo(self):
791                pass
792        suite = unittest.TestSuite([Foo('test_foo')])
793        suite(result)
794        expected_out = '\nStdout:\nset up\ntear down\ndo cleanup2\ndo cleanup1\n'
795        self.assertEqual(stdout.getvalue(), expected_out)
796        self.assertEqual(len(result.errors), 3)
797        description = f'test_foo ({strclass(Foo)})'
798        test_case, formatted_exc = result.errors[0]
799        self.assertEqual(str(test_case), description)
800        self.assertIn('ZeroDivisionError: division by zero', formatted_exc)
801        self.assertNotIn('ValueError', formatted_exc)
802        self.assertNotIn('TypeError', formatted_exc)
803        self.assertIn(expected_out, formatted_exc)
804        test_case, formatted_exc = result.errors[1]
805        self.assertEqual(str(test_case), description)
806        self.assertIn('ValueError: bad cleanup2', formatted_exc)
807        self.assertNotIn('ZeroDivisionError', formatted_exc)
808        self.assertNotIn('TypeError', formatted_exc)
809        self.assertIn(expected_out, formatted_exc)
810        test_case, formatted_exc = result.errors[2]
811        self.assertEqual(str(test_case), description)
812        self.assertIn('TypeError: bad cleanup1', formatted_exc)
813        self.assertNotIn('ZeroDivisionError', formatted_exc)
814        self.assertNotIn('ValueError', formatted_exc)
815        self.assertIn(expected_out, formatted_exc)
816
817    def testBufferSetupClass(self):
818        with captured_stdout() as stdout:
819            result = unittest.TestResult()
820        result.buffer = True
821
822        class Foo(unittest.TestCase):
823            @classmethod
824            def setUpClass(cls):
825                print('set up class')
826                1/0
827            def test_foo(self):
828                pass
829        suite = unittest.TestSuite([Foo('test_foo')])
830        suite(result)
831        expected_out = '\nStdout:\nset up class\n'
832        self.assertEqual(stdout.getvalue(), expected_out)
833        self.assertEqual(len(result.errors), 1)
834        description = f'setUpClass ({strclass(Foo)})'
835        test_case, formatted_exc = result.errors[0]
836        self.assertEqual(test_case.description, description)
837        self.assertIn('ZeroDivisionError: division by zero', formatted_exc)
838        self.assertIn(expected_out, formatted_exc)
839
840    def testBufferTearDownClass(self):
841        with captured_stdout() as stdout:
842            result = unittest.TestResult()
843        result.buffer = True
844
845        class Foo(unittest.TestCase):
846            @classmethod
847            def tearDownClass(cls):
848                print('tear down class')
849                1/0
850            def test_foo(self):
851                pass
852        suite = unittest.TestSuite([Foo('test_foo')])
853        suite(result)
854        expected_out = '\nStdout:\ntear down class\n'
855        self.assertEqual(stdout.getvalue(), expected_out)
856        self.assertEqual(len(result.errors), 1)
857        description = f'tearDownClass ({strclass(Foo)})'
858        test_case, formatted_exc = result.errors[0]
859        self.assertEqual(test_case.description, description)
860        self.assertIn('ZeroDivisionError: division by zero', formatted_exc)
861        self.assertIn(expected_out, formatted_exc)
862
863    def testBufferDoClassCleanups(self):
864        with captured_stdout() as stdout:
865            result = unittest.TestResult()
866        result.buffer = True
867
868        class Foo(unittest.TestCase):
869            @classmethod
870            def setUpClass(cls):
871                print('set up class')
872                cls.addClassCleanup(bad_cleanup1)
873                cls.addClassCleanup(bad_cleanup2)
874            @classmethod
875            def tearDownClass(cls):
876                print('tear down class')
877            def test_foo(self):
878                pass
879        suite = unittest.TestSuite([Foo('test_foo')])
880        suite(result)
881        expected_out = '\nStdout:\ntear down class\ndo cleanup2\ndo cleanup1\n'
882        self.assertEqual(stdout.getvalue(), expected_out)
883        self.assertEqual(len(result.errors), 2)
884        description = f'tearDownClass ({strclass(Foo)})'
885        test_case, formatted_exc = result.errors[0]
886        self.assertEqual(test_case.description, description)
887        self.assertIn('ValueError: bad cleanup2', formatted_exc)
888        self.assertNotIn('TypeError', formatted_exc)
889        self.assertIn(expected_out, formatted_exc)
890        test_case, formatted_exc = result.errors[1]
891        self.assertEqual(test_case.description, description)
892        self.assertIn('TypeError: bad cleanup1', formatted_exc)
893        self.assertNotIn('ValueError', formatted_exc)
894        self.assertIn(expected_out, formatted_exc)
895
896    def testBufferSetupClass_DoClassCleanups(self):
897        with captured_stdout() as stdout:
898            result = unittest.TestResult()
899        result.buffer = True
900
901        class Foo(unittest.TestCase):
902            @classmethod
903            def setUpClass(cls):
904                print('set up class')
905                cls.addClassCleanup(bad_cleanup1)
906                cls.addClassCleanup(bad_cleanup2)
907                1/0
908            def test_foo(self):
909                pass
910        suite = unittest.TestSuite([Foo('test_foo')])
911        suite(result)
912        expected_out = '\nStdout:\nset up class\ndo cleanup2\ndo cleanup1\n'
913        self.assertEqual(stdout.getvalue(), expected_out)
914        self.assertEqual(len(result.errors), 3)
915        description = f'setUpClass ({strclass(Foo)})'
916        test_case, formatted_exc = result.errors[0]
917        self.assertEqual(test_case.description, description)
918        self.assertIn('ZeroDivisionError: division by zero', formatted_exc)
919        self.assertNotIn('ValueError', formatted_exc)
920        self.assertNotIn('TypeError', formatted_exc)
921        self.assertIn('\nStdout:\nset up class\n', formatted_exc)
922        test_case, formatted_exc = result.errors[1]
923        self.assertEqual(test_case.description, description)
924        self.assertIn('ValueError: bad cleanup2', formatted_exc)
925        self.assertNotIn('ZeroDivisionError', formatted_exc)
926        self.assertNotIn('TypeError', formatted_exc)
927        self.assertIn(expected_out, formatted_exc)
928        test_case, formatted_exc = result.errors[2]
929        self.assertEqual(test_case.description, description)
930        self.assertIn('TypeError: bad cleanup1', formatted_exc)
931        self.assertNotIn('ZeroDivisionError', formatted_exc)
932        self.assertNotIn('ValueError', formatted_exc)
933        self.assertIn(expected_out, formatted_exc)
934
935    def testBufferTearDownClass_DoClassCleanups(self):
936        with captured_stdout() as stdout:
937            result = unittest.TestResult()
938        result.buffer = True
939
940        class Foo(unittest.TestCase):
941            @classmethod
942            def setUpClass(cls):
943                print('set up class')
944                cls.addClassCleanup(bad_cleanup1)
945                cls.addClassCleanup(bad_cleanup2)
946            @classmethod
947            def tearDownClass(cls):
948                print('tear down class')
949                1/0
950            def test_foo(self):
951                pass
952        suite = unittest.TestSuite([Foo('test_foo')])
953        suite(result)
954        expected_out = '\nStdout:\ntear down class\ndo cleanup2\ndo cleanup1\n'
955        self.assertEqual(stdout.getvalue(), expected_out)
956        self.assertEqual(len(result.errors), 3)
957        description = f'tearDownClass ({strclass(Foo)})'
958        test_case, formatted_exc = result.errors[0]
959        self.assertEqual(test_case.description, description)
960        self.assertIn('ZeroDivisionError: division by zero', formatted_exc)
961        self.assertNotIn('ValueError', formatted_exc)
962        self.assertNotIn('TypeError', formatted_exc)
963        self.assertIn('\nStdout:\ntear down class\n', formatted_exc)
964        test_case, formatted_exc = result.errors[1]
965        self.assertEqual(test_case.description, description)
966        self.assertIn('ValueError: bad cleanup2', formatted_exc)
967        self.assertNotIn('ZeroDivisionError', formatted_exc)
968        self.assertNotIn('TypeError', formatted_exc)
969        self.assertIn(expected_out, formatted_exc)
970        test_case, formatted_exc = result.errors[2]
971        self.assertEqual(test_case.description, description)
972        self.assertIn('TypeError: bad cleanup1', formatted_exc)
973        self.assertNotIn('ZeroDivisionError', formatted_exc)
974        self.assertNotIn('ValueError', formatted_exc)
975        self.assertIn(expected_out, formatted_exc)
976
977    def testBufferSetUpModule(self):
978        with captured_stdout() as stdout:
979            result = unittest.TestResult()
980        result.buffer = True
981
982        class Foo(unittest.TestCase):
983            def test_foo(self):
984                pass
985        class Module(object):
986            @staticmethod
987            def setUpModule():
988                print('set up module')
989                1/0
990
991        Foo.__module__ = 'Module'
992        sys.modules['Module'] = Module
993        self.addCleanup(sys.modules.pop, 'Module')
994        suite = unittest.TestSuite([Foo('test_foo')])
995        suite(result)
996        expected_out = '\nStdout:\nset up module\n'
997        self.assertEqual(stdout.getvalue(), expected_out)
998        self.assertEqual(len(result.errors), 1)
999        description = 'setUpModule (Module)'
1000        test_case, formatted_exc = result.errors[0]
1001        self.assertEqual(test_case.description, description)
1002        self.assertIn('ZeroDivisionError: division by zero', formatted_exc)
1003        self.assertIn(expected_out, formatted_exc)
1004
1005    def testBufferTearDownModule(self):
1006        with captured_stdout() as stdout:
1007            result = unittest.TestResult()
1008        result.buffer = True
1009
1010        class Foo(unittest.TestCase):
1011            def test_foo(self):
1012                pass
1013        class Module(object):
1014            @staticmethod
1015            def tearDownModule():
1016                print('tear down module')
1017                1/0
1018
1019        Foo.__module__ = 'Module'
1020        sys.modules['Module'] = Module
1021        self.addCleanup(sys.modules.pop, 'Module')
1022        suite = unittest.TestSuite([Foo('test_foo')])
1023        suite(result)
1024        expected_out = '\nStdout:\ntear down module\n'
1025        self.assertEqual(stdout.getvalue(), expected_out)
1026        self.assertEqual(len(result.errors), 1)
1027        description = 'tearDownModule (Module)'
1028        test_case, formatted_exc = result.errors[0]
1029        self.assertEqual(test_case.description, description)
1030        self.assertIn('ZeroDivisionError: division by zero', formatted_exc)
1031        self.assertIn(expected_out, formatted_exc)
1032
1033    def testBufferDoModuleCleanups(self):
1034        with captured_stdout() as stdout:
1035            result = unittest.TestResult()
1036        result.buffer = True
1037
1038        class Foo(unittest.TestCase):
1039            def test_foo(self):
1040                pass
1041        class Module(object):
1042            @staticmethod
1043            def setUpModule():
1044                print('set up module')
1045                unittest.addModuleCleanup(bad_cleanup1)
1046                unittest.addModuleCleanup(bad_cleanup2)
1047
1048        Foo.__module__ = 'Module'
1049        sys.modules['Module'] = Module
1050        self.addCleanup(sys.modules.pop, 'Module')
1051        suite = unittest.TestSuite([Foo('test_foo')])
1052        suite(result)
1053        expected_out = '\nStdout:\ndo cleanup2\ndo cleanup1\n'
1054        self.assertEqual(stdout.getvalue(), expected_out)
1055        self.assertEqual(len(result.errors), 1)
1056        description = 'tearDownModule (Module)'
1057        test_case, formatted_exc = result.errors[0]
1058        self.assertEqual(test_case.description, description)
1059        self.assertIn('ValueError: bad cleanup2', formatted_exc)
1060        self.assertNotIn('TypeError', formatted_exc)
1061        self.assertIn(expected_out, formatted_exc)
1062
1063    def testBufferSetUpModule_DoModuleCleanups(self):
1064        with captured_stdout() as stdout:
1065            result = unittest.TestResult()
1066        result.buffer = True
1067
1068        class Foo(unittest.TestCase):
1069            def test_foo(self):
1070                pass
1071        class Module(object):
1072            @staticmethod
1073            def setUpModule():
1074                print('set up module')
1075                unittest.addModuleCleanup(bad_cleanup1)
1076                unittest.addModuleCleanup(bad_cleanup2)
1077                1/0
1078
1079        Foo.__module__ = 'Module'
1080        sys.modules['Module'] = Module
1081        self.addCleanup(sys.modules.pop, 'Module')
1082        suite = unittest.TestSuite([Foo('test_foo')])
1083        suite(result)
1084        expected_out = '\nStdout:\nset up module\ndo cleanup2\ndo cleanup1\n'
1085        self.assertEqual(stdout.getvalue(), expected_out)
1086        self.assertEqual(len(result.errors), 2)
1087        description = 'setUpModule (Module)'
1088        test_case, formatted_exc = result.errors[0]
1089        self.assertEqual(test_case.description, description)
1090        self.assertIn('ZeroDivisionError: division by zero', formatted_exc)
1091        self.assertNotIn('ValueError', formatted_exc)
1092        self.assertNotIn('TypeError', formatted_exc)
1093        self.assertIn('\nStdout:\nset up module\n', formatted_exc)
1094        test_case, formatted_exc = result.errors[1]
1095        self.assertIn(expected_out, formatted_exc)
1096        self.assertEqual(test_case.description, description)
1097        self.assertIn('ValueError: bad cleanup2', formatted_exc)
1098        self.assertNotIn('ZeroDivisionError', formatted_exc)
1099        self.assertNotIn('TypeError', formatted_exc)
1100        self.assertIn(expected_out, formatted_exc)
1101
1102    def testBufferTearDownModule_DoModuleCleanups(self):
1103        with captured_stdout() as stdout:
1104            result = unittest.TestResult()
1105        result.buffer = True
1106
1107        class Foo(unittest.TestCase):
1108            def test_foo(self):
1109                pass
1110        class Module(object):
1111            @staticmethod
1112            def setUpModule():
1113                print('set up module')
1114                unittest.addModuleCleanup(bad_cleanup1)
1115                unittest.addModuleCleanup(bad_cleanup2)
1116            @staticmethod
1117            def tearDownModule():
1118                print('tear down module')
1119                1/0
1120
1121        Foo.__module__ = 'Module'
1122        sys.modules['Module'] = Module
1123        self.addCleanup(sys.modules.pop, 'Module')
1124        suite = unittest.TestSuite([Foo('test_foo')])
1125        suite(result)
1126        expected_out = '\nStdout:\ntear down module\ndo cleanup2\ndo cleanup1\n'
1127        self.assertEqual(stdout.getvalue(), expected_out)
1128        self.assertEqual(len(result.errors), 2)
1129        description = 'tearDownModule (Module)'
1130        test_case, formatted_exc = result.errors[0]
1131        self.assertEqual(test_case.description, description)
1132        self.assertIn('ZeroDivisionError: division by zero', formatted_exc)
1133        self.assertNotIn('ValueError', formatted_exc)
1134        self.assertNotIn('TypeError', formatted_exc)
1135        self.assertIn('\nStdout:\ntear down module\n', formatted_exc)
1136        test_case, formatted_exc = result.errors[1]
1137        self.assertEqual(test_case.description, description)
1138        self.assertIn('ValueError: bad cleanup2', formatted_exc)
1139        self.assertNotIn('ZeroDivisionError', formatted_exc)
1140        self.assertNotIn('TypeError', formatted_exc)
1141        self.assertIn(expected_out, formatted_exc)
1142
1143
1144if __name__ == '__main__':
1145    unittest.main()
1146