Debian Codegraph @

Lib/test/test_unittest/test_runner.py

raw
1 import io
2 import os
3 import sys
4 import pickle
5 import subprocess
6 from test import support
7
8 import unittest
9 from unittest.case import _Outcome
10
11 from test.test_unittest.support import (
12 BufferedWriter,
13 LoggingResult,
14 ResultWithNoStartTestRunStopTestRun,
15 )
16
17
18 def resultFactory(*_):
19 return unittest.TestResult()
20
21
22 def getRunner():
23 return unittest.TextTestRunner(resultclass=resultFactory,
24 stream=io.StringIO())
25
26
27 class CustomError(Exception):
28 pass
29
30 # For test output compat:
31 CustomErrorRepr = f"{__name__ + '.' if __name__ != '__main__' else ''}CustomError"
32
33
34 def runTests(*cases):
35 suite = unittest.TestSuite()
36 for case in cases:
37 tests = unittest.defaultTestLoader.loadTestsFromTestCase(case)
38 suite.addTests(tests)
39
40 runner = getRunner()
41
42 # creating a nested suite exposes some potential bugs
43 realSuite = unittest.TestSuite()
44 realSuite.addTest(suite)
45 # adding empty suites to the end exposes potential bugs
46 suite.addTest(unittest.TestSuite())
47 realSuite.addTest(unittest.TestSuite())
48 return runner.run(realSuite)
49
50
51 def cleanup(ordering, blowUp=False):
52 if not blowUp:
53 ordering.append('cleanup_good')
54 else:
55 ordering.append('cleanup_exc')
56 raise CustomError('CleanUpExc')
57
58
59 class TestCM:
60 def __init__(self, ordering, enter_result=None):
61 self.ordering = ordering
62 self.enter_result = enter_result
63
64 def __enter__(self):
65 self.ordering.append('enter')
66 return self.enter_result
67
68 def __exit__(self, *exc_info):
69 self.ordering.append('exit')
70
71
72 class LacksEnterAndExit:
73 pass
74 class LacksEnter:
75 def __exit__(self, *exc_info):
76 pass
77 class LacksExit:
78 def __enter__(self):
79 pass
80
81
82 class TestCleanUp(unittest.TestCase):
83 def testCleanUp(self):
84 class TestableTest(unittest.TestCase):
85 def testNothing(self):
86 pass
87
88 test = TestableTest('testNothing')
89 self.assertEqual(test._cleanups, [])
90
91 cleanups = []
92
93 def cleanup1(*args, **kwargs):
94 cleanups.append((1, args, kwargs))
95
96 def cleanup2(*args, **kwargs):
97 cleanups.append((2, args, kwargs))
98
99 test.addCleanup(cleanup1, 1, 2, 3, four='hello', five='goodbye')
100 test.addCleanup(cleanup2)
101
102 self.assertEqual(test._cleanups,
103 [(cleanup1, (1, 2, 3), dict(four='hello', five='goodbye')),
104 (cleanup2, (), {})])
105
106 self.assertTrue(test.doCleanups())
107 self.assertEqual(cleanups, [(2, (), {}), (1, (1, 2, 3), dict(four='hello', five='goodbye'))])
108
109 @support.force_not_colorized
110 def testCleanUpWithErrors(self):
111 class TestableTest(unittest.TestCase):
112 def testNothing(self):
113 pass
114
115 test = TestableTest('testNothing')
116 result = unittest.TestResult()
117 outcome = test._outcome = _Outcome(result=result)
118
119 CleanUpExc = CustomError('foo')
120 exc2 = CustomError('bar')
121 def cleanup1():
122 raise CleanUpExc
123
124 def cleanup2():
125 raise exc2
126
127 test.addCleanup(cleanup1)
128 test.addCleanup(cleanup2)
129
130 self.assertFalse(test.doCleanups())
131 self.assertFalse(outcome.success)
132
133 (_, msg2), (_, msg1) = result.errors
134 self.assertIn('in cleanup1', msg1)
135 self.assertIn('raise CleanUpExc', msg1)
136 self.assertIn(f'{CustomErrorRepr}: foo', msg1)
137 self.assertIn('in cleanup2', msg2)
138 self.assertIn('raise exc2', msg2)
139 self.assertIn(f'{CustomErrorRepr}: bar', msg2)
140
141 def testCleanupInRun(self):
142 blowUp = False
143 ordering = []
144
145 class TestableTest(unittest.TestCase):
146 def setUp(self):
147 ordering.append('setUp')
148 test.addCleanup(cleanup2)
149 if blowUp:
150 raise CustomError('foo')
151
152 def testNothing(self):
153 ordering.append('test')
154 test.addCleanup(cleanup3)
155
156 def tearDown(self):
157 ordering.append('tearDown')
158
159 test = TestableTest('testNothing')
160
161 def cleanup1():
162 ordering.append('cleanup1')
163 def cleanup2():
164 ordering.append('cleanup2')
165 def cleanup3():
166 ordering.append('cleanup3')
167 test.addCleanup(cleanup1)
168
169 def success(some_test):
170 self.assertEqual(some_test, test)
171 ordering.append('success')
172
173 result = unittest.TestResult()
174 result.addSuccess = success
175
176 test.run(result)
177 self.assertEqual(ordering, ['setUp', 'test', 'tearDown', 'cleanup3',
178 'cleanup2', 'cleanup1', 'success'])
179
180 blowUp = True
181 ordering = []
182 test = TestableTest('testNothing')
183 test.addCleanup(cleanup1)
184 test.run(result)
185 self.assertEqual(ordering, ['setUp', 'cleanup2', 'cleanup1'])
186
187 def testTestCaseDebugExecutesCleanups(self):
188 ordering = []
189
190 class TestableTest(unittest.TestCase):
191 def setUp(self):
192 ordering.append('setUp')
193 self.addCleanup(cleanup1)
194
195 def testNothing(self):
196 ordering.append('test')
197 self.addCleanup(cleanup3)
198
199 def tearDown(self):
200 ordering.append('tearDown')
201 test.addCleanup(cleanup4)
202
203 test = TestableTest('testNothing')
204
205 def cleanup1():
206 ordering.append('cleanup1')
207 test.addCleanup(cleanup2)
208 def cleanup2():
209 ordering.append('cleanup2')
210 def cleanup3():
211 ordering.append('cleanup3')
212 def cleanup4():
213 ordering.append('cleanup4')
214
215 test.debug()
216 self.assertEqual(ordering, ['setUp', 'test', 'tearDown', 'cleanup4',
217 'cleanup3', 'cleanup1', 'cleanup2'])
218
219
220 def test_enterContext(self):
221 class TestableTest(unittest.TestCase):
222 def testNothing(self):
223 pass
224
225 test = TestableTest('testNothing')
226 cleanups = []
227
228 test.addCleanup(cleanups.append, 'cleanup1')
229 cm = TestCM(cleanups, 42)
230 self.assertEqual(test.enterContext(cm), 42)
231 test.addCleanup(cleanups.append, 'cleanup2')
232
233 self.assertTrue(test.doCleanups())
234 self.assertEqual(cleanups, ['enter', 'cleanup2', 'exit', 'cleanup1'])
235
236 def test_enterContext_arg_errors(self):
237 class TestableTest(unittest.TestCase):
238 def testNothing(self):
239 pass
240
241 test = TestableTest('testNothing')
242
243 with self.assertRaisesRegex(TypeError, 'the context manager'):
244 test.enterContext(LacksEnterAndExit())
245 with self.assertRaisesRegex(TypeError, 'the context manager'):
246 test.enterContext(LacksEnter())
247 with self.assertRaisesRegex(TypeError, 'the context manager'):
248 test.enterContext(LacksExit())
249
250 self.assertEqual(test._cleanups, [])
251
252
253 @support.force_not_colorized_test_class
254 class TestClassCleanup(unittest.TestCase):
255 def test_addClassCleanUp(self):
256 class TestableTest(unittest.TestCase):
257 def testNothing(self):
258 pass
259 test = TestableTest('testNothing')
260 self.assertEqual(test._class_cleanups, [])
261 class_cleanups = []
262
263 def class_cleanup1(*args, **kwargs):
264 class_cleanups.append((3, args, kwargs))
265
266 def class_cleanup2(*args, **kwargs):
267 class_cleanups.append((4, args, kwargs))
268
269 TestableTest.addClassCleanup(class_cleanup1, 1, 2, 3,
270 four='hello', five='goodbye')
271 TestableTest.addClassCleanup(class_cleanup2)
272
273 self.assertEqual(test._class_cleanups,
274 [(class_cleanup1, (1, 2, 3),
275 dict(four='hello', five='goodbye')),
276 (class_cleanup2, (), {})])
277
278 TestableTest.doClassCleanups()
279 self.assertEqual(class_cleanups, [(4, (), {}), (3, (1, 2, 3),
280 dict(four='hello', five='goodbye'))])
281
282 def test_run_class_cleanUp(self):
283 ordering = []
284 blowUp = True
285
286 class TestableTest(unittest.TestCase):
287 @classmethod
288 def setUpClass(cls):
289 ordering.append('setUpClass')
290 cls.addClassCleanup(cleanup, ordering)
291 if blowUp:
292 raise CustomError()
293 def testNothing(self):
294 ordering.append('test')
295 @classmethod
296 def tearDownClass(cls):
297 ordering.append('tearDownClass')
298
299 runTests(TestableTest)
300 self.assertEqual(ordering, ['setUpClass', 'cleanup_good'])
301
302 ordering = []
303 blowUp = False
304 runTests(TestableTest)
305 self.assertEqual(ordering,
306 ['setUpClass', 'test', 'tearDownClass', 'cleanup_good'])
307
308 def test_run_class_cleanUp_without_tearDownClass(self):
309 ordering = []
310 blowUp = True
311
312 class TestableTest(unittest.TestCase):
313 @classmethod
314 def setUpClass(cls):
315 ordering.append('setUpClass')
316 cls.addClassCleanup(cleanup, ordering)
317 if blowUp:
318 raise CustomError()
319 def testNothing(self):
320 ordering.append('test')
321 @classmethod
322 @property
323 def tearDownClass(cls):
324 raise AttributeError
325
326 runTests(TestableTest)
327 self.assertEqual(ordering, ['setUpClass', 'cleanup_good'])
328
329 ordering = []
330 blowUp = False
331 runTests(TestableTest)
332 self.assertEqual(ordering,
333 ['setUpClass', 'test', 'cleanup_good'])
334
335 def test_debug_executes_classCleanUp(self):
336 ordering = []
337 blowUp = False
338
339 class TestableTest(unittest.TestCase):
340 @classmethod
341 def setUpClass(cls):
342 ordering.append('setUpClass')
343 cls.addClassCleanup(cleanup, ordering, blowUp=blowUp)
344 def testNothing(self):
345 ordering.append('test')
346 @classmethod
347 def tearDownClass(cls):
348 ordering.append('tearDownClass')
349
350 suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
351 suite.debug()
352 self.assertEqual(ordering,
353 ['setUpClass', 'test', 'tearDownClass', 'cleanup_good'])
354
355 ordering = []
356 blowUp = True
357 suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
358 with self.assertRaises(CustomError) as cm:
359 suite.debug()
360 self.assertEqual(str(cm.exception), 'CleanUpExc')
361 self.assertEqual(ordering,
362 ['setUpClass', 'test', 'tearDownClass', 'cleanup_exc'])
363
364 def test_debug_executes_classCleanUp_when_teardown_exception(self):
365 ordering = []
366 blowUp = False
367
368 class TestableTest(unittest.TestCase):
369 @classmethod
370 def setUpClass(cls):
371 ordering.append('setUpClass')
372 cls.addClassCleanup(cleanup, ordering, blowUp=blowUp)
373 def testNothing(self):
374 ordering.append('test')
375 @classmethod
376 def tearDownClass(cls):
377 ordering.append('tearDownClass')
378 raise CustomError('TearDownClassExc')
379
380 suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
381 with self.assertRaises(CustomError) as cm:
382 suite.debug()
383 self.assertEqual(str(cm.exception), 'TearDownClassExc')
384 self.assertEqual(ordering, ['setUpClass', 'test', 'tearDownClass'])
385 self.assertTrue(TestableTest._class_cleanups)
386 TestableTest._class_cleanups.clear()
387
388 ordering = []
389 blowUp = True
390 suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
391 with self.assertRaises(CustomError) as cm:
392 suite.debug()
393 self.assertEqual(str(cm.exception), 'TearDownClassExc')
394 self.assertEqual(ordering, ['setUpClass', 'test', 'tearDownClass'])
395 self.assertTrue(TestableTest._class_cleanups)
396 TestableTest._class_cleanups.clear()
397
398 def test_doClassCleanups_with_errors_addClassCleanUp(self):
399 class TestableTest(unittest.TestCase):
400 def testNothing(self):
401 pass
402
403 def cleanup1():
404 raise CustomError('cleanup1')
405
406 def cleanup2():
407 raise CustomError('cleanup2')
408
409 TestableTest.addClassCleanup(cleanup1)
410 TestableTest.addClassCleanup(cleanup2)
411 TestableTest.doClassCleanups()
412
413 self.assertEqual(len(TestableTest.tearDown_exceptions), 2)
414
415 e1, e2 = TestableTest.tearDown_exceptions
416 self.assertIsInstance(e1[1], CustomError)
417 self.assertEqual(str(e1[1]), 'cleanup2')
418 self.assertIsInstance(e2[1], CustomError)
419 self.assertEqual(str(e2[1]), 'cleanup1')
420
421 def test_with_errors_addCleanUp(self):
422 ordering = []
423 class TestableTest(unittest.TestCase):
424 @classmethod
425 def setUpClass(cls):
426 ordering.append('setUpClass')
427 cls.addClassCleanup(cleanup, ordering)
428 def setUp(self):
429 ordering.append('setUp')
430 self.addCleanup(cleanup, ordering, blowUp=True)
431 def testNothing(self):
432 pass
433 @classmethod
434 def tearDownClass(cls):
435 ordering.append('tearDownClass')
436
437 result = runTests(TestableTest)
438 self.assertEqual(result.errors[0][1].splitlines()[-1],
439 f'{CustomErrorRepr}: CleanUpExc')
440 self.assertEqual(ordering,
441 ['setUpClass', 'setUp', 'cleanup_exc',
442 'tearDownClass', 'cleanup_good'])
443
444 def test_run_with_errors_addClassCleanUp(self):
445 ordering = []
446 class TestableTest(unittest.TestCase):
447 @classmethod
448 def setUpClass(cls):
449 ordering.append('setUpClass')
450 cls.addClassCleanup(cleanup, ordering, blowUp=True)
451 def setUp(self):
452 ordering.append('setUp')
453 self.addCleanup(cleanup, ordering)
454 def testNothing(self):
455 ordering.append('test')
456 @classmethod
457 def tearDownClass(cls):
458 ordering.append('tearDownClass')
459
460 result = runTests(TestableTest)
461 self.assertEqual(result.errors[0][1].splitlines()[-1],
462 f'{CustomErrorRepr}: CleanUpExc')
463 self.assertEqual(ordering,
464 ['setUpClass', 'setUp', 'test', 'cleanup_good',
465 'tearDownClass', 'cleanup_exc'])
466
467 def test_with_errors_in_addClassCleanup_and_setUps(self):
468 ordering = []
469 class_blow_up = False
470 method_blow_up = False
471
472 class TestableTest(unittest.TestCase):
473 @classmethod
474 def setUpClass(cls):
475 ordering.append('setUpClass')
476 cls.addClassCleanup(cleanup, ordering, blowUp=True)
477 if class_blow_up:
478 raise CustomError('ClassExc')
479 def setUp(self):
480 ordering.append('setUp')
481 if method_blow_up:
482 raise CustomError('MethodExc')
483 def testNothing(self):
484 ordering.append('test')
485 @classmethod
486 def tearDownClass(cls):
487 ordering.append('tearDownClass')
488
489 result = runTests(TestableTest)
490 self.assertEqual(result.errors[0][1].splitlines()[-1],
491 f'{CustomErrorRepr}: CleanUpExc')
492 self.assertEqual(ordering,
493 ['setUpClass', 'setUp', 'test',
494 'tearDownClass', 'cleanup_exc'])
495
496 ordering = []
497 class_blow_up = True
498 method_blow_up = False
499 result = runTests(TestableTest)
500 self.assertEqual(result.errors[0][1].splitlines()[-1],
501 f'{CustomErrorRepr}: ClassExc')
502 self.assertEqual(result.errors[1][1].splitlines()[-1],
503 f'{CustomErrorRepr}: CleanUpExc')
504 self.assertEqual(ordering,
505 ['setUpClass', 'cleanup_exc'])
506
507 ordering = []
508 class_blow_up = False
509 method_blow_up = True
510 result = runTests(TestableTest)
511 self.assertEqual(result.errors[0][1].splitlines()[-1],
512 f'{CustomErrorRepr}: MethodExc')
513 self.assertEqual(result.errors[1][1].splitlines()[-1],
514 f'{CustomErrorRepr}: CleanUpExc')
515 self.assertEqual(ordering,
516 ['setUpClass', 'setUp', 'tearDownClass',
517 'cleanup_exc'])
518
519 def test_with_errors_in_tearDownClass(self):
520 ordering = []
521 class TestableTest(unittest.TestCase):
522 @classmethod
523 def setUpClass(cls):
524 ordering.append('setUpClass')
525 cls.addClassCleanup(cleanup, ordering)
526 def testNothing(self):
527 ordering.append('test')
528 @classmethod
529 def tearDownClass(cls):
530 ordering.append('tearDownClass')
531 raise CustomError('TearDownExc')
532
533 result = runTests(TestableTest)
534 self.assertEqual(result.errors[0][1].splitlines()[-1],
535 f'{CustomErrorRepr}: TearDownExc')
536 self.assertEqual(ordering,
537 ['setUpClass', 'test', 'tearDownClass', 'cleanup_good'])
538
539 def test_enterClassContext(self):
540 class TestableTest(unittest.TestCase):
541 def testNothing(self):
542 pass
543
544 cleanups = []
545
546 TestableTest.addClassCleanup(cleanups.append, 'cleanup1')
547 cm = TestCM(cleanups, 42)
548 self.assertEqual(TestableTest.enterClassContext(cm), 42)
549 TestableTest.addClassCleanup(cleanups.append, 'cleanup2')
550
551 TestableTest.doClassCleanups()
552 self.assertEqual(cleanups, ['enter', 'cleanup2', 'exit', 'cleanup1'])
553
554 def test_enterClassContext_arg_errors(self):
555 class TestableTest(unittest.TestCase):
556 def testNothing(self):
557 pass
558
559 with self.assertRaisesRegex(TypeError, 'the context manager'):
560 TestableTest.enterClassContext(LacksEnterAndExit())
561 with self.assertRaisesRegex(TypeError, 'the context manager'):
562 TestableTest.enterClassContext(LacksEnter())
563 with self.assertRaisesRegex(TypeError, 'the context manager'):
564 TestableTest.enterClassContext(LacksExit())
565
566 self.assertEqual(TestableTest._class_cleanups, [])
567
568 def test_run_nested_test(self):
569 ordering = []
570
571 class InnerTest(unittest.TestCase):
572 @classmethod
573 def setUpClass(cls):
574 ordering.append('inner setup')
575 cls.addClassCleanup(ordering.append, 'inner cleanup')
576 def test(self):
577 ordering.append('inner test')
578
579 class OuterTest(unittest.TestCase):
580 @classmethod
581 def setUpClass(cls):
582 ordering.append('outer setup')
583 cls.addClassCleanup(ordering.append, 'outer cleanup')
584 def test(self):
585 ordering.append('start outer test')
586 runTests(InnerTest)
587 ordering.append('end outer test')
588
589 runTests(OuterTest)
590 self.assertEqual(ordering, [
591 'outer setup', 'start outer test',
592 'inner setup', 'inner test', 'inner cleanup',
593 'end outer test', 'outer cleanup'])
594
595 def test_run_empty_suite_error_message(self):
596 class EmptyTest(unittest.TestCase):
597 pass
598
599 suite = unittest.defaultTestLoader.loadTestsFromTestCase(EmptyTest)
600 runner = getRunner()
601 runner.run(suite)
602
603 self.assertIn("\nNO TESTS RAN\n", runner.stream.getvalue())
604
605
606 @support.force_not_colorized_test_class
607 class TestModuleCleanUp(unittest.TestCase):
608 def test_add_and_do_ModuleCleanup(self):
609 module_cleanups = []
610
611 def module_cleanup1(*args, **kwargs):
612 module_cleanups.append((3, args, kwargs))
613
614 def module_cleanup2(*args, **kwargs):
615 module_cleanups.append((4, args, kwargs))
616
617 class Module(object):
618 unittest.addModuleCleanup(module_cleanup1, 1, 2, 3,
619 four='hello', five='goodbye')
620 unittest.addModuleCleanup(module_cleanup2)
621
622 self.assertEqual(unittest.case._module_cleanups,
623 [(module_cleanup1, (1, 2, 3),
624 dict(four='hello', five='goodbye')),
625 (module_cleanup2, (), {})])
626
627 unittest.case.doModuleCleanups()
628 self.assertEqual(module_cleanups, [(4, (), {}), (3, (1, 2, 3),
629 dict(four='hello', five='goodbye'))])
630 self.assertEqual(unittest.case._module_cleanups, [])
631
632 def test_doModuleCleanup_with_errors_in_addModuleCleanup(self):
633 module_cleanups = []
634
635 def module_cleanup_good(*args, **kwargs):
636 module_cleanups.append((3, args, kwargs))
637
638 def module_cleanup_bad(*args, **kwargs):
639 raise CustomError('CleanUpExc')
640
641 class Module(object):
642 unittest.addModuleCleanup(module_cleanup_good, 1, 2, 3,
643 four='hello', five='goodbye')
644 unittest.addModuleCleanup(module_cleanup_bad)
645 self.assertEqual(unittest.case._module_cleanups,
646 [(module_cleanup_good, (1, 2, 3),
647 dict(four='hello', five='goodbye')),
648 (module_cleanup_bad, (), {})])
649 with self.assertRaises(CustomError) as e:
650 unittest.case.doModuleCleanups()
651 self.assertEqual(str(e.exception), 'CleanUpExc')
652 self.assertEqual(unittest.case._module_cleanups, [])
653
654 def test_addModuleCleanup_arg_errors(self):
655 cleanups = []
656 def cleanup(*args, **kwargs):
657 cleanups.append((args, kwargs))
658
659 class Module(object):
660 unittest.addModuleCleanup(cleanup, 1, 2, function='hello')
661 with self.assertRaises(TypeError):
662 unittest.addModuleCleanup(function=cleanup, arg='hello')
663 with self.assertRaises(TypeError):
664 unittest.addModuleCleanup()
665 unittest.case.doModuleCleanups()
666 self.assertEqual(cleanups,
667 [((1, 2), {'function': 'hello'})])
668
669 def test_run_module_cleanUp(self):
670 blowUp = True
671 ordering = []
672 class Module(object):
673 @staticmethod
674 def setUpModule():
675 ordering.append('setUpModule')
676 unittest.addModuleCleanup(cleanup, ordering)
677 if blowUp:
678 raise CustomError('setUpModule Exc')
679 @staticmethod
680 def tearDownModule():
681 ordering.append('tearDownModule')
682
683 class TestableTest(unittest.TestCase):
684 @classmethod
685 def setUpClass(cls):
686 ordering.append('setUpClass')
687 def testNothing(self):
688 ordering.append('test')
689 @classmethod
690 def tearDownClass(cls):
691 ordering.append('tearDownClass')
692
693 TestableTest.__module__ = 'Module'
694 sys.modules['Module'] = Module
695 result = runTests(TestableTest)
696 self.assertEqual(ordering, ['setUpModule', 'cleanup_good'])
697 self.assertEqual(result.errors[0][1].splitlines()[-1],
698 f'{CustomErrorRepr}: setUpModule Exc')
699
700 ordering = []
701 blowUp = False
702 runTests(TestableTest)
703 self.assertEqual(ordering,
704 ['setUpModule', 'setUpClass', 'test', 'tearDownClass',
705 'tearDownModule', 'cleanup_good'])
706 self.assertEqual(unittest.case._module_cleanups, [])
707
708 def test_run_multiple_module_cleanUp(self):
709 blowUp = True
710 blowUp2 = False
711 ordering = []
712 class Module1(object):
713 @staticmethod
714 def setUpModule():
715 ordering.append('setUpModule')
716 unittest.addModuleCleanup(cleanup, ordering)
717 if blowUp:
718 raise CustomError()
719 @staticmethod
720 def tearDownModule():
721 ordering.append('tearDownModule')
722
723 class Module2(object):
724 @staticmethod
725 def setUpModule():
726 ordering.append('setUpModule2')
727 unittest.addModuleCleanup(cleanup, ordering)
728 if blowUp2:
729 raise CustomError()
730 @staticmethod
731 def tearDownModule():
732 ordering.append('tearDownModule2')
733
734 class TestableTest(unittest.TestCase):
735 @classmethod
736 def setUpClass(cls):
737 ordering.append('setUpClass')
738 def testNothing(self):
739 ordering.append('test')
740 @classmethod
741 def tearDownClass(cls):
742 ordering.append('tearDownClass')
743
744 class TestableTest2(unittest.TestCase):
745 @classmethod
746 def setUpClass(cls):
747 ordering.append('setUpClass2')
748 def testNothing(self):
749 ordering.append('test2')
750 @classmethod
751 def tearDownClass(cls):
752 ordering.append('tearDownClass2')
753
754 TestableTest.__module__ = 'Module1'
755 sys.modules['Module1'] = Module1
756 TestableTest2.__module__ = 'Module2'
757 sys.modules['Module2'] = Module2
758 runTests(TestableTest, TestableTest2)
759 self.assertEqual(ordering, ['setUpModule', 'cleanup_good',
760 'setUpModule2', 'setUpClass2', 'test2',
761 'tearDownClass2', 'tearDownModule2',
762 'cleanup_good'])
763 ordering = []
764 blowUp = False
765 blowUp2 = True
766 runTests(TestableTest, TestableTest2)
767 self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
768 'tearDownClass', 'tearDownModule',
769 'cleanup_good', 'setUpModule2',
770 'cleanup_good'])
771
772 ordering = []
773 blowUp = False
774 blowUp2 = False
775 runTests(TestableTest, TestableTest2)
776 self.assertEqual(ordering,
777 ['setUpModule', 'setUpClass', 'test', 'tearDownClass',
778 'tearDownModule', 'cleanup_good', 'setUpModule2',
779 'setUpClass2', 'test2', 'tearDownClass2',
780 'tearDownModule2', 'cleanup_good'])
781 self.assertEqual(unittest.case._module_cleanups, [])
782
783 def test_run_module_cleanUp_without_teardown(self):
784 ordering = []
785 class Module(object):
786 @staticmethod
787 def setUpModule():
788 ordering.append('setUpModule')
789 unittest.addModuleCleanup(cleanup, ordering)
790
791 class TestableTest(unittest.TestCase):
792 @classmethod
793 def setUpClass(cls):
794 ordering.append('setUpClass')
795 def testNothing(self):
796 ordering.append('test')
797 @classmethod
798 def tearDownClass(cls):
799 ordering.append('tearDownClass')
800
801 TestableTest.__module__ = 'Module'
802 sys.modules['Module'] = Module
803 runTests(TestableTest)
804 self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
805 'tearDownClass', 'cleanup_good'])
806 self.assertEqual(unittest.case._module_cleanups, [])
807
808 def test_run_module_cleanUp_when_teardown_exception(self):
809 ordering = []
810 class Module(object):
811 @staticmethod
812 def setUpModule():
813 ordering.append('setUpModule')
814 unittest.addModuleCleanup(cleanup, ordering)
815 @staticmethod
816 def tearDownModule():
817 ordering.append('tearDownModule')
818 raise CustomError('CleanUpExc')
819
820 class TestableTest(unittest.TestCase):
821 @classmethod
822 def setUpClass(cls):
823 ordering.append('setUpClass')
824 def testNothing(self):
825 ordering.append('test')
826 @classmethod
827 def tearDownClass(cls):
828 ordering.append('tearDownClass')
829
830 TestableTest.__module__ = 'Module'
831 sys.modules['Module'] = Module
832 result = runTests(TestableTest)
833 self.assertEqual(result.errors[0][1].splitlines()[-1],
834 f'{CustomErrorRepr}: CleanUpExc')
835 self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
836 'tearDownClass', 'tearDownModule',
837 'cleanup_good'])
838 self.assertEqual(unittest.case._module_cleanups, [])
839
840 def test_debug_module_executes_cleanUp(self):
841 ordering = []
842 blowUp = False
843 class Module(object):
844 @staticmethod
845 def setUpModule():
846 ordering.append('setUpModule')
847 unittest.addModuleCleanup(cleanup, ordering, blowUp=blowUp)
848 @staticmethod
849 def tearDownModule():
850 ordering.append('tearDownModule')
851
852 class TestableTest(unittest.TestCase):
853 @classmethod
854 def setUpClass(cls):
855 ordering.append('setUpClass')
856 def testNothing(self):
857 ordering.append('test')
858 @classmethod
859 def tearDownClass(cls):
860 ordering.append('tearDownClass')
861
862 TestableTest.__module__ = 'Module'
863 sys.modules['Module'] = Module
864 suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
865 suite.debug()
866 self.assertEqual(ordering,
867 ['setUpModule', 'setUpClass', 'test', 'tearDownClass',
868 'tearDownModule', 'cleanup_good'])
869 self.assertEqual(unittest.case._module_cleanups, [])
870
871 ordering = []
872 blowUp = True
873 suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
874 with self.assertRaises(CustomError) as cm:
875 suite.debug()
876 self.assertEqual(str(cm.exception), 'CleanUpExc')
877 self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
878 'tearDownClass', 'tearDownModule', 'cleanup_exc'])
879 self.assertEqual(unittest.case._module_cleanups, [])
880
881 def test_debug_module_cleanUp_when_teardown_exception(self):
882 ordering = []
883 blowUp = False
884 class Module(object):
885 @staticmethod
886 def setUpModule():
887 ordering.append('setUpModule')
888 unittest.addModuleCleanup(cleanup, ordering, blowUp=blowUp)
889 @staticmethod
890 def tearDownModule():
891 ordering.append('tearDownModule')
892 raise CustomError('TearDownModuleExc')
893
894 class TestableTest(unittest.TestCase):
895 @classmethod
896 def setUpClass(cls):
897 ordering.append('setUpClass')
898 def testNothing(self):
899 ordering.append('test')
900 @classmethod
901 def tearDownClass(cls):
902 ordering.append('tearDownClass')
903
904 TestableTest.__module__ = 'Module'
905 sys.modules['Module'] = Module
906 suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
907 with self.assertRaises(CustomError) as cm:
908 suite.debug()
909 self.assertEqual(str(cm.exception), 'TearDownModuleExc')
910 self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
911 'tearDownClass', 'tearDownModule'])
912 self.assertTrue(unittest.case._module_cleanups)
913 unittest.case._module_cleanups.clear()
914
915 ordering = []
916 blowUp = True
917 suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
918 with self.assertRaises(CustomError) as cm:
919 suite.debug()
920 self.assertEqual(str(cm.exception), 'TearDownModuleExc')
921 self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
922 'tearDownClass', 'tearDownModule'])
923 self.assertTrue(unittest.case._module_cleanups)
924 unittest.case._module_cleanups.clear()
925
926 def test_addClassCleanup_arg_errors(self):
927 cleanups = []
928 def cleanup(*args, **kwargs):
929 cleanups.append((args, kwargs))
930
931 class TestableTest(unittest.TestCase):
932 @classmethod
933 def setUpClass(cls):
934 cls.addClassCleanup(cleanup, 1, 2, function=3, cls=4)
935 with self.assertRaises(TypeError):
936 cls.addClassCleanup(function=cleanup, arg='hello')
937 def testNothing(self):
938 pass
939
940 with self.assertRaises(TypeError):
941 TestableTest.addClassCleanup()
942 with self.assertRaises(TypeError):
943 unittest.TestCase.addCleanup(cls=TestableTest(), function=cleanup)
944 runTests(TestableTest)
945 self.assertEqual(cleanups,
946 [((1, 2), {'function': 3, 'cls': 4})])
947
948 def test_addCleanup_arg_errors(self):
949 cleanups = []
950 def cleanup(*args, **kwargs):
951 cleanups.append((args, kwargs))
952
953 class TestableTest(unittest.TestCase):
954 def setUp(self2):
955 self2.addCleanup(cleanup, 1, 2, function=3, self=4)
956 with self.assertRaises(TypeError):
957 self2.addCleanup(function=cleanup, arg='hello')
958 def testNothing(self):
959 pass
960
961 with self.assertRaises(TypeError):
962 TestableTest().addCleanup()
963 with self.assertRaises(TypeError):
964 unittest.TestCase.addCleanup(self=TestableTest(), function=cleanup)
965 runTests(TestableTest)
966 self.assertEqual(cleanups,
967 [((1, 2), {'function': 3, 'self': 4})])
968
969 def test_with_errors_in_addClassCleanup(self):
970 ordering = []
971
972 class Module(object):
973 @staticmethod
974 def setUpModule():
975 ordering.append('setUpModule')
976 unittest.addModuleCleanup(cleanup, ordering)
977 @staticmethod
978 def tearDownModule():
979 ordering.append('tearDownModule')
980
981 class TestableTest(unittest.TestCase):
982 @classmethod
983 def setUpClass(cls):
984 ordering.append('setUpClass')
985 cls.addClassCleanup(cleanup, ordering, blowUp=True)
986 def testNothing(self):
987 ordering.append('test')
988 @classmethod
989 def tearDownClass(cls):
990 ordering.append('tearDownClass')
991
992 TestableTest.__module__ = 'Module'
993 sys.modules['Module'] = Module
994
995 result = runTests(TestableTest)
996 self.assertEqual(result.errors[0][1].splitlines()[-1],
997 f'{CustomErrorRepr}: CleanUpExc')
998 self.assertEqual(ordering,
999 ['setUpModule', 'setUpClass', 'test', 'tearDownClass',
1000 'cleanup_exc', 'tearDownModule', 'cleanup_good'])
1001
1002 def test_with_errors_in_addCleanup(self):
1003 ordering = []
1004 class Module(object):
1005 @staticmethod
1006 def setUpModule():
1007 ordering.append('setUpModule')
1008 unittest.addModuleCleanup(cleanup, ordering)
1009 @staticmethod
1010 def tearDownModule():
1011 ordering.append('tearDownModule')
1012
1013 class TestableTest(unittest.TestCase):
1014 def setUp(self):
1015 ordering.append('setUp')
1016 self.addCleanup(cleanup, ordering, blowUp=True)
1017 def testNothing(self):
1018 ordering.append('test')
1019 def tearDown(self):
1020 ordering.append('tearDown')
1021
1022 TestableTest.__module__ = 'Module'
1023 sys.modules['Module'] = Module
1024
1025 result = runTests(TestableTest)
1026 self.assertEqual(result.errors[0][1].splitlines()[-1],
1027 f'{CustomErrorRepr}: CleanUpExc')
1028 self.assertEqual(ordering,
1029 ['setUpModule', 'setUp', 'test', 'tearDown',
1030 'cleanup_exc', 'tearDownModule', 'cleanup_good'])
1031
1032 def test_with_errors_in_addModuleCleanup_and_setUps(self):
1033 ordering = []
1034 module_blow_up = False
1035 class_blow_up = False
1036 method_blow_up = False
1037 class Module(object):
1038 @staticmethod
1039 def setUpModule():
1040 ordering.append('setUpModule')
1041 unittest.addModuleCleanup(cleanup, ordering, blowUp=True)
1042 if module_blow_up:
1043 raise CustomError('ModuleExc')
1044 @staticmethod
1045 def tearDownModule():
1046 ordering.append('tearDownModule')
1047
1048 class TestableTest(unittest.TestCase):
1049 @classmethod
1050 def setUpClass(cls):
1051 ordering.append('setUpClass')
1052 if class_blow_up:
1053 raise CustomError('ClassExc')
1054 def setUp(self):
1055 ordering.append('setUp')
1056 if method_blow_up:
1057 raise CustomError('MethodExc')
1058 def testNothing(self):
1059 ordering.append('test')
1060 @classmethod
1061 def tearDownClass(cls):
1062 ordering.append('tearDownClass')
1063
1064 TestableTest.__module__ = 'Module'
1065 sys.modules['Module'] = Module
1066
1067 result = runTests(TestableTest)
1068 self.assertEqual(result.errors[0][1].splitlines()[-1],
1069 f'{CustomErrorRepr}: CleanUpExc')
1070 self.assertEqual(ordering,
1071 ['setUpModule', 'setUpClass', 'setUp', 'test',
1072 'tearDownClass', 'tearDownModule',
1073 'cleanup_exc'])
1074
1075 ordering = []
1076 module_blow_up = True
1077 class_blow_up = False
1078 method_blow_up = False
1079 result = runTests(TestableTest)
1080 self.assertEqual(result.errors[0][1].splitlines()[-1],
1081 f'{CustomErrorRepr}: ModuleExc')
1082 self.assertEqual(result.errors[1][1].splitlines()[-1],
1083 f'{CustomErrorRepr}: CleanUpExc')
1084 self.assertEqual(ordering, ['setUpModule', 'cleanup_exc'])
1085
1086 ordering = []
1087 module_blow_up = False
1088 class_blow_up = True
1089 method_blow_up = False
1090 result = runTests(TestableTest)
1091 self.assertEqual(result.errors[0][1].splitlines()[-1],
1092 f'{CustomErrorRepr}: ClassExc')
1093 self.assertEqual(result.errors[1][1].splitlines()[-1],
1094 f'{CustomErrorRepr}: CleanUpExc')
1095 self.assertEqual(ordering, ['setUpModule', 'setUpClass',
1096 'tearDownModule', 'cleanup_exc'])
1097
1098 ordering = []
1099 module_blow_up = False
1100 class_blow_up = False
1101 method_blow_up = True
1102 result = runTests(TestableTest)
1103 self.assertEqual(result.errors[0][1].splitlines()[-1],
1104 f'{CustomErrorRepr}: MethodExc')
1105 self.assertEqual(result.errors[1][1].splitlines()[-1],
1106 f'{CustomErrorRepr}: CleanUpExc')
1107 self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'setUp',
1108 'tearDownClass', 'tearDownModule',
1109 'cleanup_exc'])
1110
1111 def test_module_cleanUp_with_multiple_classes(self):
1112 ordering =[]
1113 def cleanup1():
1114 ordering.append('cleanup1')
1115
1116 def cleanup2():
1117 ordering.append('cleanup2')
1118
1119 def cleanup3():
1120 ordering.append('cleanup3')
1121
1122 class Module(object):
1123 @staticmethod
1124 def setUpModule():
1125 ordering.append('setUpModule')
1126 unittest.addModuleCleanup(cleanup1)
1127 @staticmethod
1128 def tearDownModule():
1129 ordering.append('tearDownModule')
1130
1131 class TestableTest(unittest.TestCase):
1132 def setUp(self):
1133 ordering.append('setUp')
1134 self.addCleanup(cleanup2)
1135 def testNothing(self):
1136 ordering.append('test')
1137 def tearDown(self):
1138 ordering.append('tearDown')
1139
1140 class OtherTestableTest(unittest.TestCase):
1141 def setUp(self):
1142 ordering.append('setUp2')
1143 self.addCleanup(cleanup3)
1144 def testNothing(self):
1145 ordering.append('test2')
1146 def tearDown(self):
1147 ordering.append('tearDown2')
1148
1149 TestableTest.__module__ = 'Module'
1150 OtherTestableTest.__module__ = 'Module'
1151 sys.modules['Module'] = Module
1152 runTests(TestableTest, OtherTestableTest)
1153 self.assertEqual(ordering,
1154 ['setUpModule', 'setUp', 'test', 'tearDown',
1155 'cleanup2', 'setUp2', 'test2', 'tearDown2',
1156 'cleanup3', 'tearDownModule', 'cleanup1'])
1157
1158 def test_enterModuleContext(self):
1159 cleanups = []
1160
1161 unittest.addModuleCleanup(cleanups.append, 'cleanup1')
1162 cm = TestCM(cleanups, 42)
1163 self.assertEqual(unittest.enterModuleContext(cm), 42)
1164 unittest.addModuleCleanup(cleanups.append, 'cleanup2')
1165
1166 unittest.case.doModuleCleanups()
1167 self.assertEqual(cleanups, ['enter', 'cleanup2', 'exit', 'cleanup1'])
1168
1169 def test_enterModuleContext_arg_errors(self):
1170 class TestableTest(unittest.TestCase):
1171 def testNothing(self):
1172 pass
1173
1174 with self.assertRaisesRegex(TypeError, 'the context manager'):
1175 unittest.enterModuleContext(LacksEnterAndExit())
1176 with self.assertRaisesRegex(TypeError, 'the context manager'):
1177 unittest.enterModuleContext(LacksEnter())
1178 with self.assertRaisesRegex(TypeError, 'the context manager'):
1179 unittest.enterModuleContext(LacksExit())
1180
1181 self.assertEqual(unittest.case._module_cleanups, [])
1182
1183
1184 class Test_TextTestRunner(unittest.TestCase):
1185 """Tests for TextTestRunner."""
1186
1187 def setUp(self):
1188 # clean the environment from pre-existing PYTHONWARNINGS to make
1189 # test_warnings results consistent
1190 self.pythonwarnings = os.environ.get('PYTHONWARNINGS')
1191 if self.pythonwarnings:
1192 del os.environ['PYTHONWARNINGS']
1193
1194 def tearDown(self):
1195 # bring back pre-existing PYTHONWARNINGS if present
1196 if self.pythonwarnings:
1197 os.environ['PYTHONWARNINGS'] = self.pythonwarnings
1198
1199 def test_init(self):
1200 runner = unittest.TextTestRunner()
1201 self.assertFalse(runner.failfast)
1202 self.assertFalse(runner.buffer)
1203 self.assertEqual(runner.verbosity, 1)
1204 self.assertEqual(runner.warnings, None)
1205 self.assertTrue(runner.descriptions)
1206 self.assertEqual(runner.resultclass, unittest.TextTestResult)
1207 self.assertFalse(runner.tb_locals)
1208 self.assertIsNone(runner.durations)
1209
1210 def test_multiple_inheritance(self):
1211 class AResult(unittest.TestResult):
1212 def __init__(self, stream, descriptions, verbosity):
1213 super(AResult, self).__init__(stream, descriptions, verbosity)
1214
1215 class ATextResult(unittest.TextTestResult, AResult):
1216 pass
1217
1218 # This used to raise an exception due to TextTestResult not passing
1219 # on arguments in its __init__ super call
1220 ATextResult(None, None, 1)
1221
1222 def testBufferAndFailfast(self):
1223 class Test(unittest.TestCase):
1224 def testFoo(self):
1225 pass
1226 result = unittest.TestResult()
1227 runner = unittest.TextTestRunner(stream=io.StringIO(), failfast=True,
1228 buffer=True)
1229 # Use our result object
1230 runner._makeResult = lambda: result
1231 runner.run(Test('testFoo'))
1232
1233 self.assertTrue(result.failfast)
1234 self.assertTrue(result.buffer)
1235
1236 def test_locals(self):
1237 runner = unittest.TextTestRunner(stream=io.StringIO(), tb_locals=True)
1238 result = runner.run(unittest.TestSuite())
1239 self.assertEqual(True, result.tb_locals)
1240
1241 def testRunnerRegistersResult(self):
1242 class Test(unittest.TestCase):
1243 def testFoo(self):
1244 pass
1245 originalRegisterResult = unittest.runner.registerResult
1246 def cleanup():
1247 unittest.runner.registerResult = originalRegisterResult
1248 self.addCleanup(cleanup)
1249
1250 result = unittest.TestResult()
1251 runner = unittest.TextTestRunner(stream=io.StringIO())
1252 # Use our result object
1253 runner._makeResult = lambda: result
1254
1255 self.wasRegistered = 0
1256 def fakeRegisterResult(thisResult):
1257 self.wasRegistered += 1
1258 self.assertEqual(thisResult, result)
1259 unittest.runner.registerResult = fakeRegisterResult
1260
1261 runner.run(unittest.TestSuite())
1262 self.assertEqual(self.wasRegistered, 1)
1263
1264 def test_works_with_result_without_startTestRun_stopTestRun(self):
1265 class OldTextResult(ResultWithNoStartTestRunStopTestRun):
1266 separator2 = ''
1267 def printErrors(self):
1268 pass
1269
1270 class Runner(unittest.TextTestRunner):
1271 def __init__(self):
1272 super(Runner, self).__init__(io.StringIO())
1273
1274 def _makeResult(self):
1275 return OldTextResult()
1276
1277 runner = Runner()
1278 runner.run(unittest.TestSuite())
1279
1280 def test_startTestRun_stopTestRun_called(self):
1281 class LoggingTextResult(LoggingResult):
1282 separator2 = ''
1283 def printErrors(self):
1284 pass
1285
1286 class LoggingRunner(unittest.TextTestRunner):
1287 def __init__(self, events):
1288 super(LoggingRunner, self).__init__(io.StringIO())
1289 self._events = events
1290
1291 def _makeResult(self):
1292 return LoggingTextResult(self._events)
1293
1294 events = []
1295 runner = LoggingRunner(events)
1296 runner.run(unittest.TestSuite())
1297 expected = ['startTestRun', 'stopTestRun']
1298 self.assertEqual(events, expected)
1299
1300 def test_pickle_unpickle(self):
1301 # Issue #7197: a TextTestRunner should be (un)pickleable. This is
1302 # required by test_multiprocessing under Windows (in verbose mode).
1303 stream = io.StringIO("foo")
1304 runner = unittest.TextTestRunner(stream)
1305 for protocol in range(2, pickle.HIGHEST_PROTOCOL + 1):
1306 s = pickle.dumps(runner, protocol)
1307 obj = pickle.loads(s)
1308 # StringIO objects never compare equal, a cheap test instead.
1309 self.assertEqual(obj.stream.getvalue(), stream.getvalue())
1310
1311 def test_resultclass(self):
1312 def MockResultClass(*args):
1313 return args
1314 STREAM = object()
1315 DESCRIPTIONS = object()
1316 VERBOSITY = object()
1317 runner = unittest.TextTestRunner(STREAM, DESCRIPTIONS, VERBOSITY,
1318 resultclass=MockResultClass)
1319 self.assertEqual(runner.resultclass, MockResultClass)
1320
1321 expectedresult = (runner.stream, DESCRIPTIONS, VERBOSITY)
1322 self.assertEqual(runner._makeResult(), expectedresult)
1323
1324 @support.force_not_colorized
1325 @support.requires_subprocess()
1326 def test_warnings(self):
1327 """
1328 Check that warnings argument of TextTestRunner correctly affects the
1329 behavior of the warnings.
1330 """
1331 # see #10535 and the _test_warnings file for more information
1332
1333 def get_parse_out_err(p):
1334 return [b.splitlines() for b in p.communicate()]
1335 opts = dict(stdout=subprocess.PIPE, stderr=subprocess.PIPE,
1336 cwd=os.path.dirname(__file__))
1337
1338 # no args -> all the warnings are printed, unittest warnings only once
1339 p = subprocess.Popen([sys.executable, '-E', '_test_warnings.py'], **opts)
1340 with p:
1341 out, err = get_parse_out_err(p)
1342 self.assertIn(b'OK', err)
1343 # check that the total number of warnings in the output is correct
1344 self.assertEqual(len(out), 10)
1345 # check that the numbers of the different kind of warnings is correct
1346 for msg in [b'dw', b'iw', b'uw']:
1347 self.assertEqual(out.count(msg), 3)
1348 for msg in [b'rw']:
1349 self.assertEqual(out.count(msg), 1)
1350
1351 args_list = (
1352 # passing 'ignore' as warnings arg -> no warnings
1353 [sys.executable, '_test_warnings.py', 'ignore'],
1354 # -W doesn't affect the result if the arg is passed
1355 [sys.executable, '-Wa', '_test_warnings.py', 'ignore'],
1356 # -W affects the result if the arg is not passed
1357 [sys.executable, '-Wi', '_test_warnings.py']
1358 )
1359 # in all these cases no warnings are printed
1360 for args in args_list:
1361 p = subprocess.Popen(args, **opts)
1362 with p:
1363 out, err = get_parse_out_err(p)
1364 self.assertIn(b'OK', err)
1365 self.assertEqual(len(out), 0)
1366
1367
1368 # passing 'always' as warnings arg -> all the warnings printed,
1369 # unittest warnings only once
1370 p = subprocess.Popen([sys.executable, '_test_warnings.py', 'always'],
1371 **opts)
1372 with p:
1373 out, err = get_parse_out_err(p)
1374 self.assertIn(b'OK', err)
1375 self.assertEqual(len(out), 12)
1376 for msg in [b'dw', b'iw', b'uw', b'rw']:
1377 self.assertEqual(out.count(msg), 3)
1378
1379 def testStdErrLookedUpAtInstantiationTime(self):
1380 # see issue 10786
1381 old_stderr = sys.stderr
1382 f = io.StringIO()
1383 sys.stderr = f
1384 try:
1385 runner = unittest.TextTestRunner()
1386 self.assertTrue(runner.stream.stream is f)
1387 finally:
1388 sys.stderr = old_stderr
1389
1390 def testSpecifiedStreamUsed(self):
1391 # see issue 10786
1392 f = io.StringIO()
1393 runner = unittest.TextTestRunner(f)
1394 self.assertTrue(runner.stream.stream is f)
1395
1396 def test_durations(self):
1397 def run(test, *, expect_durations=True):
1398 stream = BufferedWriter()
1399 runner = unittest.TextTestRunner(stream=stream, durations=5, verbosity=2)
1400 result = runner.run(test)
1401 self.assertEqual(result.durations, 5)
1402 stream.flush()
1403 text = stream.getvalue()
1404 regex = r"\n\d+.\d\d\ds"
1405 if expect_durations:
1406 self.assertEqual(len(result.collectedDurations), 1)
1407 self.assertIn('Slowest test durations', text)
1408 self.assertRegex(text, regex)
1409 else:
1410 self.assertEqual(len(result.collectedDurations), 0)
1411 self.assertNotIn('Slowest test durations', text)
1412 self.assertNotRegex(text, regex)
1413
1414 # success
1415 class Foo(unittest.TestCase):
1416 def test_1(self):
1417 pass
1418
1419 run(Foo('test_1'), expect_durations=True)
1420
1421 # failure
1422 class Foo(unittest.TestCase):
1423 def test_1(self):
1424 self.assertEqual(0, 1)
1425
1426 run(Foo('test_1'), expect_durations=True)
1427
1428 # error
1429 class Foo(unittest.TestCase):
1430 def test_1(self):
1431 1 / 0
1432
1433 run(Foo('test_1'), expect_durations=True)
1434
1435
1436 # error in setUp and tearDown
1437 class Foo(unittest.TestCase):
1438 def setUp(self):
1439 1 / 0
1440 tearDown = setUp
1441 def test_1(self):
1442 pass
1443
1444 run(Foo('test_1'), expect_durations=True)
1445
1446 # skip (expect no durations)
1447 class Foo(unittest.TestCase):
1448 @unittest.skip("reason")
1449 def test_1(self):
1450 pass
1451
1452 run(Foo('test_1'), expect_durations=False)
1453
1454
1455
1456 if __name__ == "__main__":
1457 unittest.main()