6 from copy
import deepcopy
7 from test
import test_support
11 from .support
import (
12 TestEquality
, TestHashing
, LoggingResult
, ResultWithNoStartTestRunStopTestRun
17 "Keep these TestCase classes out of the main namespace"
19 class Foo(unittest
.TestCase
):
20 def runTest(self
): pass
26 class LoggingTestCase(unittest
.TestCase
):
27 """A test case which logs its calls."""
29 def __init__(self
, events
):
30 super(Test
.LoggingTestCase
, self
).__init
__('test')
34 self
.events
.append('setUp')
37 self
.events
.append('test')
40 self
.events
.append('tearDown')
43 class Test_TestCase(unittest
.TestCase
, TestEquality
, TestHashing
):
45 ### Set up attributes used by inherited tests
46 ################################################################
48 # Used by TestHashing.test_hash and TestEquality.test_eq
49 eq_pairs
= [(Test
.Foo('test1'), Test
.Foo('test1'))]
51 # Used by TestEquality.test_ne
52 ne_pairs
= [(Test
.Foo('test1'), Test
.Foo('runTest'))
53 ,(Test
.Foo('test1'), Test
.Bar('test1'))
54 ,(Test
.Foo('test1'), Test
.Bar('test2'))]
56 ################################################################
57 ### /Set up attributes used by inherited tests
60 # "class TestCase([methodName])"
62 # "Each instance of TestCase will run a single test method: the
63 # method named methodName."
65 # "methodName defaults to "runTest"."
67 # Make sure it really is optional, and that it defaults to the proper
69 def test_init__no_test_name(self
):
70 class Test(unittest
.TestCase
):
71 def runTest(self
): raise TypeError()
74 self
.assertEqual(Test().id()[-13:], '.Test.runTest')
76 # "class TestCase([methodName])"
78 # "Each instance of TestCase will run a single test method: the
79 # method named methodName."
80 def test_init__test_name__valid(self
):
81 class Test(unittest
.TestCase
):
82 def runTest(self
): raise TypeError()
85 self
.assertEqual(Test('test').id()[-10:], '.Test.test')
87 # "class TestCase([methodName])"
89 # "Each instance of TestCase will run a single test method: the
90 # method named methodName."
91 def test_init__test_name__invalid(self
):
92 class Test(unittest
.TestCase
):
93 def runTest(self
): raise TypeError()
101 self
.fail("Failed to raise ValueError")
103 # "Return the number of tests represented by the this test object. For
104 # TestCase instances, this will always be 1"
105 def test_countTestCases(self
):
106 class Foo(unittest
.TestCase
):
109 self
.assertEqual(Foo('test').countTestCases(), 1)
111 # "Return the default type of test result object to be used to run this
112 # test. For TestCase instances, this will always be
113 # unittest.TestResult; subclasses of TestCase should
114 # override this as necessary."
115 def test_defaultTestResult(self
):
116 class Foo(unittest
.TestCase
):
120 result
= Foo().defaultTestResult()
121 self
.assertEqual(type(result
), unittest
.TestResult
)
123 # "When a setUp() method is defined, the test runner will run that method
124 # prior to each test. Likewise, if a tearDown() method is defined, the
125 # test runner will invoke that method after each test. In the example,
126 # setUp() was used to create a fresh sequence for each test."
128 # Make sure the proper call order is maintained, even if setUp() raises
130 def test_run_call_order__error_in_setUp(self
):
132 result
= LoggingResult(events
)
134 class Foo(Test
.LoggingTestCase
):
136 super(Foo
, self
).setUp()
137 raise RuntimeError('raised by Foo.setUp')
139 Foo(events
).run(result
)
140 expected
= ['startTest', 'setUp', 'addError', 'stopTest']
141 self
.assertEqual(events
, expected
)
143 # "With a temporary result stopTestRun is called when setUp errors.
144 def test_run_call_order__error_in_setUp_default_result(self
):
147 class Foo(Test
.LoggingTestCase
):
148 def defaultTestResult(self
):
149 return LoggingResult(self
.events
)
152 super(Foo
, self
).setUp()
153 raise RuntimeError('raised by Foo.setUp')
156 expected
= ['startTestRun', 'startTest', 'setUp', 'addError',
157 'stopTest', 'stopTestRun']
158 self
.assertEqual(events
, expected
)
160 # "When a setUp() method is defined, the test runner will run that method
161 # prior to each test. Likewise, if a tearDown() method is defined, the
162 # test runner will invoke that method after each test. In the example,
163 # setUp() was used to create a fresh sequence for each test."
165 # Make sure the proper call order is maintained, even if the test raises
166 # an error (as opposed to a failure).
167 def test_run_call_order__error_in_test(self
):
169 result
= LoggingResult(events
)
171 class Foo(Test
.LoggingTestCase
):
173 super(Foo
, self
).test()
174 raise RuntimeError('raised by Foo.test')
176 expected
= ['startTest', 'setUp', 'test', 'addError', 'tearDown',
178 Foo(events
).run(result
)
179 self
.assertEqual(events
, expected
)
181 # "With a default result, an error in the test still results in stopTestRun
183 def test_run_call_order__error_in_test_default_result(self
):
186 class Foo(Test
.LoggingTestCase
):
187 def defaultTestResult(self
):
188 return LoggingResult(self
.events
)
191 super(Foo
, self
).test()
192 raise RuntimeError('raised by Foo.test')
194 expected
= ['startTestRun', 'startTest', 'setUp', 'test', 'addError',
195 'tearDown', 'stopTest', 'stopTestRun']
197 self
.assertEqual(events
, expected
)
199 # "When a setUp() method is defined, the test runner will run that method
200 # prior to each test. Likewise, if a tearDown() method is defined, the
201 # test runner will invoke that method after each test. In the example,
202 # setUp() was used to create a fresh sequence for each test."
204 # Make sure the proper call order is maintained, even if the test signals
205 # a failure (as opposed to an error).
206 def test_run_call_order__failure_in_test(self
):
208 result
= LoggingResult(events
)
210 class Foo(Test
.LoggingTestCase
):
212 super(Foo
, self
).test()
213 self
.fail('raised by Foo.test')
215 expected
= ['startTest', 'setUp', 'test', 'addFailure', 'tearDown',
217 Foo(events
).run(result
)
218 self
.assertEqual(events
, expected
)
220 # "When a test fails with a default result stopTestRun is still called."
221 def test_run_call_order__failure_in_test_default_result(self
):
223 class Foo(Test
.LoggingTestCase
):
224 def defaultTestResult(self
):
225 return LoggingResult(self
.events
)
227 super(Foo
, self
).test()
228 self
.fail('raised by Foo.test')
230 expected
= ['startTestRun', 'startTest', 'setUp', 'test', 'addFailure',
231 'tearDown', 'stopTest', 'stopTestRun']
234 self
.assertEqual(events
, expected
)
236 # "When a setUp() method is defined, the test runner will run that method
237 # prior to each test. Likewise, if a tearDown() method is defined, the
238 # test runner will invoke that method after each test. In the example,
239 # setUp() was used to create a fresh sequence for each test."
241 # Make sure the proper call order is maintained, even if tearDown() raises
243 def test_run_call_order__error_in_tearDown(self
):
245 result
= LoggingResult(events
)
247 class Foo(Test
.LoggingTestCase
):
249 super(Foo
, self
).tearDown()
250 raise RuntimeError('raised by Foo.tearDown')
252 Foo(events
).run(result
)
253 expected
= ['startTest', 'setUp', 'test', 'tearDown', 'addError',
255 self
.assertEqual(events
, expected
)
257 # "When tearDown errors with a default result stopTestRun is still called."
258 def test_run_call_order__error_in_tearDown_default_result(self
):
260 class Foo(Test
.LoggingTestCase
):
261 def defaultTestResult(self
):
262 return LoggingResult(self
.events
)
264 super(Foo
, self
).tearDown()
265 raise RuntimeError('raised by Foo.tearDown')
269 expected
= ['startTestRun', 'startTest', 'setUp', 'test', 'tearDown',
270 'addError', 'stopTest', 'stopTestRun']
271 self
.assertEqual(events
, expected
)
273 # "TestCase.run() still works when the defaultTestResult is a TestResult
274 # that does not support startTestRun and stopTestRun.
275 def test_run_call_order_default_result(self
):
277 class Foo(unittest
.TestCase
):
278 def defaultTestResult(self
):
279 return ResultWithNoStartTestRunStopTestRun()
285 # "This class attribute gives the exception raised by the test() method.
286 # If a test framework needs to use a specialized exception, possibly to
287 # carry additional information, it must subclass this exception in
288 # order to ``play fair'' with the framework. The initial value of this
289 # attribute is AssertionError"
290 def test_failureException__default(self
):
291 class Foo(unittest
.TestCase
):
295 self
.assertTrue(Foo('test').failureException
is AssertionError)
297 # "This class attribute gives the exception raised by the test() method.
298 # If a test framework needs to use a specialized exception, possibly to
299 # carry additional information, it must subclass this exception in
300 # order to ``play fair'' with the framework."
302 # Make sure TestCase.run() respects the designated failureException
303 def test_failureException__subclassing__explicit_raise(self
):
305 result
= LoggingResult(events
)
307 class Foo(unittest
.TestCase
):
311 failureException
= RuntimeError
313 self
.assertTrue(Foo('test').failureException
is RuntimeError)
316 Foo('test').run(result
)
317 expected
= ['startTest', 'addFailure', 'stopTest']
318 self
.assertEqual(events
, expected
)
320 # "This class attribute gives the exception raised by the test() method.
321 # If a test framework needs to use a specialized exception, possibly to
322 # carry additional information, it must subclass this exception in
323 # order to ``play fair'' with the framework."
325 # Make sure TestCase.run() respects the designated failureException
326 def test_failureException__subclassing__implicit_raise(self
):
328 result
= LoggingResult(events
)
330 class Foo(unittest
.TestCase
):
334 failureException
= RuntimeError
336 self
.assertTrue(Foo('test').failureException
is RuntimeError)
339 Foo('test').run(result
)
340 expected
= ['startTest', 'addFailure', 'stopTest']
341 self
.assertEqual(events
, expected
)
343 # "The default implementation does nothing."
344 def test_setUp(self
):
345 class Foo(unittest
.TestCase
):
349 # ... and nothing should happen
352 # "The default implementation does nothing."
353 def test_tearDown(self
):
354 class Foo(unittest
.TestCase
):
358 # ... and nothing should happen
361 # "Return a string identifying the specific test case."
363 # Because of the vague nature of the docs, I'm not going to lock this
364 # test down too much. Really all that can be asserted is that the id()
365 # will be a string (either 8-byte or unicode -- again, because the docs
368 class Foo(unittest
.TestCase
):
372 self
.assertIsInstance(Foo().id(), basestring
)
374 # "If result is omitted or None, a temporary result object is created
375 # and used, but is not made available to the caller. As TestCase owns the
376 # temporary result startTestRun and stopTestRun are called.
378 def test_run__uses_defaultTestResult(self
):
381 class Foo(unittest
.TestCase
):
383 events
.append('test')
385 def defaultTestResult(self
):
386 return LoggingResult(events
)
388 # Make run() find a result object on its own
391 expected
= ['startTestRun', 'startTest', 'test', 'addSuccess',
392 'stopTest', 'stopTestRun']
393 self
.assertEqual(events
, expected
)
395 def testShortDescriptionWithoutDocstring(self
):
396 self
.assertIsNone(self
.shortDescription())
398 @unittest.skipIf(sys
.flags
.optimize
>= 2,
399 "Docstrings are omitted with -O2 and above")
400 def testShortDescriptionWithOneLineDocstring(self
):
401 """Tests shortDescription() for a method with a docstring."""
403 self
.shortDescription(),
404 'Tests shortDescription() for a method with a docstring.')
406 @unittest.skipIf(sys
.flags
.optimize
>= 2,
407 "Docstrings are omitted with -O2 and above")
408 def testShortDescriptionWithMultiLineDocstring(self
):
409 """Tests shortDescription() for a method with a longer docstring.
411 This method ensures that only the first line of a docstring is
412 returned used in the short description, no matter how long the
416 self
.shortDescription(),
417 'Tests shortDescription() for a method with a longer '
420 def testAddTypeEqualityFunc(self
):
421 class SadSnake(object):
422 """Dummy class for test_addTypeEqualityFunc."""
423 s1
, s2
= SadSnake(), SadSnake()
424 self
.assertNotEqual(s1
, s2
)
425 def AllSnakesCreatedEqual(a
, b
, msg
=None):
426 return type(a
) is type(b
) is SadSnake
427 self
.addTypeEqualityFunc(SadSnake
, AllSnakesCreatedEqual
)
428 self
.assertEqual(s1
, s2
)
429 # No this doesn't clean up and remove the SadSnake equality func
430 # from this TestCase instance but since its a local nothing else
431 # will ever notice that.
433 def testAssertIs(self
):
435 self
.assertIs(thing
, thing
)
436 self
.assertRaises(self
.failureException
, self
.assertIs
, thing
, object())
438 def testAssertIsNot(self
):
440 self
.assertIsNot(thing
, object())
441 self
.assertRaises(self
.failureException
, self
.assertIsNot
, thing
, thing
)
443 def testAssertIsInstance(self
):
445 self
.assertIsInstance(thing
, list)
446 self
.assertRaises(self
.failureException
, self
.assertIsInstance
,
449 def testAssertNotIsInstance(self
):
451 self
.assertNotIsInstance(thing
, dict)
452 self
.assertRaises(self
.failureException
, self
.assertNotIsInstance
,
455 def testAssertIn(self
):
456 animals
= {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'}
458 self
.assertIn('a', 'abc')
459 self
.assertIn(2, [1, 2, 3])
460 self
.assertIn('monkey', animals
)
462 self
.assertNotIn('d', 'abc')
463 self
.assertNotIn(0, [1, 2, 3])
464 self
.assertNotIn('otter', animals
)
466 self
.assertRaises(self
.failureException
, self
.assertIn
, 'x', 'abc')
467 self
.assertRaises(self
.failureException
, self
.assertIn
, 4, [1, 2, 3])
468 self
.assertRaises(self
.failureException
, self
.assertIn
, 'elephant',
471 self
.assertRaises(self
.failureException
, self
.assertNotIn
, 'c', 'abc')
472 self
.assertRaises(self
.failureException
, self
.assertNotIn
, 1, [1, 2, 3])
473 self
.assertRaises(self
.failureException
, self
.assertNotIn
, 'cow',
476 def testAssertDictContainsSubset(self
):
477 self
.assertDictContainsSubset({}, {})
478 self
.assertDictContainsSubset({}, {'a': 1})
479 self
.assertDictContainsSubset({'a': 1}, {'a': 1})
480 self
.assertDictContainsSubset({'a': 1}, {'a': 1, 'b': 2})
481 self
.assertDictContainsSubset({'a': 1, 'b': 2}, {'a': 1, 'b': 2})
483 with self
.assertRaises(self
.failureException
):
484 self
.assertDictContainsSubset({1: "one"}, {})
486 with self
.assertRaises(self
.failureException
):
487 self
.assertDictContainsSubset({'a': 2}, {'a': 1})
489 with self
.assertRaises(self
.failureException
):
490 self
.assertDictContainsSubset({'c': 1}, {'a': 1})
492 with self
.assertRaises(self
.failureException
):
493 self
.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1})
495 with self
.assertRaises(self
.failureException
):
496 self
.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1})
498 with test_support
.check_warnings(("", UnicodeWarning
)):
499 one
= ''.join(chr(i
) for i
in range(255))
500 # this used to cause a UnicodeDecodeError constructing the failure msg
501 with self
.assertRaises(self
.failureException
):
502 self
.assertDictContainsSubset({'foo': one
}, {'foo': u
'\uFFFD'})
504 def testAssertEqual(self
):
510 (frozenset(), frozenset())]
511 for a
, b
in equal_pairs
:
512 # This mess of try excepts is to test the assertEqual behavior
515 self
.assertEqual(a
, b
)
516 except self
.failureException
:
517 self
.fail('assertEqual(%r, %r) failed' % (a
, b
))
519 self
.assertEqual(a
, b
, msg
='foo')
520 except self
.failureException
:
521 self
.fail('assertEqual(%r, %r) with msg= failed' % (a
, b
))
523 self
.assertEqual(a
, b
, 'foo')
524 except self
.failureException
:
525 self
.fail('assertEqual(%r, %r) with third parameter failed' %
531 (set([4,1]), frozenset([4,2])),
532 (frozenset([4,5]), set([2,3])),
533 (set([3,4]), set([5,4]))]
534 for a
, b
in unequal_pairs
:
535 self
.assertRaises(self
.failureException
, self
.assertEqual
, a
, b
)
536 self
.assertRaises(self
.failureException
, self
.assertEqual
, a
, b
,
538 self
.assertRaises(self
.failureException
, self
.assertEqual
, a
, b
,
541 def testEquality(self
):
542 self
.assertListEqual([], [])
543 self
.assertTupleEqual((), ())
544 self
.assertSequenceEqual([], ())
548 self
.assertRaises(unittest
.TestCase
.failureException
,
549 self
.assertListEqual
, a
, b
)
550 self
.assertRaises(unittest
.TestCase
.failureException
,
551 self
.assertListEqual
, tuple(a
), tuple(b
))
552 self
.assertRaises(unittest
.TestCase
.failureException
,
553 self
.assertSequenceEqual
, a
, tuple(b
))
556 self
.assertListEqual(a
, b
)
557 self
.assertTupleEqual(tuple(a
), tuple(b
))
558 self
.assertSequenceEqual(a
, tuple(b
))
559 self
.assertSequenceEqual(tuple(a
), b
)
561 self
.assertRaises(self
.failureException
, self
.assertListEqual
,
563 self
.assertRaises(self
.failureException
, self
.assertTupleEqual
,
565 self
.assertRaises(self
.failureException
, self
.assertListEqual
, None, b
)
566 self
.assertRaises(self
.failureException
, self
.assertTupleEqual
, None,
568 self
.assertRaises(self
.failureException
, self
.assertSequenceEqual
,
570 self
.assertRaises(self
.failureException
, self
.assertListEqual
, 1, 1)
571 self
.assertRaises(self
.failureException
, self
.assertTupleEqual
, 1, 1)
572 self
.assertRaises(self
.failureException
, self
.assertSequenceEqual
,
575 self
.assertDictEqual({}, {})
579 self
.assertRaises(unittest
.TestCase
.failureException
,
580 self
.assertDictEqual
, c
, d
)
583 self
.assertDictEqual(c
, d
)
586 self
.assertRaises(unittest
.TestCase
.failureException
,
587 self
.assertDictEqual
, c
, d
, 'These are unequal')
589 self
.assertRaises(self
.failureException
, self
.assertDictEqual
, None, d
)
590 self
.assertRaises(self
.failureException
, self
.assertDictEqual
, [], d
)
591 self
.assertRaises(self
.failureException
, self
.assertDictEqual
, 1, 1)
593 def testAssertSequenceEqualMaxDiff(self
):
594 self
.assertEqual(self
.maxDiff
, 80*8)
595 seq1
= 'a' + 'x' * 80**2
596 seq2
= 'b' + 'x' * 80**2
597 diff
= '\n'.join(difflib
.ndiff(pprint
.pformat(seq1
).splitlines(),
598 pprint
.pformat(seq2
).splitlines()))
599 # the +1 is the leading \n added by assertSequenceEqual
600 omitted
= unittest
.case
.DIFF_OMITTED
% (len(diff
) + 1,)
602 self
.maxDiff
= len(diff
)//2
604 self
.assertSequenceEqual(seq1
, seq2
)
605 except self
.failureException
as e
:
608 self
.fail('assertSequenceEqual did not fail.')
609 self
.assertTrue(len(msg
) < len(diff
))
610 self
.assertIn(omitted
, msg
)
612 self
.maxDiff
= len(diff
) * 2
614 self
.assertSequenceEqual(seq1
, seq2
)
615 except self
.failureException
as e
:
618 self
.fail('assertSequenceEqual did not fail.')
619 self
.assertTrue(len(msg
) > len(diff
))
620 self
.assertNotIn(omitted
, msg
)
624 self
.assertSequenceEqual(seq1
, seq2
)
625 except self
.failureException
as e
:
628 self
.fail('assertSequenceEqual did not fail.')
629 self
.assertTrue(len(msg
) > len(diff
))
630 self
.assertNotIn(omitted
, msg
)
632 def testTruncateMessage(self
):
634 message
= self
._truncateMessage
('foo', 'bar')
635 omitted
= unittest
.case
.DIFF_OMITTED
% len('bar')
636 self
.assertEqual(message
, 'foo' + omitted
)
639 message
= self
._truncateMessage
('foo', 'bar')
640 self
.assertEqual(message
, 'foobar')
643 message
= self
._truncateMessage
('foo', 'bar')
644 self
.assertEqual(message
, 'foobar')
646 def testAssertDictEqualTruncates(self
):
647 test
= unittest
.TestCase('assertEqual')
648 def truncate(msg
, diff
):
650 test
._truncateMessage
= truncate
652 test
.assertDictEqual({}, {1: 0})
653 except self
.failureException
as e
:
654 self
.assertEqual(str(e
), 'foo')
656 self
.fail('assertDictEqual did not fail')
658 def testAssertMultiLineEqualTruncates(self
):
659 test
= unittest
.TestCase('assertEqual')
660 def truncate(msg
, diff
):
662 test
._truncateMessage
= truncate
664 test
.assertMultiLineEqual('foo', 'bar')
665 except self
.failureException
as e
:
666 self
.assertEqual(str(e
), 'foo')
668 self
.fail('assertMultiLineEqual did not fail')
670 def testAssertEqual_diffThreshold(self
):
671 # check threshold value
672 self
.assertEqual(self
._diffThreshold
, 2**16)
673 # disable madDiff to get diff markers
676 # set a lower threshold value and add a cleanup to restore it
677 old_threshold
= self
._diffThreshold
678 self
._diffThreshold
= 2**8
679 self
.addCleanup(lambda: setattr(self
, '_diffThreshold', old_threshold
))
681 # under the threshold: diff marker (^) in error message
683 with self
.assertRaises(self
.failureException
) as cm
:
684 self
.assertEqual(s
+ 'a', s
+ 'b')
685 self
.assertIn('^', str(cm
.exception
))
686 self
.assertEqual(s
+ 'a', s
+ 'a')
688 # over the threshold: diff not used and marker (^) not in error message
690 # if the path that uses difflib is taken, _truncateMessage will be
691 # called -- replace it with explodingTruncation to verify that this
693 def explodingTruncation(message
, diff
):
694 raise SystemError('this should not be raised')
695 old_truncate
= self
._truncateMessage
696 self
._truncateMessage
= explodingTruncation
697 self
.addCleanup(lambda: setattr(self
, '_truncateMessage', old_truncate
))
699 s1
, s2
= s
+ 'a', s
+ 'b'
700 with self
.assertRaises(self
.failureException
) as cm
:
701 self
.assertEqual(s1
, s2
)
702 self
.assertNotIn('^', str(cm
.exception
))
703 self
.assertEqual(str(cm
.exception
), '%r != %r' % (s1
, s2
))
704 self
.assertEqual(s
+ 'a', s
+ 'a')
706 def testAssertItemsEqual(self
):
708 self
.assertItemsEqual([1, 2, 3], [3, 2, 1])
709 self
.assertItemsEqual(['foo', 'bar', 'baz'], ['bar', 'baz', 'foo'])
710 self
.assertItemsEqual([a
, a
, 2, 2, 3], (a
, 2, 3, a
, 2))
711 self
.assertItemsEqual([1, "2", "a", "a"], ["a", "2", True, "a"])
712 self
.assertRaises(self
.failureException
, self
.assertItemsEqual
,
713 [1, 2] + [3] * 100, [1] * 100 + [2, 3])
714 self
.assertRaises(self
.failureException
, self
.assertItemsEqual
,
715 [1, "2", "a", "a"], ["a", "2", True, 1])
716 self
.assertRaises(self
.failureException
, self
.assertItemsEqual
,
718 self
.assertRaises(self
.failureException
, self
.assertItemsEqual
,
720 self
.assertRaises(self
.failureException
, self
.assertItemsEqual
,
721 [10, 11, 10], [10, 11])
723 # Test that sequences of unhashable objects can be tested for sameness:
724 self
.assertItemsEqual([[1, 2], [3, 4], 0], [False, [3, 4], [1, 2]])
725 # Test that iterator of unhashable objects can be tested for sameness:
726 self
.assertItemsEqual(iter([1, 2, [], 3, 4]),
727 iter([1, 2, [], 3, 4]))
729 # hashable types, but not orderable
730 self
.assertRaises(self
.failureException
, self
.assertItemsEqual
,
731 [], [divmod, 'x', 1, 5j
, 2j
, frozenset()])
733 self
.assertItemsEqual([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}])
734 # comparing heterogenous non-hashable sequences
735 self
.assertItemsEqual([1, 'x', divmod, []], [divmod, [], 'x', 1])
736 self
.assertRaises(self
.failureException
, self
.assertItemsEqual
,
737 [], [divmod, [], 'x', 1, 5j
, 2j
, set()])
738 self
.assertRaises(self
.failureException
, self
.assertItemsEqual
,
741 # Same elements, but not same sequence length
742 self
.assertRaises(self
.failureException
, self
.assertItemsEqual
,
744 self
.assertRaises(self
.failureException
, self
.assertItemsEqual
,
745 [1, 1, "2", "a", "a"], ["2", "2", True, "a"])
746 self
.assertRaises(self
.failureException
, self
.assertItemsEqual
,
747 [1, {'b': 2}, None, True], [{'b': 2}, True, None])
749 # Same elements which don't reliably compare, in
750 # different order, see issue 10242
753 self
.assertItemsEqual(a
, b
)
755 # test utility functions supporting assertItemsEqual()
757 diffs
= set(unittest
.util
._count
_diff
_all
_purpose
('aaabccd', 'abbbcce'))
758 expected
= {(3,1,'a'), (1,3,'b'), (1,0,'d'), (0,1,'e')}
759 self
.assertEqual(diffs
, expected
)
761 diffs
= unittest
.util
._count
_diff
_all
_purpose
([[]], [])
762 self
.assertEqual(diffs
, [(1, 0, [])])
764 diffs
= set(unittest
.util
._count
_diff
_hashable
('aaabccd', 'abbbcce'))
765 expected
= {(3,1,'a'), (1,3,'b'), (1,0,'d'), (0,1,'e')}
766 self
.assertEqual(diffs
, expected
)
768 def testAssertSetEqual(self
):
771 self
.assertSetEqual(set1
, set2
)
773 self
.assertRaises(self
.failureException
, self
.assertSetEqual
, None, set2
)
774 self
.assertRaises(self
.failureException
, self
.assertSetEqual
, [], set2
)
775 self
.assertRaises(self
.failureException
, self
.assertSetEqual
, set1
, None)
776 self
.assertRaises(self
.failureException
, self
.assertSetEqual
, set1
, [])
780 self
.assertRaises(self
.failureException
, self
.assertSetEqual
, set1
, set2
)
784 self
.assertSetEqual(set1
, set2
)
787 set2
= set(['a', 'b'])
788 self
.assertRaises(self
.failureException
, self
.assertSetEqual
, set1
, set2
)
791 set2
= frozenset(['a', 'b'])
792 self
.assertRaises(self
.failureException
, self
.assertSetEqual
, set1
, set2
)
794 set1
= set(['a', 'b'])
795 set2
= frozenset(['a', 'b'])
796 self
.assertSetEqual(set1
, set2
)
800 self
.assertRaises(self
.failureException
, self
.assertSetEqual
, set1
, set2
)
801 self
.assertRaises(self
.failureException
, self
.assertSetEqual
, set2
, set1
)
803 # make sure any string formatting is tuple-safe
804 set1
= set([(0, 1), (2, 3)])
806 self
.assertRaises(self
.failureException
, self
.assertSetEqual
, set1
, set2
)
808 def testInequality(self
):
810 self
.assertGreater(2, 1)
811 self
.assertGreaterEqual(2, 1)
812 self
.assertGreaterEqual(1, 1)
813 self
.assertLess(1, 2)
814 self
.assertLessEqual(1, 2)
815 self
.assertLessEqual(1, 1)
816 self
.assertRaises(self
.failureException
, self
.assertGreater
, 1, 2)
817 self
.assertRaises(self
.failureException
, self
.assertGreater
, 1, 1)
818 self
.assertRaises(self
.failureException
, self
.assertGreaterEqual
, 1, 2)
819 self
.assertRaises(self
.failureException
, self
.assertLess
, 2, 1)
820 self
.assertRaises(self
.failureException
, self
.assertLess
, 1, 1)
821 self
.assertRaises(self
.failureException
, self
.assertLessEqual
, 2, 1)
824 self
.assertGreater(1.1, 1.0)
825 self
.assertGreaterEqual(1.1, 1.0)
826 self
.assertGreaterEqual(1.0, 1.0)
827 self
.assertLess(1.0, 1.1)
828 self
.assertLessEqual(1.0, 1.1)
829 self
.assertLessEqual(1.0, 1.0)
830 self
.assertRaises(self
.failureException
, self
.assertGreater
, 1.0, 1.1)
831 self
.assertRaises(self
.failureException
, self
.assertGreater
, 1.0, 1.0)
832 self
.assertRaises(self
.failureException
, self
.assertGreaterEqual
, 1.0, 1.1)
833 self
.assertRaises(self
.failureException
, self
.assertLess
, 1.1, 1.0)
834 self
.assertRaises(self
.failureException
, self
.assertLess
, 1.0, 1.0)
835 self
.assertRaises(self
.failureException
, self
.assertLessEqual
, 1.1, 1.0)
838 self
.assertGreater('bug', 'ant')
839 self
.assertGreaterEqual('bug', 'ant')
840 self
.assertGreaterEqual('ant', 'ant')
841 self
.assertLess('ant', 'bug')
842 self
.assertLessEqual('ant', 'bug')
843 self
.assertLessEqual('ant', 'ant')
844 self
.assertRaises(self
.failureException
, self
.assertGreater
, 'ant', 'bug')
845 self
.assertRaises(self
.failureException
, self
.assertGreater
, 'ant', 'ant')
846 self
.assertRaises(self
.failureException
, self
.assertGreaterEqual
, 'ant', 'bug')
847 self
.assertRaises(self
.failureException
, self
.assertLess
, 'bug', 'ant')
848 self
.assertRaises(self
.failureException
, self
.assertLess
, 'ant', 'ant')
849 self
.assertRaises(self
.failureException
, self
.assertLessEqual
, 'bug', 'ant')
852 self
.assertGreater(u
'bug', u
'ant')
853 self
.assertGreaterEqual(u
'bug', u
'ant')
854 self
.assertGreaterEqual(u
'ant', u
'ant')
855 self
.assertLess(u
'ant', u
'bug')
856 self
.assertLessEqual(u
'ant', u
'bug')
857 self
.assertLessEqual(u
'ant', u
'ant')
858 self
.assertRaises(self
.failureException
, self
.assertGreater
, u
'ant', u
'bug')
859 self
.assertRaises(self
.failureException
, self
.assertGreater
, u
'ant', u
'ant')
860 self
.assertRaises(self
.failureException
, self
.assertGreaterEqual
, u
'ant',
862 self
.assertRaises(self
.failureException
, self
.assertLess
, u
'bug', u
'ant')
863 self
.assertRaises(self
.failureException
, self
.assertLess
, u
'ant', u
'ant')
864 self
.assertRaises(self
.failureException
, self
.assertLessEqual
, u
'bug', u
'ant')
866 # Try Mixed String/Unicode
867 self
.assertGreater('bug', u
'ant')
868 self
.assertGreater(u
'bug', 'ant')
869 self
.assertGreaterEqual('bug', u
'ant')
870 self
.assertGreaterEqual(u
'bug', 'ant')
871 self
.assertGreaterEqual('ant', u
'ant')
872 self
.assertGreaterEqual(u
'ant', 'ant')
873 self
.assertLess('ant', u
'bug')
874 self
.assertLess(u
'ant', 'bug')
875 self
.assertLessEqual('ant', u
'bug')
876 self
.assertLessEqual(u
'ant', 'bug')
877 self
.assertLessEqual('ant', u
'ant')
878 self
.assertLessEqual(u
'ant', 'ant')
879 self
.assertRaises(self
.failureException
, self
.assertGreater
, 'ant', u
'bug')
880 self
.assertRaises(self
.failureException
, self
.assertGreater
, u
'ant', 'bug')
881 self
.assertRaises(self
.failureException
, self
.assertGreater
, 'ant', u
'ant')
882 self
.assertRaises(self
.failureException
, self
.assertGreater
, u
'ant', 'ant')
883 self
.assertRaises(self
.failureException
, self
.assertGreaterEqual
, 'ant',
885 self
.assertRaises(self
.failureException
, self
.assertGreaterEqual
, u
'ant',
887 self
.assertRaises(self
.failureException
, self
.assertLess
, 'bug', u
'ant')
888 self
.assertRaises(self
.failureException
, self
.assertLess
, u
'bug', 'ant')
889 self
.assertRaises(self
.failureException
, self
.assertLess
, 'ant', u
'ant')
890 self
.assertRaises(self
.failureException
, self
.assertLess
, u
'ant', 'ant')
891 self
.assertRaises(self
.failureException
, self
.assertLessEqual
, 'bug', u
'ant')
892 self
.assertRaises(self
.failureException
, self
.assertLessEqual
, u
'bug', 'ant')
894 def testAssertMultiLineEqual(self
):
896 http://www.python.org/doc/2.3/lib/module-unittest.html
898 A test case is the smallest unit of testing. [...]
900 revised_sample_text
= b
"""\
901 http://www.python.org/doc/2.4.1/lib/module-unittest.html
903 A test case is the smallest unit of testing. [...] You may provide your
904 own implementation that does not subclass from TestCase, of course.
906 sample_text_error
= b
"""\
907 - http://www.python.org/doc/2.3/lib/module-unittest.html
909 + http://www.python.org/doc/2.4.1/lib/module-unittest.html
912 - A test case is the smallest unit of testing. [...]
913 + A test case is the smallest unit of testing. [...] You may provide your
914 ? +++++++++++++++++++++
915 + own implementation that does not subclass from TestCase, of course.
918 for type_changer
in (lambda x
: x
, lambda x
: x
.decode('utf8')):
920 self
.assertMultiLineEqual(type_changer(sample_text
),
921 type_changer(revised_sample_text
))
922 except self
.failureException
, e
:
923 # need to remove the first line of the error message
924 error
= str(e
).encode('utf8').split('\n', 1)[1]
926 # assertMultiLineEqual is hooked up as the default for
927 # unicode strings - so we can't use it for this check
928 self
.assertTrue(sample_text_error
== error
)
930 def testAsertEqualSingleLine(self
):
931 sample_text
= u
"laden swallows fly slowly"
932 revised_sample_text
= u
"unladen swallows fly quickly"
933 sample_text_error
= """\
934 - laden swallows fly slowly
936 + unladen swallows fly quickly
940 self
.assertEqual(sample_text
, revised_sample_text
)
941 except self
.failureException
as e
:
942 error
= str(e
).split('\n', 1)[1]
943 self
.assertTrue(sample_text_error
== error
)
945 def testAssertIsNone(self
):
946 self
.assertIsNone(None)
947 self
.assertRaises(self
.failureException
, self
.assertIsNone
, False)
948 self
.assertIsNotNone('DjZoPloGears on Rails')
949 self
.assertRaises(self
.failureException
, self
.assertIsNotNone
, None)
951 def testAssertRegexpMatches(self
):
952 self
.assertRegexpMatches('asdfabasdf', r
'ab+')
953 self
.assertRaises(self
.failureException
, self
.assertRegexpMatches
,
956 def testAssertRaisesRegexp(self
):
957 class ExceptionMock(Exception):
961 raise ExceptionMock('We expect')
963 self
.assertRaisesRegexp(ExceptionMock
, re
.compile('expect$'), Stub
)
964 self
.assertRaisesRegexp(ExceptionMock
, 'expect$', Stub
)
965 self
.assertRaisesRegexp(ExceptionMock
, u
'expect$', Stub
)
967 def testAssertNotRaisesRegexp(self
):
968 self
.assertRaisesRegexp(
969 self
.failureException
, '^Exception not raised$',
970 self
.assertRaisesRegexp
, Exception, re
.compile('x'),
972 self
.assertRaisesRegexp(
973 self
.failureException
, '^Exception not raised$',
974 self
.assertRaisesRegexp
, Exception, 'x',
976 self
.assertRaisesRegexp(
977 self
.failureException
, '^Exception not raised$',
978 self
.assertRaisesRegexp
, Exception, u
'x',
981 def testAssertRaisesRegexpMismatch(self
):
983 raise Exception('Unexpected')
985 self
.assertRaisesRegexp(
986 self
.failureException
,
987 r
'"\^Expected\$" does not match "Unexpected"',
988 self
.assertRaisesRegexp
, Exception, '^Expected$',
990 self
.assertRaisesRegexp(
991 self
.failureException
,
992 r
'"\^Expected\$" does not match "Unexpected"',
993 self
.assertRaisesRegexp
, Exception, u
'^Expected$',
995 self
.assertRaisesRegexp(
996 self
.failureException
,
997 r
'"\^Expected\$" does not match "Unexpected"',
998 self
.assertRaisesRegexp
, Exception,
999 re
.compile('^Expected$'), Stub
)
1001 def testAssertRaisesExcValue(self
):
1002 class ExceptionMock(Exception):
1006 raise ExceptionMock(foo
)
1007 v
= "particular value"
1009 ctx
= self
.assertRaises(ExceptionMock
)
1013 self
.assertIsInstance(e
, ExceptionMock
)
1014 self
.assertEqual(e
.args
[0], v
)
1016 def testSynonymAssertMethodNames(self
):
1017 """Test undocumented method name synonyms.
1019 Please do not use these methods names in your own code.
1021 This test confirms their continued existence and functionality
1022 in order to avoid breaking existing code.
1024 self
.assertNotEquals(3, 5)
1025 self
.assertEquals(3, 3)
1026 self
.assertAlmostEquals(2.0, 2.0)
1027 self
.assertNotAlmostEquals(3.0, 5.0)
1030 def testPendingDeprecationMethodNames(self
):
1031 """Test fail* methods pending deprecation, they will warn in 3.2.
1033 Do not use these methods. They will go away in 3.3.
1035 with test_support
.check_warnings():
1036 self
.failIfEqual(3, 5)
1037 self
.failUnlessEqual(3, 3)
1038 self
.failUnlessAlmostEqual(2.0, 2.0)
1039 self
.failIfAlmostEqual(3.0, 5.0)
1040 self
.failUnless(True)
1041 self
.failUnlessRaises(TypeError, lambda _
: 3.14 + u
'spam')
1044 def testDeepcopy(self
):
1046 class TestableTest(unittest
.TestCase
):
1047 def testNothing(self
):
1050 test
= TestableTest('testNothing')
1052 # This shouldn't blow up
1055 def testKeyboardInterrupt(self
):
1056 def _raise(self
=None):
1057 raise KeyboardInterrupt
1061 class Test1(unittest
.TestCase
):
1062 test_something
= _raise
1064 class Test2(unittest
.TestCase
):
1066 test_something
= nothing
1068 class Test3(unittest
.TestCase
):
1069 test_something
= nothing
1072 class Test4(unittest
.TestCase
):
1073 def test_something(self
):
1074 self
.addCleanup(_raise
)
1076 for klass
in (Test1
, Test2
, Test3
, Test4
):
1077 with self
.assertRaises(KeyboardInterrupt):
1078 klass('test_something').run()
1080 def testSystemExit(self
):
1081 def _raise(self
=None):
1086 class Test1(unittest
.TestCase
):
1087 test_something
= _raise
1089 class Test2(unittest
.TestCase
):
1091 test_something
= nothing
1093 class Test3(unittest
.TestCase
):
1094 test_something
= nothing
1097 class Test4(unittest
.TestCase
):
1098 def test_something(self
):
1099 self
.addCleanup(_raise
)
1101 for klass
in (Test1
, Test2
, Test3
, Test4
):
1102 result
= unittest
.TestResult()
1103 klass('test_something').run(result
)
1104 self
.assertEqual(len(result
.errors
), 1)
1105 self
.assertEqual(result
.testsRun
, 1)
1108 if __name__
== '__main__':