Debian Codegraph @

Lib/test/test_unittest/test_case.py

raw
1 import contextlib
2 import difflib
3 import pprint
4 import pickle
5 import re
6 import sys
7 import logging
8 import warnings
9 import weakref
10 import inspect
11 import types
12
13 from collections import UserString
14 from copy import deepcopy
15 from test import support
16
17 import unittest
18
19 from test.test_unittest.support import (
20 TestEquality, TestHashing, LoggingResult, LegacyLoggingResult,
21 ResultWithNoStartTestRunStopTestRun
22 )
23 from test.support import captured_stderr, gc_collect
24
25
26 log_foo = logging.getLogger('foo')
27 log_foobar = logging.getLogger('foo.bar')
28 log_quux = logging.getLogger('quux')
29
30
31 class Test(object):
32 "Keep these TestCase classes out of the main namespace"
33
34 class Foo(unittest.TestCase):
35 def runTest(self): pass
36 def test1(self): pass
37
38 class Bar(Foo):
39 def test2(self): pass
40
41 class LoggingTestCase(unittest.TestCase):
42 """A test case which logs its calls."""
43
44 def __init__(self, events):
45 super(Test.LoggingTestCase, self).__init__('test')
46 self.events = events
47
48 def setUp(self):
49 self.events.append('setUp')
50
51 def test(self):
52 self.events.append('test')
53
54 def tearDown(self):
55 self.events.append('tearDown')
56
57
58 class List(list):
59 pass
60
61
62 class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
63
64 ### Set up attributes used by inherited tests
65 ################################################################
66
67 # Used by TestHashing.test_hash and TestEquality.test_eq
68 eq_pairs = [(Test.Foo('test1'), Test.Foo('test1'))]
69
70 # Used by TestEquality.test_ne
71 ne_pairs = [(Test.Foo('test1'), Test.Foo('runTest')),
72 (Test.Foo('test1'), Test.Bar('test1')),
73 (Test.Foo('test1'), Test.Bar('test2'))]
74
75 ################################################################
76 ### /Set up attributes used by inherited tests
77
78
79 # "class TestCase([methodName])"
80 # ...
81 # "Each instance of TestCase will run a single test method: the
82 # method named methodName."
83 # ...
84 # "methodName defaults to "runTest"."
85 #
86 # Make sure it really is optional, and that it defaults to the proper
87 # thing.
88 def test_init__no_test_name(self):
89 class Test(unittest.TestCase):
90 def runTest(self): raise MyException()
91 def test(self): pass
92
93 self.assertEndsWith(Test().id(), '.Test.runTest')
94
95 # test that TestCase can be instantiated with no args
96 # primarily for use at the interactive interpreter
97 test = unittest.TestCase()
98 test.assertEqual(3, 3)
99 with test.assertRaises(test.failureException):
100 test.assertEqual(3, 2)
101
102 with self.assertRaises(AttributeError):
103 test.run()
104
105 # "class TestCase([methodName])"
106 # ...
107 # "Each instance of TestCase will run a single test method: the
108 # method named methodName."
109 def test_init__test_name__valid(self):
110 class Test(unittest.TestCase):
111 def runTest(self): raise MyException()
112 def test(self): pass
113
114 self.assertEndsWith(Test('test').id(), '.Test.test')
115
116 # "class TestCase([methodName])"
117 # ...
118 # "Each instance of TestCase will run a single test method: the
119 # method named methodName."
120 def test_init__test_name__invalid(self):
121 class Test(unittest.TestCase):
122 def runTest(self): raise MyException()
123 def test(self): pass
124
125 try:
126 Test('testfoo')
127 except ValueError:
128 pass
129 else:
130 self.fail("Failed to raise ValueError")
131
132 # "Return the number of tests represented by the this test object. For
133 # TestCase instances, this will always be 1"
134 def test_countTestCases(self):
135 class Foo(unittest.TestCase):
136 def test(self): pass
137
138 self.assertEqual(Foo('test').countTestCases(), 1)
139
140 # "Return the default type of test result object to be used to run this
141 # test. For TestCase instances, this will always be
142 # unittest.TestResult; subclasses of TestCase should
143 # override this as necessary."
144 def test_defaultTestResult(self):
145 class Foo(unittest.TestCase):
146 def runTest(self):
147 pass
148
149 result = Foo().defaultTestResult()
150 self.assertEqual(type(result), unittest.TestResult)
151
152 # "When a setUp() method is defined, the test runner will run that method
153 # prior to each test. Likewise, if a tearDown() method is defined, the
154 # test runner will invoke that method after each test. In the example,
155 # setUp() was used to create a fresh sequence for each test."
156 #
157 # Make sure the proper call order is maintained, even if setUp() raises
158 # an exception.
159 def test_run_call_order__error_in_setUp(self):
160 events = []
161 result = LoggingResult(events)
162
163 class Foo(Test.LoggingTestCase):
164 def setUp(self):
165 super(Foo, self).setUp()
166 raise RuntimeError('raised by Foo.setUp')
167
168 Foo(events).run(result)
169 expected = ['startTest', 'setUp', 'addError', 'stopTest']
170 self.assertEqual(events, expected)
171
172 # "With a temporary result stopTestRun is called when setUp errors.
173 def test_run_call_order__error_in_setUp_default_result(self):
174 events = []
175
176 class Foo(Test.LoggingTestCase):
177 def defaultTestResult(self):
178 return LoggingResult(self.events)
179
180 def setUp(self):
181 super(Foo, self).setUp()
182 raise RuntimeError('raised by Foo.setUp')
183
184 Foo(events).run()
185 expected = ['startTestRun', 'startTest', 'setUp', 'addError',
186 'stopTest', 'stopTestRun']
187 self.assertEqual(events, expected)
188
189 # "When a setUp() method is defined, the test runner will run that method
190 # prior to each test. Likewise, if a tearDown() method is defined, the
191 # test runner will invoke that method after each test. In the example,
192 # setUp() was used to create a fresh sequence for each test."
193 #
194 # Make sure the proper call order is maintained, even if the test raises
195 # an error (as opposed to a failure).
196 def test_run_call_order__error_in_test(self):
197 events = []
198 result = LoggingResult(events)
199
200 class Foo(Test.LoggingTestCase):
201 def test(self):
202 super(Foo, self).test()
203 raise RuntimeError('raised by Foo.test')
204
205 expected = ['startTest', 'setUp', 'test',
206 'addError', 'tearDown', 'stopTest']
207 Foo(events).run(result)
208 self.assertEqual(events, expected)
209
210 # "With a default result, an error in the test still results in stopTestRun
211 # being called."
212 def test_run_call_order__error_in_test_default_result(self):
213 events = []
214
215 class Foo(Test.LoggingTestCase):
216 def defaultTestResult(self):
217 return LoggingResult(self.events)
218
219 def test(self):
220 super(Foo, self).test()
221 raise RuntimeError('raised by Foo.test')
222
223 expected = ['startTestRun', 'startTest', 'setUp', 'test',
224 'addError', 'tearDown', 'stopTest', 'stopTestRun']
225 Foo(events).run()
226 self.assertEqual(events, expected)
227
228 # "When a setUp() method is defined, the test runner will run that method
229 # prior to each test. Likewise, if a tearDown() method is defined, the
230 # test runner will invoke that method after each test. In the example,
231 # setUp() was used to create a fresh sequence for each test."
232 #
233 # Make sure the proper call order is maintained, even if the test signals
234 # a failure (as opposed to an error).
235 def test_run_call_order__failure_in_test(self):
236 events = []
237 result = LoggingResult(events)
238
239 class Foo(Test.LoggingTestCase):
240 def test(self):
241 super(Foo, self).test()
242 self.fail('raised by Foo.test')
243
244 expected = ['startTest', 'setUp', 'test',
245 'addFailure', 'tearDown', 'stopTest']
246 Foo(events).run(result)
247 self.assertEqual(events, expected)
248
249 # "When a test fails with a default result stopTestRun is still called."
250 def test_run_call_order__failure_in_test_default_result(self):
251
252 class Foo(Test.LoggingTestCase):
253 def defaultTestResult(self):
254 return LoggingResult(self.events)
255 def test(self):
256 super(Foo, self).test()
257 self.fail('raised by Foo.test')
258
259 expected = ['startTestRun', 'startTest', 'setUp', 'test',
260 'addFailure', 'tearDown', 'stopTest', 'stopTestRun']
261 events = []
262 Foo(events).run()
263 self.assertEqual(events, expected)
264
265 # "When a setUp() method is defined, the test runner will run that method
266 # prior to each test. Likewise, if a tearDown() method is defined, the
267 # test runner will invoke that method after each test. In the example,
268 # setUp() was used to create a fresh sequence for each test."
269 #
270 # Make sure the proper call order is maintained, even if tearDown() raises
271 # an exception.
272 def test_run_call_order__error_in_tearDown(self):
273 events = []
274 result = LoggingResult(events)
275
276 class Foo(Test.LoggingTestCase):
277 def tearDown(self):
278 super(Foo, self).tearDown()
279 raise RuntimeError('raised by Foo.tearDown')
280
281 Foo(events).run(result)
282 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError',
283 'stopTest']
284 self.assertEqual(events, expected)
285
286 # "When tearDown errors with a default result stopTestRun is still called."
287 def test_run_call_order__error_in_tearDown_default_result(self):
288
289 class Foo(Test.LoggingTestCase):
290 def defaultTestResult(self):
291 return LoggingResult(self.events)
292 def tearDown(self):
293 super(Foo, self).tearDown()
294 raise RuntimeError('raised by Foo.tearDown')
295
296 events = []
297 Foo(events).run()
298 expected = ['startTestRun', 'startTest', 'setUp', 'test', 'tearDown',
299 'addError', 'stopTest', 'stopTestRun']
300 self.assertEqual(events, expected)
301
302 # "TestCase.run() still works when the defaultTestResult is a TestResult
303 # that does not support startTestRun and stopTestRun.
304 def test_run_call_order_default_result(self):
305
306 class Foo(unittest.TestCase):
307 def defaultTestResult(self):
308 return ResultWithNoStartTestRunStopTestRun()
309 def test(self):
310 pass
311
312 with self.assertWarns(RuntimeWarning):
313 Foo('test').run()
314
315 def test_deprecation_of_return_val_from_test(self):
316 # Issue 41322 - deprecate return of value that is not None from a test
317 class Nothing:
318 def __eq__(self, o):
319 return o is None
320 class Foo(unittest.TestCase):
321 def test1(self):
322 return 1
323 def test2(self):
324 yield 1
325 def test3(self):
326 return Nothing()
327
328 with self.assertWarns(DeprecationWarning) as w:
329 Foo('test1').run()
330 self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
331 self.assertIn('test1', str(w.warning))
332 self.assertEqual(w.filename, __file__)
333 self.assertIn("returned 'int'", str(w.warning))
334
335 with self.assertWarns(DeprecationWarning) as w:
336 Foo('test2').run()
337 self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
338 self.assertIn('test2', str(w.warning))
339 self.assertEqual(w.filename, __file__)
340 self.assertIn("returned 'generator'", str(w.warning))
341
342 with self.assertWarns(DeprecationWarning) as w:
343 Foo('test3').run()
344 self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
345 self.assertIn('test3', str(w.warning))
346 self.assertEqual(w.filename, __file__)
347 self.assertIn(f'returned {Nothing.__name__!r}', str(w.warning))
348
349 def test_deprecation_of_return_val_from_test_async_method(self):
350 class Foo(unittest.TestCase):
351 async def test1(self):
352 return 1
353
354 with self.assertWarns(DeprecationWarning) as w:
355 warnings.filterwarnings('ignore',
356 'coroutine .* was never awaited', RuntimeWarning)
357 Foo('test1').run()
358 support.gc_collect()
359 self.assertIn('It is deprecated to return a value that is not None', str(w.warning))
360 self.assertIn('test1', str(w.warning))
361 self.assertEqual(w.filename, __file__)
362 self.assertIn("returned 'coroutine'", str(w.warning))
363 self.assertIn(
364 'Maybe you forgot to use IsolatedAsyncioTestCase as the base class?',
365 str(w.warning),
366 )
367
368 def _check_call_order__subtests(self, result, events, expected_events):
369 class Foo(Test.LoggingTestCase):
370 def test(self):
371 super(Foo, self).test()
372 for i in [1, 2, 3]:
373 with self.subTest(i=i):
374 if i == 1:
375 self.fail('failure')
376 for j in [2, 3]:
377 with self.subTest(j=j):
378 if i * j == 6:
379 raise RuntimeError('raised by Foo.test')
380 1 / 0
381
382 # Order is the following:
383 # i=1 => subtest failure
384 # i=2, j=2 => subtest success
385 # i=2, j=3 => subtest error
386 # i=3, j=2 => subtest error
387 # i=3, j=3 => subtest success
388 # toplevel => error
389 Foo(events).run(result)
390 self.assertEqual(events, expected_events)
391
392 def test_run_call_order__subtests(self):
393 events = []
394 result = LoggingResult(events)
395 expected = ['startTest', 'setUp', 'test',
396 'addSubTestFailure', 'addSubTestSuccess',
397 'addSubTestFailure', 'addSubTestFailure',
398 'addSubTestSuccess', 'addError', 'tearDown', 'stopTest']
399 self._check_call_order__subtests(result, events, expected)
400
401 def test_run_call_order__subtests_legacy(self):
402 # With a legacy result object (without an addSubTest method),
403 # text execution stops after the first subtest failure.
404 events = []
405 result = LegacyLoggingResult(events)
406 expected = ['startTest', 'setUp', 'test',
407 'addFailure', 'tearDown', 'stopTest']
408 self._check_call_order__subtests(result, events, expected)
409
410 def _check_call_order__subtests_success(self, result, events, expected_events):
411 class Foo(Test.LoggingTestCase):
412 def test(self):
413 super(Foo, self).test()
414 for i in [1, 2]:
415 with self.subTest(i=i):
416 for j in [2, 3]:
417 with self.subTest(j=j):
418 pass
419
420 Foo(events).run(result)
421 self.assertEqual(events, expected_events)
422
423 def test_run_call_order__subtests_success(self):
424 events = []
425 result = LoggingResult(events)
426 # The 6 subtest successes are individually recorded, in addition
427 # to the whole test success.
428 expected = (['startTest', 'setUp', 'test']
429 + 6 * ['addSubTestSuccess']
430 + ['tearDown', 'addSuccess', 'stopTest'])
431 self._check_call_order__subtests_success(result, events, expected)
432
433 def test_run_call_order__subtests_success_legacy(self):
434 # With a legacy result, only the whole test success is recorded.
435 events = []
436 result = LegacyLoggingResult(events)
437 expected = ['startTest', 'setUp', 'test', 'tearDown',
438 'addSuccess', 'stopTest']
439 self._check_call_order__subtests_success(result, events, expected)
440
441 def test_run_call_order__subtests_failfast(self):
442 events = []
443 result = LoggingResult(events)
444 result.failfast = True
445
446 class Foo(Test.LoggingTestCase):
447 def test(self):
448 super(Foo, self).test()
449 with self.subTest(i=1):
450 self.fail('failure')
451 with self.subTest(i=2):
452 self.fail('failure')
453 self.fail('failure')
454
455 expected = ['startTest', 'setUp', 'test',
456 'addSubTestFailure', 'tearDown', 'stopTest']
457 Foo(events).run(result)
458 self.assertEqual(events, expected)
459
460 def test_subtests_failfast(self):
461 # Ensure proper test flow with subtests and failfast (issue #22894)
462 events = []
463
464 class Foo(unittest.TestCase):
465 def test_a(self):
466 with self.subTest():
467 events.append('a1')
468 events.append('a2')
469
470 def test_b(self):
471 with self.subTest():
472 events.append('b1')
473 with self.subTest():
474 self.fail('failure')
475 events.append('b2')
476
477 def test_c(self):
478 events.append('c')
479
480 result = unittest.TestResult()
481 result.failfast = True
482 suite = unittest.TestLoader().loadTestsFromTestCase(Foo)
483 suite.run(result)
484
485 expected = ['a1', 'a2', 'b1']
486 self.assertEqual(events, expected)
487
488 def test_subtests_debug(self):
489 # Test debug() with a test that uses subTest() (bpo-34900)
490 events = []
491
492 class Foo(unittest.TestCase):
493 def test_a(self):
494 events.append('test case')
495 with self.subTest():
496 events.append('subtest 1')
497
498 Foo('test_a').debug()
499
500 self.assertEqual(events, ['test case', 'subtest 1'])
501
502 # "This class attribute gives the exception raised by the test() method.
503 # If a test framework needs to use a specialized exception, possibly to
504 # carry additional information, it must subclass this exception in
505 # order to ``play fair'' with the framework. The initial value of this
506 # attribute is AssertionError"
507 def test_failureException__default(self):
508 class Foo(unittest.TestCase):
509 def test(self):
510 pass
511
512 self.assertIs(Foo('test').failureException, AssertionError)
513
514 # "This class attribute gives the exception raised by the test() method.
515 # If a test framework needs to use a specialized exception, possibly to
516 # carry additional information, it must subclass this exception in
517 # order to ``play fair'' with the framework."
518 #
519 # Make sure TestCase.run() respects the designated failureException
520 def test_failureException__subclassing__explicit_raise(self):
521 events = []
522 result = LoggingResult(events)
523
524 class Foo(unittest.TestCase):
525 def test(self):
526 raise RuntimeError()
527
528 failureException = RuntimeError
529
530 self.assertIs(Foo('test').failureException, RuntimeError)
531
532
533 Foo('test').run(result)
534 expected = ['startTest', 'addFailure', 'stopTest']
535 self.assertEqual(events, expected)
536
537 # "This class attribute gives the exception raised by the test() method.
538 # If a test framework needs to use a specialized exception, possibly to
539 # carry additional information, it must subclass this exception in
540 # order to ``play fair'' with the framework."
541 #
542 # Make sure TestCase.run() respects the designated failureException
543 def test_failureException__subclassing__implicit_raise(self):
544 events = []
545 result = LoggingResult(events)
546
547 class Foo(unittest.TestCase):
548 def test(self):
549 self.fail("foo")
550
551 failureException = RuntimeError
552
553 self.assertIs(Foo('test').failureException, RuntimeError)
554
555
556 Foo('test').run(result)
557 expected = ['startTest', 'addFailure', 'stopTest']
558 self.assertEqual(events, expected)
559
560 # "The default implementation does nothing."
561 def test_setUp(self):
562 class Foo(unittest.TestCase):
563 def runTest(self):
564 pass
565
566 # ... and nothing should happen
567 Foo().setUp()
568
569 # "The default implementation does nothing."
570 def test_tearDown(self):
571 class Foo(unittest.TestCase):
572 def runTest(self):
573 pass
574
575 # ... and nothing should happen
576 Foo().tearDown()
577
578 # "Return a string identifying the specific test case."
579 #
580 # Because of the vague nature of the docs, I'm not going to lock this
581 # test down too much. Really all that can be asserted is that the id()
582 # will be a string (either 8-byte or unicode -- again, because the docs
583 # just say "string")
584 def test_id(self):
585 class Foo(unittest.TestCase):
586 def runTest(self):
587 pass
588
589 self.assertIsInstance(Foo().id(), str)
590
591
592 # "If result is omitted or None, a temporary result object is created,
593 # used, and is made available to the caller. As TestCase owns the
594 # temporary result startTestRun and stopTestRun are called.
595
596 def test_run__uses_defaultTestResult(self):
597 events = []
598 defaultResult = LoggingResult(events)
599
600 class Foo(unittest.TestCase):
601 def test(self):
602 events.append('test')
603
604 def defaultTestResult(self):
605 return defaultResult
606
607 # Make run() find a result object on its own
608 result = Foo('test').run()
609
610 self.assertIs(result, defaultResult)
611 expected = ['startTestRun', 'startTest', 'test', 'addSuccess',
612 'stopTest', 'stopTestRun']
613 self.assertEqual(events, expected)
614
615
616 # "The result object is returned to run's caller"
617 def test_run__returns_given_result(self):
618
619 class Foo(unittest.TestCase):
620 def test(self):
621 pass
622
623 result = unittest.TestResult()
624
625 retval = Foo('test').run(result)
626 self.assertIs(retval, result)
627
628
629 # "The same effect [as method run] may be had by simply calling the
630 # TestCase instance."
631 def test_call__invoking_an_instance_delegates_to_run(self):
632 resultIn = unittest.TestResult()
633 resultOut = unittest.TestResult()
634
635 class Foo(unittest.TestCase):
636 def test(self):
637 pass
638
639 def run(self, result):
640 self.assertIs(result, resultIn)
641 return resultOut
642
643 retval = Foo('test')(resultIn)
644
645 self.assertIs(retval, resultOut)
646
647
648 def testShortDescriptionWithoutDocstring(self):
649 self.assertIsNone(self.shortDescription())
650
651 @unittest.skipIf(sys.flags.optimize >= 2,
652 "Docstrings are omitted with -O2 and above")
653 def testShortDescriptionWithOneLineDocstring(self):
654 """Tests shortDescription() for a method with a docstring."""
655 self.assertEqual(
656 self.shortDescription(),
657 'Tests shortDescription() for a method with a docstring.')
658
659 @unittest.skipIf(sys.flags.optimize >= 2,
660 "Docstrings are omitted with -O2 and above")
661 def testShortDescriptionWithMultiLineDocstring(self):
662 """Tests shortDescription() for a method with a longer docstring.
663
664 This method ensures that only the first line of a docstring is
665 returned used in the short description, no matter how long the
666 whole thing is.
667 """
668 self.assertEqual(
669 self.shortDescription(),
670 'Tests shortDescription() for a method with a longer '
671 'docstring.')
672
673 @unittest.skipIf(sys.flags.optimize >= 2,
674 "Docstrings are omitted with -O2 and above")
675 def testShortDescriptionWhitespaceTrimming(self):
676 """
677 Tests shortDescription() whitespace is trimmed, so that the first
678 line of nonwhite-space text becomes the docstring.
679 """
680 self.assertEqual(
681 self.shortDescription(),
682 'Tests shortDescription() whitespace is trimmed, so that the first')
683
684 def testAddTypeEqualityFunc(self):
685 class SadSnake(object):
686 """Dummy class for test_addTypeEqualityFunc."""
687 s1, s2 = SadSnake(), SadSnake()
688 self.assertFalse(s1 == s2)
689 def AllSnakesCreatedEqual(a, b, msg=None):
690 return type(a) == type(b) == SadSnake
691 self.addTypeEqualityFunc(SadSnake, AllSnakesCreatedEqual)
692 self.assertEqual(s1, s2)
693 # No this doesn't clean up and remove the SadSnake equality func
694 # from this TestCase instance but since it's local nothing else
695 # will ever notice that.
696
697 def testAssertIs(self):
698 thing = object()
699 self.assertIs(thing, thing)
700 self.assertRaises(self.failureException, self.assertIs, thing, object())
701
702 def testAssertIsNot(self):
703 thing = object()
704 self.assertIsNot(thing, object())
705 self.assertRaises(self.failureException, self.assertIsNot, thing, thing)
706
707 def testAssertIsInstance(self):
708 thing = List()
709 self.assertIsInstance(thing, list)
710 self.assertIsInstance(thing, (int, list))
711 with self.assertRaises(self.failureException) as cm:
712 self.assertIsInstance(thing, int)
713 self.assertEqual(str(cm.exception),
714 "[] is not an instance of <class 'int'>")
715 with self.assertRaises(self.failureException) as cm:
716 self.assertIsInstance(thing, (int, float))
717 self.assertEqual(str(cm.exception),
718 "[] is not an instance of any of (<class 'int'>, <class 'float'>)")
719
720 with self.assertRaises(self.failureException) as cm:
721 self.assertIsInstance(thing, int, 'ababahalamaha')
722 self.assertIn('ababahalamaha', str(cm.exception))
723 with self.assertRaises(self.failureException) as cm:
724 self.assertIsInstance(thing, int, msg='ababahalamaha')
725 self.assertIn('ababahalamaha', str(cm.exception))
726
727 def testAssertNotIsInstance(self):
728 thing = List()
729 self.assertNotIsInstance(thing, int)
730 self.assertNotIsInstance(thing, (int, float))
731 with self.assertRaises(self.failureException) as cm:
732 self.assertNotIsInstance(thing, list)
733 self.assertEqual(str(cm.exception),
734 "[] is an instance of <class 'list'>")
735 with self.assertRaises(self.failureException) as cm:
736 self.assertNotIsInstance(thing, (int, list))
737 self.assertEqual(str(cm.exception),
738 "[] is an instance of <class 'list'>")
739
740 with self.assertRaises(self.failureException) as cm:
741 self.assertNotIsInstance(thing, list, 'ababahalamaha')
742 self.assertIn('ababahalamaha', str(cm.exception))
743 with self.assertRaises(self.failureException) as cm:
744 self.assertNotIsInstance(thing, list, msg='ababahalamaha')
745 self.assertIn('ababahalamaha', str(cm.exception))
746
747 def testAssertIsSubclass(self):
748 self.assertIsSubclass(List, list)
749 self.assertIsSubclass(List, (int, list))
750 with self.assertRaises(self.failureException) as cm:
751 self.assertIsSubclass(List, int)
752 self.assertEqual(str(cm.exception),
753 f"{List!r} is not a subclass of <class 'int'>")
754 with self.assertRaises(self.failureException) as cm:
755 self.assertIsSubclass(List, (int, float))
756 self.assertEqual(str(cm.exception),
757 f"{List!r} is not a subclass of any of (<class 'int'>, <class 'float'>)")
758 with self.assertRaises(self.failureException) as cm:
759 self.assertIsSubclass(1, int)
760 self.assertEqual(str(cm.exception), "1 is not a class")
761
762 with self.assertRaises(self.failureException) as cm:
763 self.assertIsSubclass(List, int, 'ababahalamaha')
764 self.assertIn('ababahalamaha', str(cm.exception))
765 with self.assertRaises(self.failureException) as cm:
766 self.assertIsSubclass(List, int, msg='ababahalamaha')
767 self.assertIn('ababahalamaha', str(cm.exception))
768
769 def testAssertNotIsSubclass(self):
770 self.assertNotIsSubclass(List, int)
771 self.assertNotIsSubclass(List, (int, float))
772 with self.assertRaises(self.failureException) as cm:
773 self.assertNotIsSubclass(List, list)
774 self.assertEqual(str(cm.exception),
775 f"{List!r} is a subclass of <class 'list'>")
776 with self.assertRaises(self.failureException) as cm:
777 self.assertNotIsSubclass(List, (int, list))
778 self.assertEqual(str(cm.exception),
779 f"{List!r} is a subclass of <class 'list'>")
780 with self.assertRaises(self.failureException) as cm:
781 self.assertNotIsSubclass(1, int)
782 self.assertEqual(str(cm.exception), "1 is not a class")
783
784 with self.assertRaises(self.failureException) as cm:
785 self.assertNotIsSubclass(List, list, 'ababahalamaha')
786 self.assertIn('ababahalamaha', str(cm.exception))
787 with self.assertRaises(self.failureException) as cm:
788 self.assertNotIsSubclass(List, list, msg='ababahalamaha')
789 self.assertIn('ababahalamaha', str(cm.exception))
790
791 def testAssertHasAttr(self):
792 a = List()
793 a.x = 1
794 self.assertHasAttr(a, 'x')
795 with self.assertRaises(self.failureException) as cm:
796 self.assertHasAttr(a, 'y')
797 self.assertEqual(str(cm.exception),
798 "'List' object has no attribute 'y'")
799 with self.assertRaises(self.failureException) as cm:
800 self.assertHasAttr(List, 'spam')
801 self.assertEqual(str(cm.exception),
802 "type object 'List' has no attribute 'spam'")
803 with self.assertRaises(self.failureException) as cm:
804 self.assertHasAttr(sys, 'nonexistent')
805 self.assertEqual(str(cm.exception),
806 "module 'sys' has no attribute 'nonexistent'")
807
808 with self.assertRaises(self.failureException) as cm:
809 self.assertHasAttr(a, 'y', 'ababahalamaha')
810 self.assertIn('ababahalamaha', str(cm.exception))
811 with self.assertRaises(self.failureException) as cm:
812 self.assertHasAttr(a, 'y', msg='ababahalamaha')
813 self.assertIn('ababahalamaha', str(cm.exception))
814
815 def testAssertNotHasAttr(self):
816 a = List()
817 a.x = 1
818 self.assertNotHasAttr(a, 'y')
819 with self.assertRaises(self.failureException) as cm:
820 self.assertNotHasAttr(a, 'x')
821 self.assertEqual(str(cm.exception),
822 "'List' object has unexpected attribute 'x'")
823 with self.assertRaises(self.failureException) as cm:
824 self.assertNotHasAttr(List, 'append')
825 self.assertEqual(str(cm.exception),
826 "type object 'List' has unexpected attribute 'append'")
827 with self.assertRaises(self.failureException) as cm:
828 self.assertNotHasAttr(sys, 'modules')
829 self.assertEqual(str(cm.exception),
830 "module 'sys' has unexpected attribute 'modules'")
831
832 with self.assertRaises(self.failureException) as cm:
833 self.assertNotHasAttr(a, 'x', 'ababahalamaha')
834 self.assertIn('ababahalamaha', str(cm.exception))
835 with self.assertRaises(self.failureException) as cm:
836 self.assertNotHasAttr(a, 'x', msg='ababahalamaha')
837 self.assertIn('ababahalamaha', str(cm.exception))
838
839 def testAssertIn(self):
840 animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'}
841
842 self.assertIn('a', 'abc')
843 self.assertIn(2, [1, 2, 3])
844 self.assertIn('monkey', animals)
845
846 self.assertNotIn('d', 'abc')
847 self.assertNotIn(0, [1, 2, 3])
848 self.assertNotIn('otter', animals)
849
850 self.assertRaises(self.failureException, self.assertIn, 'x', 'abc')
851 self.assertRaises(self.failureException, self.assertIn, 4, [1, 2, 3])
852 self.assertRaises(self.failureException, self.assertIn, 'elephant',
853 animals)
854
855 self.assertRaises(self.failureException, self.assertNotIn, 'c', 'abc')
856 self.assertRaises(self.failureException, self.assertNotIn, 1, [1, 2, 3])
857 self.assertRaises(self.failureException, self.assertNotIn, 'cow',
858 animals)
859
860 def testAssertEqual(self):
861 equal_pairs = [
862 ((), ()),
863 ({}, {}),
864 ([], []),
865 (set(), set()),
866 (frozenset(), frozenset())]
867 for a, b in equal_pairs:
868 # This mess of try excepts is to test the assertEqual behavior
869 # itself.
870 try:
871 self.assertEqual(a, b)
872 except self.failureException:
873 self.fail('assertEqual(%r, %r) failed' % (a, b))
874 try:
875 self.assertEqual(a, b, msg='foo')
876 except self.failureException:
877 self.fail('assertEqual(%r, %r) with msg= failed' % (a, b))
878 try:
879 self.assertEqual(a, b, 'foo')
880 except self.failureException:
881 self.fail('assertEqual(%r, %r) with third parameter failed' %
882 (a, b))
883
884 unequal_pairs = [
885 ((), []),
886 ({}, set()),
887 (set([4,1]), frozenset([4,2])),
888 (frozenset([4,5]), set([2,3])),
889 (set([3,4]), set([5,4]))]
890 for a, b in unequal_pairs:
891 self.assertRaises(self.failureException, self.assertEqual, a, b)
892 self.assertRaises(self.failureException, self.assertEqual, a, b,
893 'foo')
894 self.assertRaises(self.failureException, self.assertEqual, a, b,
895 msg='foo')
896
897 def testEquality(self):
898 self.assertListEqual([], [])
899 self.assertTupleEqual((), ())
900 self.assertSequenceEqual([], ())
901
902 a = [0, 'a', []]
903 b = []
904 self.assertRaises(unittest.TestCase.failureException,
905 self.assertListEqual, a, b)
906 self.assertRaises(unittest.TestCase.failureException,
907 self.assertListEqual, tuple(a), tuple(b))
908 self.assertRaises(unittest.TestCase.failureException,
909 self.assertSequenceEqual, a, tuple(b))
910
911 b.extend(a)
912 self.assertListEqual(a, b)
913 self.assertTupleEqual(tuple(a), tuple(b))
914 self.assertSequenceEqual(a, tuple(b))
915 self.assertSequenceEqual(tuple(a), b)
916
917 self.assertRaises(self.failureException, self.assertListEqual,
918 a, tuple(b))
919 self.assertRaises(self.failureException, self.assertTupleEqual,
920 tuple(a), b)
921 self.assertRaises(self.failureException, self.assertListEqual, None, b)
922 self.assertRaises(self.failureException, self.assertTupleEqual, None,
923 tuple(b))
924 self.assertRaises(self.failureException, self.assertSequenceEqual,
925 None, tuple(b))
926 self.assertRaises(self.failureException, self.assertListEqual, 1, 1)
927 self.assertRaises(self.failureException, self.assertTupleEqual, 1, 1)
928 self.assertRaises(self.failureException, self.assertSequenceEqual,
929 1, 1)
930
931 self.assertDictEqual({}, {})
932
933 c = { 'x': 1 }
934 d = {}
935 self.assertRaises(unittest.TestCase.failureException,
936 self.assertDictEqual, c, d)
937
938 d.update(c)
939 self.assertDictEqual(c, d)
940
941 d['x'] = 0
942 self.assertRaises(unittest.TestCase.failureException,
943 self.assertDictEqual, c, d, 'These are unequal')
944
945 self.assertRaises(self.failureException, self.assertDictEqual, None, d)
946 self.assertRaises(self.failureException, self.assertDictEqual, [], d)
947 self.assertRaises(self.failureException, self.assertDictEqual, 1, 1)
948
949 def testAssertSequenceEqualMaxDiff(self):
950 self.assertEqual(self.maxDiff, 80*8)
951 seq1 = 'a' + 'x' * 80**2
952 seq2 = 'b' + 'x' * 80**2
953 diff = '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(),
954 pprint.pformat(seq2).splitlines()))
955 # the +1 is the leading \n added by assertSequenceEqual
956 omitted = unittest.case.DIFF_OMITTED % (len(diff) + 1,)
957
958 self.maxDiff = len(diff)//2
959 try:
960
961 self.assertSequenceEqual(seq1, seq2)
962 except self.failureException as e:
963 msg = e.args[0]
964 else:
965 self.fail('assertSequenceEqual did not fail.')
966 self.assertLess(len(msg), len(diff))
967 self.assertIn(omitted, msg)
968
969 self.maxDiff = len(diff) * 2
970 try:
971 self.assertSequenceEqual(seq1, seq2)
972 except self.failureException as e:
973 msg = e.args[0]
974 else:
975 self.fail('assertSequenceEqual did not fail.')
976 self.assertGreater(len(msg), len(diff))
977 self.assertNotIn(omitted, msg)
978
979 self.maxDiff = None
980 try:
981 self.assertSequenceEqual(seq1, seq2)
982 except self.failureException as e:
983 msg = e.args[0]
984 else:
985 self.fail('assertSequenceEqual did not fail.')
986 self.assertGreater(len(msg), len(diff))
987 self.assertNotIn(omitted, msg)
988
989 def testTruncateMessage(self):
990 self.maxDiff = 1
991 message = self._truncateMessage('foo', 'bar')
992 omitted = unittest.case.DIFF_OMITTED % len('bar')
993 self.assertEqual(message, 'foo' + omitted)
994
995 self.maxDiff = None
996 message = self._truncateMessage('foo', 'bar')
997 self.assertEqual(message, 'foobar')
998
999 self.maxDiff = 4
1000 message = self._truncateMessage('foo', 'bar')
1001 self.assertEqual(message, 'foobar')
1002
1003 def testAssertDictEqualTruncates(self):
1004 test = unittest.TestCase('assertEqual')
1005 def truncate(msg, diff):
1006 return 'foo'
1007 test._truncateMessage = truncate
1008 try:
1009 test.assertDictEqual({}, {1: 0})
1010 except self.failureException as e:
1011 self.assertEqual(str(e), 'foo')
1012 else:
1013 self.fail('assertDictEqual did not fail')
1014
1015 def testAssertMultiLineEqualTruncates(self):
1016 test = unittest.TestCase('assertEqual')
1017 def truncate(msg, diff):
1018 return 'foo'
1019 test._truncateMessage = truncate
1020 try:
1021 test.assertMultiLineEqual('foo', 'bar')
1022 except self.failureException as e:
1023 self.assertEqual(str(e), 'foo')
1024 else:
1025 self.fail('assertMultiLineEqual did not fail')
1026
1027 def testAssertEqual_diffThreshold(self):
1028 # check threshold value
1029 self.assertEqual(self._diffThreshold, 2**16)
1030 # disable madDiff to get diff markers
1031 self.maxDiff = None
1032
1033 # set a lower threshold value and add a cleanup to restore it
1034 old_threshold = self._diffThreshold
1035 self._diffThreshold = 2**5
1036 self.addCleanup(lambda: setattr(self, '_diffThreshold', old_threshold))
1037
1038 # under the threshold: diff marker (^) in error message
1039 s = 'x' * (2**4)
1040 with self.assertRaises(self.failureException) as cm:
1041 self.assertEqual(s + 'a', s + 'b')
1042 self.assertIn('^', str(cm.exception))
1043 self.assertEqual(s + 'a', s + 'a')
1044
1045 # over the threshold: diff not used and marker (^) not in error message
1046 s = 'x' * (2**6)
1047 # if the path that uses difflib is taken, _truncateMessage will be
1048 # called -- replace it with explodingTruncation to verify that this
1049 # doesn't happen
1050 def explodingTruncation(message, diff):
1051 raise SystemError('this should not be raised')
1052 old_truncate = self._truncateMessage
1053 self._truncateMessage = explodingTruncation
1054 self.addCleanup(lambda: setattr(self, '_truncateMessage', old_truncate))
1055
1056 s1, s2 = s + 'a', s + 'b'
1057 with self.assertRaises(self.failureException) as cm:
1058 self.assertEqual(s1, s2)
1059 self.assertNotIn('^', str(cm.exception))
1060 self.assertEqual(str(cm.exception), '%r != %r' % (s1, s2))
1061 self.assertEqual(s + 'a', s + 'a')
1062
1063 def testAssertEqual_shorten(self):
1064 # set a lower threshold value and add a cleanup to restore it
1065 old_threshold = self._diffThreshold
1066 self._diffThreshold = 0
1067 self.addCleanup(lambda: setattr(self, '_diffThreshold', old_threshold))
1068
1069 s = 'x' * 100
1070 s1, s2 = s + 'a', s + 'b'
1071 with self.assertRaises(self.failureException) as cm:
1072 self.assertEqual(s1, s2)
1073 c = 'xxxx[35 chars]' + 'x' * 61
1074 self.assertEqual(str(cm.exception), "'%sa' != '%sb'" % (c, c))
1075 self.assertEqual(s + 'a', s + 'a')
1076
1077 p = 'y' * 50
1078 s1, s2 = s + 'a' + p, s + 'b' + p
1079 with self.assertRaises(self.failureException) as cm:
1080 self.assertEqual(s1, s2)
1081 c = 'xxxx[85 chars]xxxxxxxxxxx'
1082 self.assertEqual(str(cm.exception), "'%sa%s' != '%sb%s'" % (c, p, c, p))
1083
1084 p = 'y' * 100
1085 s1, s2 = s + 'a' + p, s + 'b' + p
1086 with self.assertRaises(self.failureException) as cm:
1087 self.assertEqual(s1, s2)
1088 c = 'xxxx[91 chars]xxxxx'
1089 d = 'y' * 40 + '[56 chars]yyyy'
1090 self.assertEqual(str(cm.exception), "'%sa%s' != '%sb%s'" % (c, d, c, d))
1091
1092 def testAssertCountEqual(self):
1093 a = object()
1094 self.assertCountEqual([1, 2, 3], [3, 2, 1])
1095 self.assertCountEqual(['foo', 'bar', 'baz'], ['bar', 'baz', 'foo'])
1096 self.assertCountEqual([a, a, 2, 2, 3], (a, 2, 3, a, 2))
1097 self.assertCountEqual([1, "2", "a", "a"], ["a", "2", True, "a"])
1098 self.assertRaises(self.failureException, self.assertCountEqual,
1099 [1, 2] + [3] * 100, [1] * 100 + [2, 3])
1100 self.assertRaises(self.failureException, self.assertCountEqual,
1101 [1, "2", "a", "a"], ["a", "2", True, 1])
1102 self.assertRaises(self.failureException, self.assertCountEqual,
1103 [10], [10, 11])
1104 self.assertRaises(self.failureException, self.assertCountEqual,
1105 [10, 11], [10])
1106 self.assertRaises(self.failureException, self.assertCountEqual,
1107 [10, 11, 10], [10, 11])
1108
1109 # Test that sequences of unhashable objects can be tested for sameness:
1110 self.assertCountEqual([[1, 2], [3, 4], 0], [False, [3, 4], [1, 2]])
1111 # Test that iterator of unhashable objects can be tested for sameness:
1112 self.assertCountEqual(iter([1, 2, [], 3, 4]),
1113 iter([1, 2, [], 3, 4]))
1114
1115 # hashable types, but not orderable
1116 self.assertRaises(self.failureException, self.assertCountEqual,
1117 [], [divmod, 'x', 1, 5j, 2j, frozenset()])
1118 # comparing dicts
1119 self.assertCountEqual([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}])
1120 # comparing heterogeneous non-hashable sequences
1121 self.assertCountEqual([1, 'x', divmod, []], [divmod, [], 'x', 1])
1122 self.assertRaises(self.failureException, self.assertCountEqual,
1123 [], [divmod, [], 'x', 1, 5j, 2j, set()])
1124 self.assertRaises(self.failureException, self.assertCountEqual,
1125 [[1]], [[2]])
1126
1127 # Same elements, but not same sequence length
1128 self.assertRaises(self.failureException, self.assertCountEqual,
1129 [1, 1, 2], [2, 1])
1130 self.assertRaises(self.failureException, self.assertCountEqual,
1131 [1, 1, "2", "a", "a"], ["2", "2", True, "a"])
1132 self.assertRaises(self.failureException, self.assertCountEqual,
1133 [1, {'b': 2}, None, True], [{'b': 2}, True, None])
1134
1135 # Same elements which don't reliably compare, in
1136 # different order, see issue 10242
1137 a = [{2,4}, {1,2}]
1138 b = a[::-1]
1139 self.assertCountEqual(a, b)
1140
1141 # test utility functions supporting assertCountEqual()
1142
1143 diffs = set(unittest.util._count_diff_all_purpose('aaabccd', 'abbbcce'))
1144 expected = {(3,1,'a'), (1,3,'b'), (1,0,'d'), (0,1,'e')}
1145 self.assertEqual(diffs, expected)
1146
1147 diffs = unittest.util._count_diff_all_purpose([[]], [])
1148 self.assertEqual(diffs, [(1, 0, [])])
1149
1150 diffs = set(unittest.util._count_diff_hashable('aaabccd', 'abbbcce'))
1151 expected = {(3,1,'a'), (1,3,'b'), (1,0,'d'), (0,1,'e')}
1152 self.assertEqual(diffs, expected)
1153
1154 def testAssertSetEqual(self):
1155 set1 = set()
1156 set2 = set()
1157 self.assertSetEqual(set1, set2)
1158
1159 self.assertRaises(self.failureException, self.assertSetEqual, None, set2)
1160 self.assertRaises(self.failureException, self.assertSetEqual, [], set2)
1161 self.assertRaises(self.failureException, self.assertSetEqual, set1, None)
1162 self.assertRaises(self.failureException, self.assertSetEqual, set1, [])
1163
1164 set1 = set(['a'])
1165 set2 = set()
1166 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
1167
1168 set1 = set(['a'])
1169 set2 = set(['a'])
1170 self.assertSetEqual(set1, set2)
1171
1172 set1 = set(['a'])
1173 set2 = set(['a', 'b'])
1174 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
1175
1176 set1 = set(['a'])
1177 set2 = frozenset(['a', 'b'])
1178 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
1179
1180 set1 = set(['a', 'b'])
1181 set2 = frozenset(['a', 'b'])
1182 self.assertSetEqual(set1, set2)
1183
1184 set1 = set()
1185 set2 = "foo"
1186 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
1187 self.assertRaises(self.failureException, self.assertSetEqual, set2, set1)
1188
1189 # make sure any string formatting is tuple-safe
1190 set1 = set([(0, 1), (2, 3)])
1191 set2 = set([(4, 5)])
1192 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2)
1193
1194 def testInequality(self):
1195 # Try ints
1196 self.assertGreater(2, 1)
1197 self.assertGreaterEqual(2, 1)
1198 self.assertGreaterEqual(1, 1)
1199 self.assertLess(1, 2)
1200 self.assertLessEqual(1, 2)
1201 self.assertLessEqual(1, 1)
1202 self.assertRaises(self.failureException, self.assertGreater, 1, 2)
1203 self.assertRaises(self.failureException, self.assertGreater, 1, 1)
1204 self.assertRaises(self.failureException, self.assertGreaterEqual, 1, 2)
1205 self.assertRaises(self.failureException, self.assertLess, 2, 1)
1206 self.assertRaises(self.failureException, self.assertLess, 1, 1)
1207 self.assertRaises(self.failureException, self.assertLessEqual, 2, 1)
1208
1209 # Try Floats
1210 self.assertGreater(1.1, 1.0)
1211 self.assertGreaterEqual(1.1, 1.0)
1212 self.assertGreaterEqual(1.0, 1.0)
1213 self.assertLess(1.0, 1.1)
1214 self.assertLessEqual(1.0, 1.1)
1215 self.assertLessEqual(1.0, 1.0)
1216 self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.1)
1217 self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.0)
1218 self.assertRaises(self.failureException, self.assertGreaterEqual, 1.0, 1.1)
1219 self.assertRaises(self.failureException, self.assertLess, 1.1, 1.0)
1220 self.assertRaises(self.failureException, self.assertLess, 1.0, 1.0)
1221 self.assertRaises(self.failureException, self.assertLessEqual, 1.1, 1.0)
1222
1223 # Try Strings
1224 self.assertGreater('bug', 'ant')
1225 self.assertGreaterEqual('bug', 'ant')
1226 self.assertGreaterEqual('ant', 'ant')
1227 self.assertLess('ant', 'bug')
1228 self.assertLessEqual('ant', 'bug')
1229 self.assertLessEqual('ant', 'ant')
1230 self.assertRaises(self.failureException, self.assertGreater, 'ant', 'bug')
1231 self.assertRaises(self.failureException, self.assertGreater, 'ant', 'ant')
1232 self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant', 'bug')
1233 self.assertRaises(self.failureException, self.assertLess, 'bug', 'ant')
1234 self.assertRaises(self.failureException, self.assertLess, 'ant', 'ant')
1235 self.assertRaises(self.failureException, self.assertLessEqual, 'bug', 'ant')
1236
1237 # Try bytes
1238 self.assertGreater(b'bug', b'ant')
1239 self.assertGreaterEqual(b'bug', b'ant')
1240 self.assertGreaterEqual(b'ant', b'ant')
1241 self.assertLess(b'ant', b'bug')
1242 self.assertLessEqual(b'ant', b'bug')
1243 self.assertLessEqual(b'ant', b'ant')
1244 self.assertRaises(self.failureException, self.assertGreater, b'ant', b'bug')
1245 self.assertRaises(self.failureException, self.assertGreater, b'ant', b'ant')
1246 self.assertRaises(self.failureException, self.assertGreaterEqual, b'ant',
1247 b'bug')
1248 self.assertRaises(self.failureException, self.assertLess, b'bug', b'ant')
1249 self.assertRaises(self.failureException, self.assertLess, b'ant', b'ant')
1250 self.assertRaises(self.failureException, self.assertLessEqual, b'bug', b'ant')
1251
1252 def testAssertMultiLineEqual(self):
1253 sample_text = """\
1254 http://www.python.org/doc/2.3/lib/module-unittest.html
1255 test case
1256 A test case is the smallest unit of testing. [...]
1257 """
1258 revised_sample_text = """\
1259 http://www.python.org/doc/2.4.1/lib/module-unittest.html
1260 test case
1261 A test case is the smallest unit of testing. [...] You may provide your
1262 own implementation that does not subclass from TestCase, of course.
1263 """
1264 sample_text_error = """\
1265 - http://www.python.org/doc/2.3/lib/module-unittest.html
1266 ? ^
1267 + http://www.python.org/doc/2.4.1/lib/module-unittest.html
1268 ? ^^^
1269 test case
1270 - A test case is the smallest unit of testing. [...]
1271 + A test case is the smallest unit of testing. [...] You may provide your
1272 ? +++++++++++++++++++++
1273 + own implementation that does not subclass from TestCase, of course.
1274 """
1275 self.maxDiff = None
1276 try:
1277 self.assertMultiLineEqual(sample_text, revised_sample_text)
1278 except self.failureException as e:
1279 # need to remove the first line of the error message
1280 error = str(e).split('\n', 1)[1]
1281 self.assertEqual(sample_text_error, error)
1282 else:
1283 self.fail(f'{self.failureException} not raised')
1284
1285 def testAssertEqualSingleLine(self):
1286 sample_text = "laden swallows fly slowly"
1287 revised_sample_text = "unladen swallows fly quickly"
1288 sample_text_error = """\
1289 - laden swallows fly slowly
1290 ? ^^^^
1291 + unladen swallows fly quickly
1292 ? ++ ^^^^^
1293 """
1294 try:
1295 self.assertEqual(sample_text, revised_sample_text)
1296 except self.failureException as e:
1297 # need to remove the first line of the error message
1298 error = str(e).split('\n', 1)[1]
1299 self.assertEqual(sample_text_error, error)
1300 else:
1301 self.fail(f'{self.failureException} not raised')
1302
1303 def testAssertEqualwithEmptyString(self):
1304 '''Verify when there is an empty string involved, the diff output
1305 does not treat the empty string as a single empty line. It should
1306 instead be handled as a non-line.
1307 '''
1308 sample_text = ''
1309 revised_sample_text = 'unladen swallows fly quickly'
1310 sample_text_error = '''\
1311 + unladen swallows fly quickly
1312 '''
1313 try:
1314 self.assertEqual(sample_text, revised_sample_text)
1315 except self.failureException as e:
1316 # need to remove the first line of the error message
1317 error = str(e).split('\n', 1)[1]
1318 self.assertEqual(sample_text_error, error)
1319 else:
1320 self.fail(f'{self.failureException} not raised')
1321
1322 def testAssertEqualMultipleLinesMissingNewlineTerminator(self):
1323 '''Verifying format of diff output from assertEqual involving strings
1324 with multiple lines, but missing the terminating newline on both.
1325 '''
1326 sample_text = 'laden swallows\nfly sloely'
1327 revised_sample_text = 'laden swallows\nfly slowly'
1328 sample_text_error = '''\
1329 laden swallows
1330 - fly sloely
1331 ? ^
1332 + fly slowly
1333 ? ^
1334 '''
1335 try:
1336 self.assertEqual(sample_text, revised_sample_text)
1337 except self.failureException as e:
1338 # need to remove the first line of the error message
1339 error = str(e).split('\n', 1)[1]
1340 self.assertEqual(sample_text_error, error)
1341 else:
1342 self.fail(f'{self.failureException} not raised')
1343
1344 def testAssertEqualMultipleLinesMismatchedNewlinesTerminators(self):
1345 '''Verifying format of diff output from assertEqual involving strings
1346 with multiple lines and mismatched newlines. The output should
1347 include a - on it's own line to indicate the newline difference
1348 between the two strings
1349 '''
1350 sample_text = 'laden swallows\nfly sloely\n'
1351 revised_sample_text = 'laden swallows\nfly slowly'
1352 sample_text_error = '''\
1353 laden swallows
1354 - fly sloely
1355 ? ^
1356 + fly slowly
1357 ? ^
1358 -\x20
1359 '''
1360 try:
1361 self.assertEqual(sample_text, revised_sample_text)
1362 except self.failureException as e:
1363 # need to remove the first line of the error message
1364 error = str(e).split('\n', 1)[1]
1365 self.assertEqual(sample_text_error, error)
1366 else:
1367 self.fail(f'{self.failureException} not raised')
1368
1369 def testEqualityBytesWarning(self):
1370 if sys.flags.bytes_warning:
1371 def bytes_warning():
1372 return self.assertWarnsRegex(BytesWarning,
1373 'Comparison between bytes and string')
1374 else:
1375 def bytes_warning():
1376 return contextlib.ExitStack()
1377
1378 with bytes_warning(), self.assertRaises(self.failureException):
1379 self.assertEqual('a', b'a')
1380 with bytes_warning():
1381 self.assertNotEqual('a', b'a')
1382
1383 a = [0, 'a']
1384 b = [0, b'a']
1385 with bytes_warning(), self.assertRaises(self.failureException):
1386 self.assertListEqual(a, b)
1387 with bytes_warning(), self.assertRaises(self.failureException):
1388 self.assertTupleEqual(tuple(a), tuple(b))
1389 with bytes_warning(), self.assertRaises(self.failureException):
1390 self.assertSequenceEqual(a, tuple(b))
1391 with bytes_warning(), self.assertRaises(self.failureException):
1392 self.assertSequenceEqual(tuple(a), b)
1393 with bytes_warning(), self.assertRaises(self.failureException):
1394 self.assertSequenceEqual('a', b'a')
1395 with bytes_warning(), self.assertRaises(self.failureException):
1396 self.assertSetEqual(set(a), set(b))
1397
1398 with self.assertRaises(self.failureException):
1399 self.assertListEqual(a, tuple(b))
1400 with self.assertRaises(self.failureException):
1401 self.assertTupleEqual(tuple(a), b)
1402
1403 a = [0, b'a']
1404 b = [0]
1405 with self.assertRaises(self.failureException):
1406 self.assertListEqual(a, b)
1407 with self.assertRaises(self.failureException):
1408 self.assertTupleEqual(tuple(a), tuple(b))
1409 with self.assertRaises(self.failureException):
1410 self.assertSequenceEqual(a, tuple(b))
1411 with self.assertRaises(self.failureException):
1412 self.assertSequenceEqual(tuple(a), b)
1413 with self.assertRaises(self.failureException):
1414 self.assertSetEqual(set(a), set(b))
1415
1416 a = [0]
1417 b = [0, b'a']
1418 with self.assertRaises(self.failureException):
1419 self.assertListEqual(a, b)
1420 with self.assertRaises(self.failureException):
1421 self.assertTupleEqual(tuple(a), tuple(b))
1422 with self.assertRaises(self.failureException):
1423 self.assertSequenceEqual(a, tuple(b))
1424 with self.assertRaises(self.failureException):
1425 self.assertSequenceEqual(tuple(a), b)
1426 with self.assertRaises(self.failureException):
1427 self.assertSetEqual(set(a), set(b))
1428
1429 with bytes_warning(), self.assertRaises(self.failureException):
1430 self.assertDictEqual({'a': 0}, {b'a': 0})
1431 with self.assertRaises(self.failureException):
1432 self.assertDictEqual({}, {b'a': 0})
1433 with self.assertRaises(self.failureException):
1434 self.assertDictEqual({b'a': 0}, {})
1435
1436 with self.assertRaises(self.failureException):
1437 self.assertCountEqual([b'a', b'a'], [b'a', b'a', b'a'])
1438 with bytes_warning():
1439 self.assertCountEqual(['a', b'a'], ['a', b'a'])
1440 with bytes_warning(), self.assertRaises(self.failureException):
1441 self.assertCountEqual(['a', 'a'], [b'a', b'a'])
1442 with bytes_warning(), self.assertRaises(self.failureException):
1443 self.assertCountEqual(['a', 'a', []], [b'a', b'a', []])
1444
1445 def testAssertIsNone(self):
1446 self.assertIsNone(None)
1447 self.assertRaises(self.failureException, self.assertIsNone, False)
1448 self.assertIsNotNone('DjZoPloGears on Rails')
1449 self.assertRaises(self.failureException, self.assertIsNotNone, None)
1450
1451 def testAssertRegex(self):
1452 self.assertRegex('asdfabasdf', r'ab+')
1453 self.assertRaises(self.failureException, self.assertRegex,
1454 'saaas', r'aaaa')
1455
1456 def testAssertRaisesCallable(self):
1457 class ExceptionMock(Exception):
1458 pass
1459 def Stub():
1460 raise ExceptionMock('We expect')
1461 self.assertRaises(ExceptionMock, Stub)
1462 # A tuple of exception classes is accepted
1463 self.assertRaises((ValueError, ExceptionMock), Stub)
1464 # *args and **kwargs also work
1465 self.assertRaises(ValueError, int, '19', base=8)
1466 # Failure when no exception is raised
1467 with self.assertRaises(self.failureException):
1468 self.assertRaises(ExceptionMock, lambda: 0)
1469 # Failure when the function is None
1470 with self.assertRaises(TypeError):
1471 self.assertRaises(ExceptionMock, None)
1472 # Failure when another exception is raised
1473 with self.assertRaises(ExceptionMock):
1474 self.assertRaises(ValueError, Stub)
1475
1476 def testAssertRaisesContext(self):
1477 class ExceptionMock(Exception):
1478 pass
1479 def Stub():
1480 raise ExceptionMock('We expect')
1481 with self.assertRaises(ExceptionMock):
1482 Stub()
1483 # A tuple of exception classes is accepted
1484 with self.assertRaises((ValueError, ExceptionMock)) as cm:
1485 Stub()
1486 # The context manager exposes caught exception
1487 self.assertIsInstance(cm.exception, ExceptionMock)
1488 self.assertEqual(cm.exception.args[0], 'We expect')
1489 # *args and **kwargs also work
1490 with self.assertRaises(ValueError):
1491 int('19', base=8)
1492 # Failure when no exception is raised
1493 with self.assertRaises(self.failureException):
1494 with self.assertRaises(ExceptionMock):
1495 pass
1496 # Custom message
1497 with self.assertRaisesRegex(self.failureException, 'foobar'):
1498 with self.assertRaises(ExceptionMock, msg='foobar'):
1499 pass
1500 # Invalid keyword argument
1501 with self.assertRaisesRegex(TypeError, 'foobar'):
1502 with self.assertRaises(ExceptionMock, foobar=42):
1503 pass
1504 # Failure when another exception is raised
1505 with self.assertRaises(ExceptionMock):
1506 self.assertRaises(ValueError, Stub)
1507
1508 def testAssertRaisesNoExceptionType(self):
1509 with self.assertRaises(TypeError):
1510 self.assertRaises()
1511 with self.assertRaises(TypeError):
1512 self.assertRaises(1)
1513 with self.assertRaises(TypeError):
1514 self.assertRaises(object)
1515 with self.assertRaises(TypeError):
1516 self.assertRaises((ValueError, 1))
1517 with self.assertRaises(TypeError):
1518 self.assertRaises((ValueError, object))
1519
1520 def testAssertRaisesRefcount(self):
1521 # bpo-23890: assertRaises() must not keep objects alive longer
1522 # than expected
1523 def func() :
1524 try:
1525 raise ValueError
1526 except ValueError:
1527 raise ValueError
1528
1529 refcount = sys.getrefcount(func)
1530 self.assertRaises(ValueError, func)
1531 self.assertEqual(refcount, sys.getrefcount(func))
1532
1533 def testAssertRaisesRegex(self):
1534 class ExceptionMock(Exception):
1535 pass
1536
1537 def Stub():
1538 raise ExceptionMock('We expect')
1539
1540 self.assertRaisesRegex(ExceptionMock, re.compile('expect$'), Stub)
1541 self.assertRaisesRegex(ExceptionMock, 'expect$', Stub)
1542 with self.assertRaises(TypeError):
1543 self.assertRaisesRegex(ExceptionMock, 'expect$', None)
1544
1545 def testAssertNotRaisesRegex(self):
1546 self.assertRaisesRegex(
1547 self.failureException, '^Exception not raised by <lambda>$',
1548 self.assertRaisesRegex, Exception, re.compile('x'),
1549 lambda: None)
1550 self.assertRaisesRegex(
1551 self.failureException, '^Exception not raised by <lambda>$',
1552 self.assertRaisesRegex, Exception, 'x',
1553 lambda: None)
1554 # Custom message
1555 with self.assertRaisesRegex(self.failureException, 'foobar'):
1556 with self.assertRaisesRegex(Exception, 'expect', msg='foobar'):
1557 pass
1558 # Invalid keyword argument
1559 with self.assertRaisesRegex(TypeError, 'foobar'):
1560 with self.assertRaisesRegex(Exception, 'expect', foobar=42):
1561 pass
1562
1563 def testAssertRaisesRegexInvalidRegex(self):
1564 # Issue 20145.
1565 class MyExc(Exception):
1566 pass
1567 self.assertRaises(TypeError, self.assertRaisesRegex, MyExc, lambda: True)
1568
1569 def testAssertWarnsRegexInvalidRegex(self):
1570 # Issue 20145.
1571 class MyWarn(Warning):
1572 pass
1573 self.assertRaises(TypeError, self.assertWarnsRegex, MyWarn, lambda: True)
1574
1575 def testAssertWarnsModifySysModules(self):
1576 # bpo-29620: handle modified sys.modules during iteration
1577 class Foo(types.ModuleType):
1578 @property
1579 def __warningregistry__(self):
1580 sys.modules['@bar@'] = 'bar'
1581
1582 sys.modules['@foo@'] = Foo('foo')
1583 try:
1584 self.assertWarns(UserWarning, warnings.warn, 'expected')
1585 finally:
1586 del sys.modules['@foo@']
1587 del sys.modules['@bar@']
1588
1589 def testAssertRaisesRegexMismatch(self):
1590 def Stub():
1591 raise Exception('Unexpected')
1592
1593 self.assertRaisesRegex(
1594 self.failureException,
1595 r'"\^Expected\$" does not match "Unexpected"',
1596 self.assertRaisesRegex, Exception, '^Expected$',
1597 Stub)
1598 self.assertRaisesRegex(
1599 self.failureException,
1600 r'"\^Expected\$" does not match "Unexpected"',
1601 self.assertRaisesRegex, Exception,
1602 re.compile('^Expected$'), Stub)
1603
1604 def testAssertRaisesExcValue(self):
1605 class ExceptionMock(Exception):
1606 pass
1607
1608 def Stub(foo):
1609 raise ExceptionMock(foo)
1610 v = "particular value"
1611
1612 ctx = self.assertRaises(ExceptionMock)
1613 with ctx:
1614 Stub(v)
1615 e = ctx.exception
1616 self.assertIsInstance(e, ExceptionMock)
1617 self.assertEqual(e.args[0], v)
1618
1619 def testAssertRaisesRegexNoExceptionType(self):
1620 with self.assertRaises(TypeError):
1621 self.assertRaisesRegex()
1622 with self.assertRaises(TypeError):
1623 self.assertRaisesRegex(ValueError)
1624 with self.assertRaises(TypeError):
1625 self.assertRaisesRegex(1, 'expect')
1626 with self.assertRaises(TypeError):
1627 self.assertRaisesRegex(object, 'expect')
1628 with self.assertRaises(TypeError):
1629 self.assertRaisesRegex((ValueError, 1), 'expect')
1630 with self.assertRaises(TypeError):
1631 self.assertRaisesRegex((ValueError, object), 'expect')
1632
1633 def testAssertWarnsCallable(self):
1634 def _runtime_warn():
1635 warnings.warn("foo", RuntimeWarning)
1636 # Success when the right warning is triggered, even several times
1637 self.assertWarns(RuntimeWarning, _runtime_warn)
1638 self.assertWarns(RuntimeWarning, _runtime_warn)
1639 # A tuple of warning classes is accepted
1640 self.assertWarns((DeprecationWarning, RuntimeWarning), _runtime_warn)
1641 # *args and **kwargs also work
1642 self.assertWarns(RuntimeWarning,
1643 warnings.warn, "foo", category=RuntimeWarning)
1644 # Failure when no warning is triggered
1645 with self.assertRaises(self.failureException):
1646 self.assertWarns(RuntimeWarning, lambda: 0)
1647 # Failure when the function is None
1648 with self.assertRaises(TypeError):
1649 self.assertWarns(RuntimeWarning, None)
1650 # Failure when another warning is triggered
1651 with warnings.catch_warnings():
1652 # Force default filter (in case tests are run with -We)
1653 warnings.simplefilter("default", RuntimeWarning)
1654 with self.assertRaises(self.failureException):
1655 self.assertWarns(DeprecationWarning, _runtime_warn)
1656 # Filters for other warnings are not modified
1657 with warnings.catch_warnings():
1658 warnings.simplefilter("error", RuntimeWarning)
1659 with self.assertRaises(RuntimeWarning):
1660 self.assertWarns(DeprecationWarning, _runtime_warn)
1661
1662 def testAssertWarnsContext(self):
1663 # Believe it or not, it is preferable to duplicate all tests above,
1664 # to make sure the __warningregistry__ $@ is circumvented correctly.
1665 def _runtime_warn():
1666 warnings.warn("foo", RuntimeWarning)
1667 _runtime_warn_lineno = inspect.getsourcelines(_runtime_warn)[1]
1668 with self.assertWarns(RuntimeWarning) as cm:
1669 _runtime_warn()
1670 # A tuple of warning classes is accepted
1671 with self.assertWarns((DeprecationWarning, RuntimeWarning)) as cm:
1672 _runtime_warn()
1673 # The context manager exposes various useful attributes
1674 self.assertIsInstance(cm.warning, RuntimeWarning)
1675 self.assertEqual(cm.warning.args[0], "foo")
1676 self.assertIn("test_case.py", cm.filename)
1677 self.assertEqual(cm.lineno, _runtime_warn_lineno + 1)
1678 # Same with several warnings
1679 with self.assertWarns(RuntimeWarning):
1680 _runtime_warn()
1681 _runtime_warn()
1682 with self.assertWarns(RuntimeWarning):
1683 warnings.warn("foo", category=RuntimeWarning)
1684 # Failure when no warning is triggered
1685 with self.assertRaises(self.failureException):
1686 with self.assertWarns(RuntimeWarning):
1687 pass
1688 # Custom message
1689 with self.assertRaisesRegex(self.failureException, 'foobar'):
1690 with self.assertWarns(RuntimeWarning, msg='foobar'):
1691 pass
1692 # Invalid keyword argument
1693 with self.assertRaisesRegex(TypeError, 'foobar'):
1694 with self.assertWarns(RuntimeWarning, foobar=42):
1695 pass
1696 # Failure when another warning is triggered
1697 with warnings.catch_warnings():
1698 # Force default filter (in case tests are run with -We)
1699 warnings.simplefilter("default", RuntimeWarning)
1700 with self.assertRaises(self.failureException):
1701 with self.assertWarns(DeprecationWarning):
1702 _runtime_warn()
1703 # Filters for other warnings are not modified
1704 with warnings.catch_warnings():
1705 warnings.simplefilter("error", RuntimeWarning)
1706 with self.assertRaises(RuntimeWarning):
1707 with self.assertWarns(DeprecationWarning):
1708 _runtime_warn()
1709
1710 def testAssertWarnsNoExceptionType(self):
1711 with self.assertRaises(TypeError):
1712 self.assertWarns()
1713 with self.assertRaises(TypeError):
1714 self.assertWarns(1)
1715 with self.assertRaises(TypeError):
1716 self.assertWarns(object)
1717 with self.assertRaises(TypeError):
1718 self.assertWarns((UserWarning, 1))
1719 with self.assertRaises(TypeError):
1720 self.assertWarns((UserWarning, object))
1721 with self.assertRaises(TypeError):
1722 self.assertWarns((UserWarning, Exception))
1723
1724 def testAssertWarnsRegexCallable(self):
1725 def _runtime_warn(msg):
1726 warnings.warn(msg, RuntimeWarning)
1727 self.assertWarnsRegex(RuntimeWarning, "o+",
1728 _runtime_warn, "foox")
1729 # Failure when no warning is triggered
1730 with self.assertRaises(self.failureException):
1731 self.assertWarnsRegex(RuntimeWarning, "o+",
1732 lambda: 0)
1733 # Failure when the function is None
1734 with self.assertRaises(TypeError):
1735 self.assertWarnsRegex(RuntimeWarning, "o+", None)
1736 # Failure when another warning is triggered
1737 with warnings.catch_warnings():
1738 # Force default filter (in case tests are run with -We)
1739 warnings.simplefilter("default", RuntimeWarning)
1740 with self.assertRaises(self.failureException):
1741 self.assertWarnsRegex(DeprecationWarning, "o+",
1742 _runtime_warn, "foox")
1743 # Failure when message doesn't match
1744 with self.assertRaises(self.failureException):
1745 self.assertWarnsRegex(RuntimeWarning, "o+",
1746 _runtime_warn, "barz")
1747 # A little trickier: we ask RuntimeWarnings to be raised, and then
1748 # check for some of them. It is implementation-defined whether
1749 # non-matching RuntimeWarnings are simply re-raised, or produce a
1750 # failureException.
1751 with warnings.catch_warnings():
1752 warnings.simplefilter("error", RuntimeWarning)
1753 with self.assertRaises((RuntimeWarning, self.failureException)):
1754 self.assertWarnsRegex(RuntimeWarning, "o+",
1755 _runtime_warn, "barz")
1756
1757 def testAssertWarnsRegexContext(self):
1758 # Same as above, but with assertWarnsRegex as a context manager
1759 def _runtime_warn(msg):
1760 warnings.warn(msg, RuntimeWarning)
1761 _runtime_warn_lineno = inspect.getsourcelines(_runtime_warn)[1]
1762 with self.assertWarnsRegex(RuntimeWarning, "o+") as cm:
1763 _runtime_warn("foox")
1764 self.assertIsInstance(cm.warning, RuntimeWarning)
1765 self.assertEqual(cm.warning.args[0], "foox")
1766 self.assertIn("test_case.py", cm.filename)
1767 self.assertEqual(cm.lineno, _runtime_warn_lineno + 1)
1768 # Failure when no warning is triggered
1769 with self.assertRaises(self.failureException):
1770 with self.assertWarnsRegex(RuntimeWarning, "o+"):
1771 pass
1772 # Custom message
1773 with self.assertRaisesRegex(self.failureException, 'foobar'):
1774 with self.assertWarnsRegex(RuntimeWarning, 'o+', msg='foobar'):
1775 pass
1776 # Invalid keyword argument
1777 with self.assertRaisesRegex(TypeError, 'foobar'):
1778 with self.assertWarnsRegex(RuntimeWarning, 'o+', foobar=42):
1779 pass
1780 # Failure when another warning is triggered
1781 with warnings.catch_warnings():
1782 # Force default filter (in case tests are run with -We)
1783 warnings.simplefilter("default", RuntimeWarning)
1784 with self.assertRaises(self.failureException):
1785 with self.assertWarnsRegex(DeprecationWarning, "o+"):
1786 _runtime_warn("foox")
1787 # Failure when message doesn't match
1788 with self.assertRaises(self.failureException):
1789 with self.assertWarnsRegex(RuntimeWarning, "o+"):
1790 _runtime_warn("barz")
1791 # A little trickier: we ask RuntimeWarnings to be raised, and then
1792 # check for some of them. It is implementation-defined whether
1793 # non-matching RuntimeWarnings are simply re-raised, or produce a
1794 # failureException.
1795 with warnings.catch_warnings():
1796 warnings.simplefilter("error", RuntimeWarning)
1797 with self.assertRaises((RuntimeWarning, self.failureException)):
1798 with self.assertWarnsRegex(RuntimeWarning, "o+"):
1799 _runtime_warn("barz")
1800
1801 def testAssertWarnsRegexNoExceptionType(self):
1802 with self.assertRaises(TypeError):
1803 self.assertWarnsRegex()
1804 with self.assertRaises(TypeError):
1805 self.assertWarnsRegex(UserWarning)
1806 with self.assertRaises(TypeError):
1807 self.assertWarnsRegex(1, 'expect')
1808 with self.assertRaises(TypeError):
1809 self.assertWarnsRegex(object, 'expect')
1810 with self.assertRaises(TypeError):
1811 self.assertWarnsRegex((UserWarning, 1), 'expect')
1812 with self.assertRaises(TypeError):
1813 self.assertWarnsRegex((UserWarning, object), 'expect')
1814 with self.assertRaises(TypeError):
1815 self.assertWarnsRegex((UserWarning, Exception), 'expect')
1816
1817 @contextlib.contextmanager
1818 def assertNoStderr(self):
1819 with captured_stderr() as buf:
1820 yield
1821 self.assertEqual(buf.getvalue(), "")
1822
1823 def assertLogRecords(self, records, matches):
1824 self.assertEqual(len(records), len(matches))
1825 for rec, match in zip(records, matches):
1826 self.assertIsInstance(rec, logging.LogRecord)
1827 for k, v in match.items():
1828 self.assertEqual(getattr(rec, k), v)
1829
1830 def testAssertLogsDefaults(self):
1831 # defaults: root logger, level INFO
1832 with self.assertNoStderr():
1833 with self.assertLogs() as cm:
1834 log_foo.info("1")
1835 log_foobar.debug("2")
1836 self.assertEqual(cm.output, ["INFO:foo:1"])
1837 self.assertLogRecords(cm.records, [{'name': 'foo'}])
1838
1839 def testAssertLogsTwoMatchingMessages(self):
1840 # Same, but with two matching log messages
1841 with self.assertNoStderr():
1842 with self.assertLogs() as cm:
1843 log_foo.info("1")
1844 log_foobar.debug("2")
1845 log_quux.warning("3")
1846 self.assertEqual(cm.output, ["INFO:foo:1", "WARNING:quux:3"])
1847 self.assertLogRecords(cm.records,
1848 [{'name': 'foo'}, {'name': 'quux'}])
1849
1850 def checkAssertLogsPerLevel(self, level):
1851 # Check level filtering
1852 with self.assertNoStderr():
1853 with self.assertLogs(level=level) as cm:
1854 log_foo.warning("1")
1855 log_foobar.error("2")
1856 log_quux.critical("3")
1857 self.assertEqual(cm.output, ["ERROR:foo.bar:2", "CRITICAL:quux:3"])
1858 self.assertLogRecords(cm.records,
1859 [{'name': 'foo.bar'}, {'name': 'quux'}])
1860
1861 def testAssertLogsPerLevel(self):
1862 self.checkAssertLogsPerLevel(logging.ERROR)
1863 self.checkAssertLogsPerLevel('ERROR')
1864
1865 def checkAssertLogsPerLogger(self, logger):
1866 # Check per-logger filtering
1867 with self.assertNoStderr():
1868 with self.assertLogs(level='DEBUG') as outer_cm:
1869 with self.assertLogs(logger, level='DEBUG') as cm:
1870 log_foo.info("1")
1871 log_foobar.debug("2")
1872 log_quux.warning("3")
1873 self.assertEqual(cm.output, ["INFO:foo:1", "DEBUG:foo.bar:2"])
1874 self.assertLogRecords(cm.records,
1875 [{'name': 'foo'}, {'name': 'foo.bar'}])
1876 # The outer catchall caught the quux log
1877 self.assertEqual(outer_cm.output, ["WARNING:quux:3"])
1878
1879 def testAssertLogsPerLogger(self):
1880 self.checkAssertLogsPerLogger(logging.getLogger('foo'))
1881 self.checkAssertLogsPerLogger('foo')
1882
1883 def testAssertLogsFailureNoLogs(self):
1884 # Failure due to no logs
1885 with self.assertNoStderr():
1886 with self.assertRaises(self.failureException):
1887 with self.assertLogs():
1888 pass
1889
1890 def testAssertLogsFailureLevelTooHigh(self):
1891 # Failure due to level too high
1892 with self.assertNoStderr():
1893 with self.assertRaises(self.failureException):
1894 with self.assertLogs(level='WARNING'):
1895 log_foo.info("1")
1896
1897 def testAssertLogsFailureLevelTooHigh_FilterInRootLogger(self):
1898 # Failure due to level too high - message propagated to root
1899 with self.assertNoStderr():
1900 oldLevel = log_foo.level
1901 log_foo.setLevel(logging.INFO)
1902 try:
1903 with self.assertRaises(self.failureException):
1904 with self.assertLogs(level='WARNING'):
1905 log_foo.info("1")
1906 finally:
1907 log_foo.setLevel(oldLevel)
1908
1909 def testAssertLogsFailureMismatchingLogger(self):
1910 # Failure due to mismatching logger (and the logged message is
1911 # passed through)
1912 with self.assertLogs('quux', level='ERROR'):
1913 with self.assertRaises(self.failureException):
1914 with self.assertLogs('foo'):
1915 log_quux.error("1")
1916
1917 def testAssertLogsUnexpectedException(self):
1918 # Check unexpected exception will go through.
1919 with self.assertRaises(ZeroDivisionError):
1920 with self.assertLogs():
1921 raise ZeroDivisionError("Unexpected")
1922
1923 def testAssertNoLogsDefault(self):
1924 with self.assertRaises(self.failureException) as cm:
1925 with self.assertNoLogs():
1926 log_foo.info("1")
1927 log_foobar.debug("2")
1928 self.assertEqual(
1929 str(cm.exception),
1930 "Unexpected logs found: ['INFO:foo:1']",
1931 )
1932
1933 def testAssertNoLogsFailureFoundLogs(self):
1934 with self.assertRaises(self.failureException) as cm:
1935 with self.assertNoLogs():
1936 log_quux.error("1")
1937 log_foo.error("foo")
1938
1939 self.assertEqual(
1940 str(cm.exception),
1941 "Unexpected logs found: ['ERROR:quux:1', 'ERROR:foo:foo']",
1942 )
1943
1944 def testAssertNoLogsPerLogger(self):
1945 with self.assertNoStderr():
1946 with self.assertLogs(log_quux):
1947 with self.assertNoLogs(logger=log_foo):
1948 log_quux.error("1")
1949
1950 def testAssertNoLogsFailurePerLogger(self):
1951 # Failure due to unexpected logs for the given logger or its
1952 # children.
1953 with self.assertRaises(self.failureException) as cm:
1954 with self.assertLogs(log_quux):
1955 with self.assertNoLogs(logger=log_foo):
1956 log_quux.error("1")
1957 log_foobar.info("2")
1958 self.assertEqual(
1959 str(cm.exception),
1960 "Unexpected logs found: ['INFO:foo.bar:2']",
1961 )
1962
1963 def testAssertNoLogsPerLevel(self):
1964 # Check per-level filtering
1965 with self.assertNoStderr():
1966 with self.assertNoLogs(level="ERROR"):
1967 log_foo.info("foo")
1968 log_quux.debug("1")
1969
1970 def testAssertNoLogsFailurePerLevel(self):
1971 # Failure due to unexpected logs at the specified level.
1972 with self.assertRaises(self.failureException) as cm:
1973 with self.assertNoLogs(level="DEBUG"):
1974 log_foo.debug("foo")
1975 log_quux.debug("1")
1976 self.assertEqual(
1977 str(cm.exception),
1978 "Unexpected logs found: ['DEBUG:foo:foo', 'DEBUG:quux:1']",
1979 )
1980
1981 def testAssertNoLogsUnexpectedException(self):
1982 # Check unexpected exception will go through.
1983 with self.assertRaises(ZeroDivisionError):
1984 with self.assertNoLogs():
1985 raise ZeroDivisionError("Unexpected")
1986
1987 def testAssertNoLogsYieldsNone(self):
1988 with self.assertNoLogs() as value:
1989 pass
1990 self.assertIsNone(value)
1991
1992 def testAssertStartsWith(self):
1993 self.assertStartsWith('ababahalamaha', 'ababa')
1994 self.assertStartsWith('ababahalamaha', ('x', 'ababa', 'y'))
1995 self.assertStartsWith(UserString('ababahalamaha'), 'ababa')
1996 self.assertStartsWith(UserString('ababahalamaha'), ('x', 'ababa', 'y'))
1997 self.assertStartsWith(bytearray(b'ababahalamaha'), b'ababa')
1998 self.assertStartsWith(bytearray(b'ababahalamaha'), (b'x', b'ababa', b'y'))
1999 self.assertStartsWith(b'ababahalamaha', bytearray(b'ababa'))
2000 self.assertStartsWith(b'ababahalamaha',
2001 (bytearray(b'x'), bytearray(b'ababa'), bytearray(b'y')))
2002
2003 with self.assertRaises(self.failureException) as cm:
2004 self.assertStartsWith('ababahalamaha', 'amaha')
2005 self.assertEqual(str(cm.exception),
2006 "'ababahalamaha' doesn't start with 'amaha'")
2007 with self.assertRaises(self.failureException) as cm:
2008 self.assertStartsWith('ababahalamaha', ('x', 'y'))
2009 self.assertEqual(str(cm.exception),
2010 "'ababahalamaha' doesn't start with any of ('x', 'y')")
2011
2012 with self.assertRaises(self.failureException) as cm:
2013 self.assertStartsWith(b'ababahalamaha', 'ababa')
2014 self.assertEqual(str(cm.exception), 'Expected str, not bytes')
2015 with self.assertRaises(self.failureException) as cm:
2016 self.assertStartsWith(b'ababahalamaha', ('amaha', 'ababa'))
2017 self.assertEqual(str(cm.exception), 'Expected str, not bytes')
2018 with self.assertRaises(self.failureException) as cm:
2019 self.assertStartsWith([], 'ababa')
2020 self.assertEqual(str(cm.exception), 'Expected str, not list')
2021 with self.assertRaises(self.failureException) as cm:
2022 self.assertStartsWith('ababahalamaha', b'ababa')
2023 self.assertEqual(str(cm.exception), 'Expected bytes, not str')
2024 with self.assertRaises(self.failureException) as cm:
2025 self.assertStartsWith('ababahalamaha', (b'amaha', b'ababa'))
2026 self.assertEqual(str(cm.exception), 'Expected bytes, not str')
2027 with self.assertRaises(TypeError):
2028 self.assertStartsWith('ababahalamaha', ord('a'))
2029
2030 with self.assertRaises(self.failureException) as cm:
2031 self.assertStartsWith('ababahalamaha', 'amaha', 'abracadabra')
2032 self.assertIn('ababahalamaha', str(cm.exception))
2033 with self.assertRaises(self.failureException) as cm:
2034 self.assertStartsWith('ababahalamaha', 'amaha', msg='abracadabra')
2035 self.assertIn('ababahalamaha', str(cm.exception))
2036
2037 def testAssertNotStartsWith(self):
2038 self.assertNotStartsWith('ababahalamaha', 'amaha')
2039 self.assertNotStartsWith('ababahalamaha', ('x', 'amaha', 'y'))
2040 self.assertNotStartsWith(UserString('ababahalamaha'), 'amaha')
2041 self.assertNotStartsWith(UserString('ababahalamaha'), ('x', 'amaha', 'y'))
2042 self.assertNotStartsWith(bytearray(b'ababahalamaha'), b'amaha')
2043 self.assertNotStartsWith(bytearray(b'ababahalamaha'), (b'x', b'amaha', b'y'))
2044 self.assertNotStartsWith(b'ababahalamaha', bytearray(b'amaha'))
2045 self.assertNotStartsWith(b'ababahalamaha',
2046 (bytearray(b'x'), bytearray(b'amaha'), bytearray(b'y')))
2047
2048 with self.assertRaises(self.failureException) as cm:
2049 self.assertNotStartsWith('ababahalamaha', 'ababa')
2050 self.assertEqual(str(cm.exception),
2051 "'ababahalamaha' starts with 'ababa'")
2052 with self.assertRaises(self.failureException) as cm:
2053 self.assertNotStartsWith('ababahalamaha', ('x', 'ababa', 'y'))
2054 self.assertEqual(str(cm.exception),
2055 "'ababahalamaha' starts with 'ababa'")
2056
2057 with self.assertRaises(self.failureException) as cm:
2058 self.assertNotStartsWith(b'ababahalamaha', 'ababa')
2059 self.assertEqual(str(cm.exception), 'Expected str, not bytes')
2060 with self.assertRaises(self.failureException) as cm:
2061 self.assertNotStartsWith(b'ababahalamaha', ('amaha', 'ababa'))
2062 self.assertEqual(str(cm.exception), 'Expected str, not bytes')
2063 with self.assertRaises(self.failureException) as cm:
2064 self.assertNotStartsWith([], 'ababa')
2065 self.assertEqual(str(cm.exception), 'Expected str, not list')
2066 with self.assertRaises(self.failureException) as cm:
2067 self.assertNotStartsWith('ababahalamaha', b'ababa')
2068 self.assertEqual(str(cm.exception), 'Expected bytes, not str')
2069 with self.assertRaises(self.failureException) as cm:
2070 self.assertNotStartsWith('ababahalamaha', (b'amaha', b'ababa'))
2071 self.assertEqual(str(cm.exception), 'Expected bytes, not str')
2072 with self.assertRaises(TypeError):
2073 self.assertNotStartsWith('ababahalamaha', ord('a'))
2074
2075 with self.assertRaises(self.failureException) as cm:
2076 self.assertNotStartsWith('ababahalamaha', 'ababa', 'abracadabra')
2077 self.assertIn('ababahalamaha', str(cm.exception))
2078 with self.assertRaises(self.failureException) as cm:
2079 self.assertNotStartsWith('ababahalamaha', 'ababa', msg='abracadabra')
2080 self.assertIn('ababahalamaha', str(cm.exception))
2081
2082 def testAssertEndsWith(self):
2083 self.assertEndsWith('ababahalamaha', 'amaha')
2084 self.assertEndsWith('ababahalamaha', ('x', 'amaha', 'y'))
2085 self.assertEndsWith(UserString('ababahalamaha'), 'amaha')
2086 self.assertEndsWith(UserString('ababahalamaha'), ('x', 'amaha', 'y'))
2087 self.assertEndsWith(bytearray(b'ababahalamaha'), b'amaha')
2088 self.assertEndsWith(bytearray(b'ababahalamaha'), (b'x', b'amaha', b'y'))
2089 self.assertEndsWith(b'ababahalamaha', bytearray(b'amaha'))
2090 self.assertEndsWith(b'ababahalamaha',
2091 (bytearray(b'x'), bytearray(b'amaha'), bytearray(b'y')))
2092
2093 with self.assertRaises(self.failureException) as cm:
2094 self.assertEndsWith('ababahalamaha', 'ababa')
2095 self.assertEqual(str(cm.exception),
2096 "'ababahalamaha' doesn't end with 'ababa'")
2097 with self.assertRaises(self.failureException) as cm:
2098 self.assertEndsWith('ababahalamaha', ('x', 'y'))
2099 self.assertEqual(str(cm.exception),
2100 "'ababahalamaha' doesn't end with any of ('x', 'y')")
2101
2102 with self.assertRaises(self.failureException) as cm:
2103 self.assertEndsWith(b'ababahalamaha', 'amaha')
2104 self.assertEqual(str(cm.exception), 'Expected str, not bytes')
2105 with self.assertRaises(self.failureException) as cm:
2106 self.assertEndsWith(b'ababahalamaha', ('ababa', 'amaha'))
2107 self.assertEqual(str(cm.exception), 'Expected str, not bytes')
2108 with self.assertRaises(self.failureException) as cm:
2109 self.assertEndsWith([], 'amaha')
2110 self.assertEqual(str(cm.exception), 'Expected str, not list')
2111 with self.assertRaises(self.failureException) as cm:
2112 self.assertEndsWith('ababahalamaha', b'amaha')
2113 self.assertEqual(str(cm.exception), 'Expected bytes, not str')
2114 with self.assertRaises(self.failureException) as cm:
2115 self.assertEndsWith('ababahalamaha', (b'ababa', b'amaha'))
2116 self.assertEqual(str(cm.exception), 'Expected bytes, not str')
2117 with self.assertRaises(TypeError):
2118 self.assertEndsWith('ababahalamaha', ord('a'))
2119
2120 with self.assertRaises(self.failureException) as cm:
2121 self.assertEndsWith('ababahalamaha', 'ababa', 'abracadabra')
2122 self.assertIn('ababahalamaha', str(cm.exception))
2123 with self.assertRaises(self.failureException) as cm:
2124 self.assertEndsWith('ababahalamaha', 'ababa', msg='abracadabra')
2125 self.assertIn('ababahalamaha', str(cm.exception))
2126
2127 def testAssertNotEndsWith(self):
2128 self.assertNotEndsWith('ababahalamaha', 'ababa')
2129 self.assertNotEndsWith('ababahalamaha', ('x', 'ababa', 'y'))
2130 self.assertNotEndsWith(UserString('ababahalamaha'), 'ababa')
2131 self.assertNotEndsWith(UserString('ababahalamaha'), ('x', 'ababa', 'y'))
2132 self.assertNotEndsWith(bytearray(b'ababahalamaha'), b'ababa')
2133 self.assertNotEndsWith(bytearray(b'ababahalamaha'), (b'x', b'ababa', b'y'))
2134 self.assertNotEndsWith(b'ababahalamaha', bytearray(b'ababa'))
2135 self.assertNotEndsWith(b'ababahalamaha',
2136 (bytearray(b'x'), bytearray(b'ababa'), bytearray(b'y')))
2137
2138 with self.assertRaises(self.failureException) as cm:
2139 self.assertNotEndsWith('ababahalamaha', 'amaha')
2140 self.assertEqual(str(cm.exception),
2141 "'ababahalamaha' ends with 'amaha'")
2142 with self.assertRaises(self.failureException) as cm:
2143 self.assertNotEndsWith('ababahalamaha', ('x', 'amaha', 'y'))
2144 self.assertEqual(str(cm.exception),
2145 "'ababahalamaha' ends with 'amaha'")
2146
2147 with self.assertRaises(self.failureException) as cm:
2148 self.assertNotEndsWith(b'ababahalamaha', 'amaha')
2149 self.assertEqual(str(cm.exception), 'Expected str, not bytes')
2150 with self.assertRaises(self.failureException) as cm:
2151 self.assertNotEndsWith(b'ababahalamaha', ('ababa', 'amaha'))
2152 self.assertEqual(str(cm.exception), 'Expected str, not bytes')
2153 with self.assertRaises(self.failureException) as cm:
2154 self.assertNotEndsWith([], 'amaha')
2155 self.assertEqual(str(cm.exception), 'Expected str, not list')
2156 with self.assertRaises(self.failureException) as cm:
2157 self.assertNotEndsWith('ababahalamaha', b'amaha')
2158 self.assertEqual(str(cm.exception), 'Expected bytes, not str')
2159 with self.assertRaises(self.failureException) as cm:
2160 self.assertNotEndsWith('ababahalamaha', (b'ababa', b'amaha'))
2161 self.assertEqual(str(cm.exception), 'Expected bytes, not str')
2162 with self.assertRaises(TypeError):
2163 self.assertNotEndsWith('ababahalamaha', ord('a'))
2164
2165 with self.assertRaises(self.failureException) as cm:
2166 self.assertNotEndsWith('ababahalamaha', 'amaha', 'abracadabra')
2167 self.assertIn('ababahalamaha', str(cm.exception))
2168 with self.assertRaises(self.failureException) as cm:
2169 self.assertNotEndsWith('ababahalamaha', 'amaha', msg='abracadabra')
2170 self.assertIn('ababahalamaha', str(cm.exception))
2171
2172 def testDeprecatedFailMethods(self):
2173 """Test that the deprecated fail* methods get removed in 3.12"""
2174 deprecated_names = [
2175 'failIfEqual', 'failUnlessEqual', 'failUnlessAlmostEqual',
2176 'failIfAlmostEqual', 'failUnless', 'failUnlessRaises', 'failIf',
2177 'assertNotEquals', 'assertEquals', 'assertAlmostEquals',
2178 'assertNotAlmostEquals', 'assert_', 'assertDictContainsSubset',
2179 'assertRaisesRegexp', 'assertRegexpMatches'
2180 ]
2181 for deprecated_name in deprecated_names:
2182 with self.assertRaises(AttributeError):
2183 getattr(self, deprecated_name)
2184
2185 def testDeepcopy(self):
2186 # Issue: 5660
2187 class TestableTest(unittest.TestCase):
2188 def testNothing(self):
2189 pass
2190
2191 test = TestableTest('testNothing')
2192
2193 # This shouldn't blow up
2194 deepcopy(test)
2195
2196 def testPickle(self):
2197 # Issue 10326
2198
2199 # Can't use TestCase classes defined in Test class as
2200 # pickle does not work with inner classes
2201 test = unittest.TestCase('run')
2202 for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
2203
2204 # blew up prior to fix
2205 pickled_test = pickle.dumps(test, protocol=protocol)
2206 unpickled_test = pickle.loads(pickled_test)
2207 self.assertEqual(test, unpickled_test)
2208
2209 # exercise the TestCase instance in a way that will invoke
2210 # the type equality lookup mechanism
2211 unpickled_test.assertEqual(set(), set())
2212
2213 def testKeyboardInterrupt(self):
2214 def _raise(self=None):
2215 raise KeyboardInterrupt
2216 def nothing(self):
2217 pass
2218
2219 class Test1(unittest.TestCase):
2220 test_something = _raise
2221
2222 class Test2(unittest.TestCase):
2223 setUp = _raise
2224 test_something = nothing
2225
2226 class Test3(unittest.TestCase):
2227 test_something = nothing
2228 tearDown = _raise
2229
2230 class Test4(unittest.TestCase):
2231 def test_something(self):
2232 self.addCleanup(_raise)
2233
2234 for klass in (Test1, Test2, Test3, Test4):
2235 with self.assertRaises(KeyboardInterrupt):
2236 klass('test_something').run()
2237
2238 def testSkippingEverywhere(self):
2239 def _skip(self=None):
2240 raise unittest.SkipTest('some reason')
2241 def nothing(self):
2242 pass
2243
2244 class Test1(unittest.TestCase):
2245 test_something = _skip
2246
2247 class Test2(unittest.TestCase):
2248 setUp = _skip
2249 test_something = nothing
2250
2251 class Test3(unittest.TestCase):
2252 test_something = nothing
2253 tearDown = _skip
2254
2255 class Test4(unittest.TestCase):
2256 def test_something(self):
2257 self.addCleanup(_skip)
2258
2259 for klass in (Test1, Test2, Test3, Test4):
2260 result = unittest.TestResult()
2261 klass('test_something').run(result)
2262 self.assertEqual(len(result.skipped), 1)
2263 self.assertEqual(result.testsRun, 1)
2264
2265 def testSystemExit(self):
2266 def _raise(self=None):
2267 raise SystemExit
2268 def nothing(self):
2269 pass
2270
2271 class Test1(unittest.TestCase):
2272 test_something = _raise
2273
2274 class Test2(unittest.TestCase):
2275 setUp = _raise
2276 test_something = nothing
2277
2278 class Test3(unittest.TestCase):
2279 test_something = nothing
2280 tearDown = _raise
2281
2282 class Test4(unittest.TestCase):
2283 def test_something(self):
2284 self.addCleanup(_raise)
2285
2286 for klass in (Test1, Test2, Test3, Test4):
2287 result = unittest.TestResult()
2288 klass('test_something').run(result)
2289 self.assertEqual(len(result.errors), 1)
2290 self.assertEqual(result.testsRun, 1)
2291
2292 @support.cpython_only
2293 def testNoCycles(self):
2294 case = unittest.TestCase()
2295 wr = weakref.ref(case)
2296 with support.disable_gc():
2297 del case
2298 self.assertFalse(wr())
2299
2300 def test_no_exception_leak(self):
2301 # Issue #19880: TestCase.run() should not keep a reference
2302 # to the exception
2303 class MyException(Exception):
2304 ninstance = 0
2305
2306 def __init__(self):
2307 MyException.ninstance += 1
2308 Exception.__init__(self)
2309
2310 def __del__(self):
2311 MyException.ninstance -= 1
2312
2313 class TestCase(unittest.TestCase):
2314 def test1(self):
2315 raise MyException()
2316
2317 @unittest.expectedFailure
2318 def test2(self):
2319 raise MyException()
2320
2321 for method_name in ('test1', 'test2'):
2322 testcase = TestCase(method_name)
2323 testcase.run()
2324 gc_collect() # For PyPy or other GCs.
2325 self.assertEqual(MyException.ninstance, 0)
2326
2327
2328 if __name__ == "__main__":
2329 unittest.main()