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