1import difflib 2import pprint 3import pickle 4import re 5import sys 6 7from copy import deepcopy 8from test import test_support 9 10import unittest 11 12from unittest.test.support import ( 13 TestEquality, TestHashing, LoggingResult, ResultWithNoStartTestRunStopTestRun 14) 15 16 17class Test(object): 18 "Keep these TestCase classes out of the main namespace" 19 20 class Foo(unittest.TestCase): 21 def runTest(self): pass 22 def test1(self): pass 23 24 class Bar(Foo): 25 def test2(self): pass 26 27 class LoggingTestCase(unittest.TestCase): 28 """A test case which logs its calls.""" 29 30 def __init__(self, events): 31 super(Test.LoggingTestCase, self).__init__('test') 32 self.events = events 33 34 def setUp(self): 35 self.events.append('setUp') 36 37 def test(self): 38 self.events.append('test') 39 40 def tearDown(self): 41 self.events.append('tearDown') 42 43 44class Test_TestCase(unittest.TestCase, TestEquality, TestHashing): 45 46 ### Set up attributes used by inherited tests 47 ################################################################ 48 49 # Used by TestHashing.test_hash and TestEquality.test_eq 50 eq_pairs = [(Test.Foo('test1'), Test.Foo('test1'))] 51 52 # Used by TestEquality.test_ne 53 ne_pairs = [(Test.Foo('test1'), Test.Foo('runTest')) 54 ,(Test.Foo('test1'), Test.Bar('test1')) 55 ,(Test.Foo('test1'), Test.Bar('test2'))] 56 57 ################################################################ 58 ### /Set up attributes used by inherited tests 59 60 61 # "class TestCase([methodName])" 62 # ... 63 # "Each instance of TestCase will run a single test method: the 64 # method named methodName." 65 # ... 66 # "methodName defaults to "runTest"." 67 # 68 # Make sure it really is optional, and that it defaults to the proper 69 # thing. 70 def test_init__no_test_name(self): 71 class Test(unittest.TestCase): 72 def runTest(self): raise TypeError() 73 def test(self): pass 74 75 self.assertEqual(Test().id()[-13:], '.Test.runTest') 76 77 # "class TestCase([methodName])" 78 # ... 79 # "Each instance of TestCase will run a single test method: the 80 # method named methodName." 81 def test_init__test_name__valid(self): 82 class Test(unittest.TestCase): 83 def runTest(self): raise TypeError() 84 def test(self): pass 85 86 self.assertEqual(Test('test').id()[-10:], '.Test.test') 87 88 # "class TestCase([methodName])" 89 # ... 90 # "Each instance of TestCase will run a single test method: the 91 # method named methodName." 92 def test_init__test_name__invalid(self): 93 class Test(unittest.TestCase): 94 def runTest(self): raise TypeError() 95 def test(self): pass 96 97 try: 98 Test('testfoo') 99 except ValueError: 100 pass 101 else: 102 self.fail("Failed to raise ValueError") 103 104 # "Return the number of tests represented by the this test object. For 105 # TestCase instances, this will always be 1" 106 def test_countTestCases(self): 107 class Foo(unittest.TestCase): 108 def test(self): pass 109 110 self.assertEqual(Foo('test').countTestCases(), 1) 111 112 # "Return the default type of test result object to be used to run this 113 # test. For TestCase instances, this will always be 114 # unittest.TestResult; subclasses of TestCase should 115 # override this as necessary." 116 def test_defaultTestResult(self): 117 class Foo(unittest.TestCase): 118 def runTest(self): 119 pass 120 121 result = Foo().defaultTestResult() 122 self.assertEqual(type(result), unittest.TestResult) 123 124 # "When a setUp() method is defined, the test runner will run that method 125 # prior to each test. Likewise, if a tearDown() method is defined, the 126 # test runner will invoke that method after each test. In the example, 127 # setUp() was used to create a fresh sequence for each test." 128 # 129 # Make sure the proper call order is maintained, even if setUp() raises 130 # an exception. 131 def test_run_call_order__error_in_setUp(self): 132 events = [] 133 result = LoggingResult(events) 134 135 class Foo(Test.LoggingTestCase): 136 def setUp(self): 137 super(Foo, self).setUp() 138 raise RuntimeError('raised by Foo.setUp') 139 140 Foo(events).run(result) 141 expected = ['startTest', 'setUp', 'addError', 'stopTest'] 142 self.assertEqual(events, expected) 143 144 # "With a temporary result stopTestRun is called when setUp errors. 145 def test_run_call_order__error_in_setUp_default_result(self): 146 events = [] 147 148 class Foo(Test.LoggingTestCase): 149 def defaultTestResult(self): 150 return LoggingResult(self.events) 151 152 def setUp(self): 153 super(Foo, self).setUp() 154 raise RuntimeError('raised by Foo.setUp') 155 156 Foo(events).run() 157 expected = ['startTestRun', 'startTest', 'setUp', 'addError', 158 'stopTest', 'stopTestRun'] 159 self.assertEqual(events, expected) 160 161 # "When a setUp() method is defined, the test runner will run that method 162 # prior to each test. Likewise, if a tearDown() method is defined, the 163 # test runner will invoke that method after each test. In the example, 164 # setUp() was used to create a fresh sequence for each test." 165 # 166 # Make sure the proper call order is maintained, even if the test raises 167 # an error (as opposed to a failure). 168 def test_run_call_order__error_in_test(self): 169 events = [] 170 result = LoggingResult(events) 171 172 class Foo(Test.LoggingTestCase): 173 def test(self): 174 super(Foo, self).test() 175 raise RuntimeError('raised by Foo.test') 176 177 expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown', 178 'stopTest'] 179 Foo(events).run(result) 180 self.assertEqual(events, expected) 181 182 # "With a default result, an error in the test still results in stopTestRun 183 # being called." 184 def test_run_call_order__error_in_test_default_result(self): 185 events = [] 186 187 class Foo(Test.LoggingTestCase): 188 def defaultTestResult(self): 189 return LoggingResult(self.events) 190 191 def test(self): 192 super(Foo, self).test() 193 raise RuntimeError('raised by Foo.test') 194 195 expected = ['startTestRun', 'startTest', 'setUp', 'test', 'addError', 196 'tearDown', 'stopTest', 'stopTestRun'] 197 Foo(events).run() 198 self.assertEqual(events, expected) 199 200 # "When a setUp() method is defined, the test runner will run that method 201 # prior to each test. Likewise, if a tearDown() method is defined, the 202 # test runner will invoke that method after each test. In the example, 203 # setUp() was used to create a fresh sequence for each test." 204 # 205 # Make sure the proper call order is maintained, even if the test signals 206 # a failure (as opposed to an error). 207 def test_run_call_order__failure_in_test(self): 208 events = [] 209 result = LoggingResult(events) 210 211 class Foo(Test.LoggingTestCase): 212 def test(self): 213 super(Foo, self).test() 214 self.fail('raised by Foo.test') 215 216 expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown', 217 'stopTest'] 218 Foo(events).run(result) 219 self.assertEqual(events, expected) 220 221 # "When a test fails with a default result stopTestRun is still called." 222 def test_run_call_order__failure_in_test_default_result(self): 223 224 class Foo(Test.LoggingTestCase): 225 def defaultTestResult(self): 226 return LoggingResult(self.events) 227 def test(self): 228 super(Foo, self).test() 229 self.fail('raised by Foo.test') 230 231 expected = ['startTestRun', 'startTest', 'setUp', 'test', 'addFailure', 232 'tearDown', 'stopTest', 'stopTestRun'] 233 events = [] 234 Foo(events).run() 235 self.assertEqual(events, expected) 236 237 # "When a setUp() method is defined, the test runner will run that method 238 # prior to each test. Likewise, if a tearDown() method is defined, the 239 # test runner will invoke that method after each test. In the example, 240 # setUp() was used to create a fresh sequence for each test." 241 # 242 # Make sure the proper call order is maintained, even if tearDown() raises 243 # an exception. 244 def test_run_call_order__error_in_tearDown(self): 245 events = [] 246 result = LoggingResult(events) 247 248 class Foo(Test.LoggingTestCase): 249 def tearDown(self): 250 super(Foo, self).tearDown() 251 raise RuntimeError('raised by Foo.tearDown') 252 253 Foo(events).run(result) 254 expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError', 255 'stopTest'] 256 self.assertEqual(events, expected) 257 258 # "When tearDown errors with a default result stopTestRun is still called." 259 def test_run_call_order__error_in_tearDown_default_result(self): 260 261 class Foo(Test.LoggingTestCase): 262 def defaultTestResult(self): 263 return LoggingResult(self.events) 264 def tearDown(self): 265 super(Foo, self).tearDown() 266 raise RuntimeError('raised by Foo.tearDown') 267 268 events = [] 269 Foo(events).run() 270 expected = ['startTestRun', 'startTest', 'setUp', 'test', 'tearDown', 271 'addError', 'stopTest', 'stopTestRun'] 272 self.assertEqual(events, expected) 273 274 # "TestCase.run() still works when the defaultTestResult is a TestResult 275 # that does not support startTestRun and stopTestRun. 276 def test_run_call_order_default_result(self): 277 278 class Foo(unittest.TestCase): 279 def defaultTestResult(self): 280 return ResultWithNoStartTestRunStopTestRun() 281 def test(self): 282 pass 283 284 Foo('test').run() 285 286 # "This class attribute gives the exception raised by the test() method. 287 # If a test framework needs to use a specialized exception, possibly to 288 # carry additional information, it must subclass this exception in 289 # order to ``play fair'' with the framework. The initial value of this 290 # attribute is AssertionError" 291 def test_failureException__default(self): 292 class Foo(unittest.TestCase): 293 def test(self): 294 pass 295 296 self.assertIs(Foo('test').failureException, AssertionError) 297 298 # "This class attribute gives the exception raised by the test() method. 299 # If a test framework needs to use a specialized exception, possibly to 300 # carry additional information, it must subclass this exception in 301 # order to ``play fair'' with the framework." 302 # 303 # Make sure TestCase.run() respects the designated failureException 304 def test_failureException__subclassing__explicit_raise(self): 305 events = [] 306 result = LoggingResult(events) 307 308 class Foo(unittest.TestCase): 309 def test(self): 310 raise RuntimeError() 311 312 failureException = RuntimeError 313 314 self.assertIs(Foo('test').failureException, RuntimeError) 315 316 317 Foo('test').run(result) 318 expected = ['startTest', 'addFailure', 'stopTest'] 319 self.assertEqual(events, expected) 320 321 # "This class attribute gives the exception raised by the test() method. 322 # If a test framework needs to use a specialized exception, possibly to 323 # carry additional information, it must subclass this exception in 324 # order to ``play fair'' with the framework." 325 # 326 # Make sure TestCase.run() respects the designated failureException 327 def test_failureException__subclassing__implicit_raise(self): 328 events = [] 329 result = LoggingResult(events) 330 331 class Foo(unittest.TestCase): 332 def test(self): 333 self.fail("foo") 334 335 failureException = RuntimeError 336 337 self.assertIs(Foo('test').failureException, RuntimeError) 338 339 340 Foo('test').run(result) 341 expected = ['startTest', 'addFailure', 'stopTest'] 342 self.assertEqual(events, expected) 343 344 # "The default implementation does nothing." 345 def test_setUp(self): 346 class Foo(unittest.TestCase): 347 def runTest(self): 348 pass 349 350 # ... and nothing should happen 351 Foo().setUp() 352 353 # "The default implementation does nothing." 354 def test_tearDown(self): 355 class Foo(unittest.TestCase): 356 def runTest(self): 357 pass 358 359 # ... and nothing should happen 360 Foo().tearDown() 361 362 # "Return a string identifying the specific test case." 363 # 364 # Because of the vague nature of the docs, I'm not going to lock this 365 # test down too much. Really all that can be asserted is that the id() 366 # will be a string (either 8-byte or unicode -- again, because the docs 367 # just say "string") 368 def test_id(self): 369 class Foo(unittest.TestCase): 370 def runTest(self): 371 pass 372 373 self.assertIsInstance(Foo().id(), basestring) 374 375 # "If result is omitted or None, a temporary result object is created 376 # and used, but is not made available to the caller. As TestCase owns the 377 # temporary result startTestRun and stopTestRun are called. 378 379 def test_run__uses_defaultTestResult(self): 380 events = [] 381 382 class Foo(unittest.TestCase): 383 def test(self): 384 events.append('test') 385 386 def defaultTestResult(self): 387 return LoggingResult(events) 388 389 # Make run() find a result object on its own 390 Foo('test').run() 391 392 expected = ['startTestRun', 'startTest', 'test', 'addSuccess', 393 'stopTest', 'stopTestRun'] 394 self.assertEqual(events, expected) 395 396 def testShortDescriptionWithoutDocstring(self): 397 self.assertIsNone(self.shortDescription()) 398 399 @unittest.skipIf(sys.flags.optimize >= 2, 400 "Docstrings are omitted with -O2 and above") 401 def testShortDescriptionWithOneLineDocstring(self): 402 """Tests shortDescription() for a method with a docstring.""" 403 self.assertEqual( 404 self.shortDescription(), 405 'Tests shortDescription() for a method with a docstring.') 406 407 @unittest.skipIf(sys.flags.optimize >= 2, 408 "Docstrings are omitted with -O2 and above") 409 def testShortDescriptionWithMultiLineDocstring(self): 410 """Tests shortDescription() for a method with a longer docstring. 411 412 This method ensures that only the first line of a docstring is 413 returned used in the short description, no matter how long the 414 whole thing is. 415 """ 416 self.assertEqual( 417 self.shortDescription(), 418 'Tests shortDescription() for a method with a longer ' 419 'docstring.') 420 421 def testAddTypeEqualityFunc(self): 422 class SadSnake(object): 423 """Dummy class for test_addTypeEqualityFunc.""" 424 s1, s2 = SadSnake(), SadSnake() 425 self.assertNotEqual(s1, s2) 426 def AllSnakesCreatedEqual(a, b, msg=None): 427 return type(a) is type(b) is SadSnake 428 self.addTypeEqualityFunc(SadSnake, AllSnakesCreatedEqual) 429 self.assertEqual(s1, s2) 430 # No this doesn't clean up and remove the SadSnake equality func 431 # from this TestCase instance but since its a local nothing else 432 # will ever notice that. 433 434 def testAssertIs(self): 435 thing = object() 436 self.assertIs(thing, thing) 437 self.assertRaises(self.failureException, self.assertIs, thing, object()) 438 439 def testAssertIsNot(self): 440 thing = object() 441 self.assertIsNot(thing, object()) 442 self.assertRaises(self.failureException, self.assertIsNot, thing, thing) 443 444 def testAssertIsInstance(self): 445 thing = [] 446 self.assertIsInstance(thing, list) 447 self.assertRaises(self.failureException, self.assertIsInstance, 448 thing, dict) 449 450 def testAssertNotIsInstance(self): 451 thing = [] 452 self.assertNotIsInstance(thing, dict) 453 self.assertRaises(self.failureException, self.assertNotIsInstance, 454 thing, list) 455 456 def testAssertIn(self): 457 animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'} 458 459 self.assertIn('a', 'abc') 460 self.assertIn(2, [1, 2, 3]) 461 self.assertIn('monkey', animals) 462 463 self.assertNotIn('d', 'abc') 464 self.assertNotIn(0, [1, 2, 3]) 465 self.assertNotIn('otter', animals) 466 467 self.assertRaises(self.failureException, self.assertIn, 'x', 'abc') 468 self.assertRaises(self.failureException, self.assertIn, 4, [1, 2, 3]) 469 self.assertRaises(self.failureException, self.assertIn, 'elephant', 470 animals) 471 472 self.assertRaises(self.failureException, self.assertNotIn, 'c', 'abc') 473 self.assertRaises(self.failureException, self.assertNotIn, 1, [1, 2, 3]) 474 self.assertRaises(self.failureException, self.assertNotIn, 'cow', 475 animals) 476 477 def testAssertDictContainsSubset(self): 478 self.assertDictContainsSubset({}, {}) 479 self.assertDictContainsSubset({}, {'a': 1}) 480 self.assertDictContainsSubset({'a': 1}, {'a': 1}) 481 self.assertDictContainsSubset({'a': 1}, {'a': 1, 'b': 2}) 482 self.assertDictContainsSubset({'a': 1, 'b': 2}, {'a': 1, 'b': 2}) 483 484 with self.assertRaises(self.failureException): 485 self.assertDictContainsSubset({1: "one"}, {}) 486 487 with self.assertRaises(self.failureException): 488 self.assertDictContainsSubset({'a': 2}, {'a': 1}) 489 490 with self.assertRaises(self.failureException): 491 self.assertDictContainsSubset({'c': 1}, {'a': 1}) 492 493 with self.assertRaises(self.failureException): 494 self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1}) 495 496 with self.assertRaises(self.failureException): 497 self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1}) 498 499 with test_support.check_warnings(("", UnicodeWarning)): 500 one = ''.join(chr(i) for i in range(255)) 501 # this used to cause a UnicodeDecodeError constructing the failure msg 502 with self.assertRaises(self.failureException): 503 self.assertDictContainsSubset({'foo': one}, {'foo': u'\uFFFD'}) 504 505 def testAssertEqual(self): 506 equal_pairs = [ 507 ((), ()), 508 ({}, {}), 509 ([], []), 510 (set(), set()), 511 (frozenset(), frozenset())] 512 for a, b in equal_pairs: 513 # This mess of try excepts is to test the assertEqual behavior 514 # itself. 515 try: 516 self.assertEqual(a, b) 517 except self.failureException: 518 self.fail('assertEqual(%r, %r) failed' % (a, b)) 519 try: 520 self.assertEqual(a, b, msg='foo') 521 except self.failureException: 522 self.fail('assertEqual(%r, %r) with msg= failed' % (a, b)) 523 try: 524 self.assertEqual(a, b, 'foo') 525 except self.failureException: 526 self.fail('assertEqual(%r, %r) with third parameter failed' % 527 (a, b)) 528 529 unequal_pairs = [ 530 ((), []), 531 ({}, set()), 532 (set([4,1]), frozenset([4,2])), 533 (frozenset([4,5]), set([2,3])), 534 (set([3,4]), set([5,4]))] 535 for a, b in unequal_pairs: 536 self.assertRaises(self.failureException, self.assertEqual, a, b) 537 self.assertRaises(self.failureException, self.assertEqual, a, b, 538 'foo') 539 self.assertRaises(self.failureException, self.assertEqual, a, b, 540 msg='foo') 541 542 def testEquality(self): 543 self.assertListEqual([], []) 544 self.assertTupleEqual((), ()) 545 self.assertSequenceEqual([], ()) 546 547 a = [0, 'a', []] 548 b = [] 549 self.assertRaises(unittest.TestCase.failureException, 550 self.assertListEqual, a, b) 551 self.assertRaises(unittest.TestCase.failureException, 552 self.assertListEqual, tuple(a), tuple(b)) 553 self.assertRaises(unittest.TestCase.failureException, 554 self.assertSequenceEqual, a, tuple(b)) 555 556 b.extend(a) 557 self.assertListEqual(a, b) 558 self.assertTupleEqual(tuple(a), tuple(b)) 559 self.assertSequenceEqual(a, tuple(b)) 560 self.assertSequenceEqual(tuple(a), b) 561 562 self.assertRaises(self.failureException, self.assertListEqual, 563 a, tuple(b)) 564 self.assertRaises(self.failureException, self.assertTupleEqual, 565 tuple(a), b) 566 self.assertRaises(self.failureException, self.assertListEqual, None, b) 567 self.assertRaises(self.failureException, self.assertTupleEqual, None, 568 tuple(b)) 569 self.assertRaises(self.failureException, self.assertSequenceEqual, 570 None, tuple(b)) 571 self.assertRaises(self.failureException, self.assertListEqual, 1, 1) 572 self.assertRaises(self.failureException, self.assertTupleEqual, 1, 1) 573 self.assertRaises(self.failureException, self.assertSequenceEqual, 574 1, 1) 575 576 self.assertDictEqual({}, {}) 577 578 c = { 'x': 1 } 579 d = {} 580 self.assertRaises(unittest.TestCase.failureException, 581 self.assertDictEqual, c, d) 582 583 d.update(c) 584 self.assertDictEqual(c, d) 585 586 d['x'] = 0 587 self.assertRaises(unittest.TestCase.failureException, 588 self.assertDictEqual, c, d, 'These are unequal') 589 590 self.assertRaises(self.failureException, self.assertDictEqual, None, d) 591 self.assertRaises(self.failureException, self.assertDictEqual, [], d) 592 self.assertRaises(self.failureException, self.assertDictEqual, 1, 1) 593 594 def testAssertSequenceEqualMaxDiff(self): 595 self.assertEqual(self.maxDiff, 80*8) 596 seq1 = 'a' + 'x' * 80**2 597 seq2 = 'b' + 'x' * 80**2 598 diff = '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(), 599 pprint.pformat(seq2).splitlines())) 600 # the +1 is the leading \n added by assertSequenceEqual 601 omitted = unittest.case.DIFF_OMITTED % (len(diff) + 1,) 602 603 self.maxDiff = len(diff)//2 604 try: 605 self.assertSequenceEqual(seq1, seq2) 606 except self.failureException as e: 607 msg = e.args[0] 608 else: 609 self.fail('assertSequenceEqual did not fail.') 610 self.assertLess(len(msg), len(diff)) 611 self.assertIn(omitted, msg) 612 613 self.maxDiff = len(diff) * 2 614 try: 615 self.assertSequenceEqual(seq1, seq2) 616 except self.failureException as e: 617 msg = e.args[0] 618 else: 619 self.fail('assertSequenceEqual did not fail.') 620 self.assertGreater(len(msg), len(diff)) 621 self.assertNotIn(omitted, msg) 622 623 self.maxDiff = None 624 try: 625 self.assertSequenceEqual(seq1, seq2) 626 except self.failureException as e: 627 msg = e.args[0] 628 else: 629 self.fail('assertSequenceEqual did not fail.') 630 self.assertGreater(len(msg), len(diff)) 631 self.assertNotIn(omitted, msg) 632 633 def testTruncateMessage(self): 634 self.maxDiff = 1 635 message = self._truncateMessage('foo', 'bar') 636 omitted = unittest.case.DIFF_OMITTED % len('bar') 637 self.assertEqual(message, 'foo' + omitted) 638 639 self.maxDiff = None 640 message = self._truncateMessage('foo', 'bar') 641 self.assertEqual(message, 'foobar') 642 643 self.maxDiff = 4 644 message = self._truncateMessage('foo', 'bar') 645 self.assertEqual(message, 'foobar') 646 647 def testAssertDictEqualTruncates(self): 648 test = unittest.TestCase('assertEqual') 649 def truncate(msg, diff): 650 return 'foo' 651 test._truncateMessage = truncate 652 try: 653 test.assertDictEqual({}, {1: 0}) 654 except self.failureException as e: 655 self.assertEqual(str(e), 'foo') 656 else: 657 self.fail('assertDictEqual did not fail') 658 659 def testAssertMultiLineEqualTruncates(self): 660 test = unittest.TestCase('assertEqual') 661 def truncate(msg, diff): 662 return 'foo' 663 test._truncateMessage = truncate 664 try: 665 test.assertMultiLineEqual('foo', 'bar') 666 except self.failureException as e: 667 self.assertEqual(str(e), 'foo') 668 else: 669 self.fail('assertMultiLineEqual did not fail') 670 671 def testAssertEqual_diffThreshold(self): 672 # check threshold value 673 self.assertEqual(self._diffThreshold, 2**16) 674 # disable madDiff to get diff markers 675 self.maxDiff = None 676 677 # set a lower threshold value and add a cleanup to restore it 678 old_threshold = self._diffThreshold 679 self._diffThreshold = 2**8 680 self.addCleanup(lambda: setattr(self, '_diffThreshold', old_threshold)) 681 682 # under the threshold: diff marker (^) in error message 683 s = u'x' * (2**7) 684 with self.assertRaises(self.failureException) as cm: 685 self.assertEqual(s + 'a', s + 'b') 686 self.assertIn('^', str(cm.exception)) 687 self.assertEqual(s + 'a', s + 'a') 688 689 # over the threshold: diff not used and marker (^) not in error message 690 s = u'x' * (2**9) 691 # if the path that uses difflib is taken, _truncateMessage will be 692 # called -- replace it with explodingTruncation to verify that this 693 # doesn't happen 694 def explodingTruncation(message, diff): 695 raise SystemError('this should not be raised') 696 old_truncate = self._truncateMessage 697 self._truncateMessage = explodingTruncation 698 self.addCleanup(lambda: setattr(self, '_truncateMessage', old_truncate)) 699 700 s1, s2 = s + 'a', s + 'b' 701 with self.assertRaises(self.failureException) as cm: 702 self.assertEqual(s1, s2) 703 self.assertNotIn('^', str(cm.exception)) 704 self.assertEqual(str(cm.exception), '%r != %r' % (s1, s2)) 705 self.assertEqual(s + 'a', s + 'a') 706 707 def testAssertItemsEqual(self): 708 a = object() 709 self.assertItemsEqual([1, 2, 3], [3, 2, 1]) 710 self.assertItemsEqual(['foo', 'bar', 'baz'], ['bar', 'baz', 'foo']) 711 self.assertItemsEqual([a, a, 2, 2, 3], (a, 2, 3, a, 2)) 712 self.assertItemsEqual([1, "2", "a", "a"], ["a", "2", True, "a"]) 713 self.assertRaises(self.failureException, self.assertItemsEqual, 714 [1, 2] + [3] * 100, [1] * 100 + [2, 3]) 715 self.assertRaises(self.failureException, self.assertItemsEqual, 716 [1, "2", "a", "a"], ["a", "2", True, 1]) 717 self.assertRaises(self.failureException, self.assertItemsEqual, 718 [10], [10, 11]) 719 self.assertRaises(self.failureException, self.assertItemsEqual, 720 [10, 11], [10]) 721 self.assertRaises(self.failureException, self.assertItemsEqual, 722 [10, 11, 10], [10, 11]) 723 724 # Test that sequences of unhashable objects can be tested for sameness: 725 self.assertItemsEqual([[1, 2], [3, 4], 0], [False, [3, 4], [1, 2]]) 726 # Test that iterator of unhashable objects can be tested for sameness: 727 self.assertItemsEqual(iter([1, 2, [], 3, 4]), 728 iter([1, 2, [], 3, 4])) 729 730 # hashable types, but not orderable 731 self.assertRaises(self.failureException, self.assertItemsEqual, 732 [], [divmod, 'x', 1, 5j, 2j, frozenset()]) 733 # comparing dicts 734 self.assertItemsEqual([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}]) 735 # comparing heterogenous non-hashable sequences 736 self.assertItemsEqual([1, 'x', divmod, []], [divmod, [], 'x', 1]) 737 self.assertRaises(self.failureException, self.assertItemsEqual, 738 [], [divmod, [], 'x', 1, 5j, 2j, set()]) 739 self.assertRaises(self.failureException, self.assertItemsEqual, 740 [[1]], [[2]]) 741 742 # Same elements, but not same sequence length 743 self.assertRaises(self.failureException, self.assertItemsEqual, 744 [1, 1, 2], [2, 1]) 745 self.assertRaises(self.failureException, self.assertItemsEqual, 746 [1, 1, "2", "a", "a"], ["2", "2", True, "a"]) 747 self.assertRaises(self.failureException, self.assertItemsEqual, 748 [1, {'b': 2}, None, True], [{'b': 2}, True, None]) 749 750 # Same elements which don't reliably compare, in 751 # different order, see issue 10242 752 a = [{2,4}, {1,2}] 753 b = a[::-1] 754 self.assertItemsEqual(a, b) 755 756 # test utility functions supporting assertItemsEqual() 757 758 diffs = set(unittest.util._count_diff_all_purpose('aaabccd', 'abbbcce')) 759 expected = {(3,1,'a'), (1,3,'b'), (1,0,'d'), (0,1,'e')} 760 self.assertEqual(diffs, expected) 761 762 diffs = unittest.util._count_diff_all_purpose([[]], []) 763 self.assertEqual(diffs, [(1, 0, [])]) 764 765 diffs = set(unittest.util._count_diff_hashable('aaabccd', 'abbbcce')) 766 expected = {(3,1,'a'), (1,3,'b'), (1,0,'d'), (0,1,'e')} 767 self.assertEqual(diffs, expected) 768 769 def testAssertSetEqual(self): 770 set1 = set() 771 set2 = set() 772 self.assertSetEqual(set1, set2) 773 774 self.assertRaises(self.failureException, self.assertSetEqual, None, set2) 775 self.assertRaises(self.failureException, self.assertSetEqual, [], set2) 776 self.assertRaises(self.failureException, self.assertSetEqual, set1, None) 777 self.assertRaises(self.failureException, self.assertSetEqual, set1, []) 778 779 set1 = set(['a']) 780 set2 = set() 781 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) 782 783 set1 = set(['a']) 784 set2 = set(['a']) 785 self.assertSetEqual(set1, set2) 786 787 set1 = set(['a']) 788 set2 = set(['a', 'b']) 789 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) 790 791 set1 = set(['a']) 792 set2 = frozenset(['a', 'b']) 793 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) 794 795 set1 = set(['a', 'b']) 796 set2 = frozenset(['a', 'b']) 797 self.assertSetEqual(set1, set2) 798 799 set1 = set() 800 set2 = "foo" 801 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) 802 self.assertRaises(self.failureException, self.assertSetEqual, set2, set1) 803 804 # make sure any string formatting is tuple-safe 805 set1 = set([(0, 1), (2, 3)]) 806 set2 = set([(4, 5)]) 807 self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) 808 809 def testInequality(self): 810 # Try ints 811 self.assertGreater(2, 1) 812 self.assertGreaterEqual(2, 1) 813 self.assertGreaterEqual(1, 1) 814 self.assertLess(1, 2) 815 self.assertLessEqual(1, 2) 816 self.assertLessEqual(1, 1) 817 self.assertRaises(self.failureException, self.assertGreater, 1, 2) 818 self.assertRaises(self.failureException, self.assertGreater, 1, 1) 819 self.assertRaises(self.failureException, self.assertGreaterEqual, 1, 2) 820 self.assertRaises(self.failureException, self.assertLess, 2, 1) 821 self.assertRaises(self.failureException, self.assertLess, 1, 1) 822 self.assertRaises(self.failureException, self.assertLessEqual, 2, 1) 823 824 # Try Floats 825 self.assertGreater(1.1, 1.0) 826 self.assertGreaterEqual(1.1, 1.0) 827 self.assertGreaterEqual(1.0, 1.0) 828 self.assertLess(1.0, 1.1) 829 self.assertLessEqual(1.0, 1.1) 830 self.assertLessEqual(1.0, 1.0) 831 self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.1) 832 self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.0) 833 self.assertRaises(self.failureException, self.assertGreaterEqual, 1.0, 1.1) 834 self.assertRaises(self.failureException, self.assertLess, 1.1, 1.0) 835 self.assertRaises(self.failureException, self.assertLess, 1.0, 1.0) 836 self.assertRaises(self.failureException, self.assertLessEqual, 1.1, 1.0) 837 838 # Try Strings 839 self.assertGreater('bug', 'ant') 840 self.assertGreaterEqual('bug', 'ant') 841 self.assertGreaterEqual('ant', 'ant') 842 self.assertLess('ant', 'bug') 843 self.assertLessEqual('ant', 'bug') 844 self.assertLessEqual('ant', 'ant') 845 self.assertRaises(self.failureException, self.assertGreater, 'ant', 'bug') 846 self.assertRaises(self.failureException, self.assertGreater, 'ant', 'ant') 847 self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant', 'bug') 848 self.assertRaises(self.failureException, self.assertLess, 'bug', 'ant') 849 self.assertRaises(self.failureException, self.assertLess, 'ant', 'ant') 850 self.assertRaises(self.failureException, self.assertLessEqual, 'bug', 'ant') 851 852 # Try Unicode 853 self.assertGreater(u'bug', u'ant') 854 self.assertGreaterEqual(u'bug', u'ant') 855 self.assertGreaterEqual(u'ant', u'ant') 856 self.assertLess(u'ant', u'bug') 857 self.assertLessEqual(u'ant', u'bug') 858 self.assertLessEqual(u'ant', u'ant') 859 self.assertRaises(self.failureException, self.assertGreater, u'ant', u'bug') 860 self.assertRaises(self.failureException, self.assertGreater, u'ant', u'ant') 861 self.assertRaises(self.failureException, self.assertGreaterEqual, u'ant', 862 u'bug') 863 self.assertRaises(self.failureException, self.assertLess, u'bug', u'ant') 864 self.assertRaises(self.failureException, self.assertLess, u'ant', u'ant') 865 self.assertRaises(self.failureException, self.assertLessEqual, u'bug', u'ant') 866 867 # Try Mixed String/Unicode 868 self.assertGreater('bug', u'ant') 869 self.assertGreater(u'bug', 'ant') 870 self.assertGreaterEqual('bug', u'ant') 871 self.assertGreaterEqual(u'bug', 'ant') 872 self.assertGreaterEqual('ant', u'ant') 873 self.assertGreaterEqual(u'ant', 'ant') 874 self.assertLess('ant', u'bug') 875 self.assertLess(u'ant', 'bug') 876 self.assertLessEqual('ant', u'bug') 877 self.assertLessEqual(u'ant', 'bug') 878 self.assertLessEqual('ant', u'ant') 879 self.assertLessEqual(u'ant', 'ant') 880 self.assertRaises(self.failureException, self.assertGreater, 'ant', u'bug') 881 self.assertRaises(self.failureException, self.assertGreater, u'ant', 'bug') 882 self.assertRaises(self.failureException, self.assertGreater, 'ant', u'ant') 883 self.assertRaises(self.failureException, self.assertGreater, u'ant', 'ant') 884 self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant', 885 u'bug') 886 self.assertRaises(self.failureException, self.assertGreaterEqual, u'ant', 887 'bug') 888 self.assertRaises(self.failureException, self.assertLess, 'bug', u'ant') 889 self.assertRaises(self.failureException, self.assertLess, u'bug', 'ant') 890 self.assertRaises(self.failureException, self.assertLess, 'ant', u'ant') 891 self.assertRaises(self.failureException, self.assertLess, u'ant', 'ant') 892 self.assertRaises(self.failureException, self.assertLessEqual, 'bug', u'ant') 893 self.assertRaises(self.failureException, self.assertLessEqual, u'bug', 'ant') 894 895 def testAssertMultiLineEqual(self): 896 sample_text = b"""\ 897http://www.python.org/doc/2.3/lib/module-unittest.html 898test case 899 A test case is the smallest unit of testing. [...] 900""" 901 revised_sample_text = b"""\ 902http://www.python.org/doc/2.4.1/lib/module-unittest.html 903test case 904 A test case is the smallest unit of testing. [...] You may provide your 905 own implementation that does not subclass from TestCase, of course. 906""" 907 sample_text_error = b"""\ 908- http://www.python.org/doc/2.3/lib/module-unittest.html 909? ^ 910+ http://www.python.org/doc/2.4.1/lib/module-unittest.html 911? ^^^ 912 test case 913- A test case is the smallest unit of testing. [...] 914+ A test case is the smallest unit of testing. [...] You may provide your 915? +++++++++++++++++++++ 916+ own implementation that does not subclass from TestCase, of course. 917""" 918 self.maxDiff = None 919 for type_changer in (lambda x: x, lambda x: x.decode('utf8')): 920 try: 921 self.assertMultiLineEqual(type_changer(sample_text), 922 type_changer(revised_sample_text)) 923 except self.failureException, e: 924 # need to remove the first line of the error message 925 error = str(e).encode('utf8').split('\n', 1)[1] 926 927 # assertMultiLineEqual is hooked up as the default for 928 # unicode strings - so we can't use it for this check 929 self.assertTrue(sample_text_error == error) 930 931 def testAsertEqualSingleLine(self): 932 sample_text = u"laden swallows fly slowly" 933 revised_sample_text = u"unladen swallows fly quickly" 934 sample_text_error = """\ 935- laden swallows fly slowly 936? ^^^^ 937+ unladen swallows fly quickly 938? ++ ^^^^^ 939""" 940 try: 941 self.assertEqual(sample_text, revised_sample_text) 942 except self.failureException as e: 943 error = str(e).split('\n', 1)[1] 944 self.assertTrue(sample_text_error == error) 945 946 def testAssertIsNone(self): 947 self.assertIsNone(None) 948 self.assertRaises(self.failureException, self.assertIsNone, False) 949 self.assertIsNotNone('DjZoPloGears on Rails') 950 self.assertRaises(self.failureException, self.assertIsNotNone, None) 951 952 def testAssertRegexpMatches(self): 953 self.assertRegexpMatches('asdfabasdf', r'ab+') 954 self.assertRaises(self.failureException, self.assertRegexpMatches, 955 'saaas', r'aaaa') 956 957 def testAssertRaisesCallable(self): 958 class ExceptionMock(Exception): 959 pass 960 def Stub(): 961 raise ExceptionMock('We expect') 962 self.assertRaises(ExceptionMock, Stub) 963 # A tuple of exception classes is accepted 964 self.assertRaises((ValueError, ExceptionMock), Stub) 965 # *args and **kwargs also work 966 self.assertRaises(ValueError, int, '19', base=8) 967 # Failure when no exception is raised 968 with self.assertRaises(self.failureException): 969 self.assertRaises(ExceptionMock, lambda: 0) 970 # Failure when another exception is raised 971 with self.assertRaises(ExceptionMock): 972 self.assertRaises(ValueError, Stub) 973 974 def testAssertRaisesContext(self): 975 class ExceptionMock(Exception): 976 pass 977 def Stub(): 978 raise ExceptionMock('We expect') 979 with self.assertRaises(ExceptionMock): 980 Stub() 981 # A tuple of exception classes is accepted 982 with self.assertRaises((ValueError, ExceptionMock)) as cm: 983 Stub() 984 # The context manager exposes caught exception 985 self.assertIsInstance(cm.exception, ExceptionMock) 986 self.assertEqual(cm.exception.args[0], 'We expect') 987 # *args and **kwargs also work 988 with self.assertRaises(ValueError): 989 int('19', base=8) 990 # Failure when no exception is raised 991 with self.assertRaises(self.failureException): 992 with self.assertRaises(ExceptionMock): 993 pass 994 # Failure when another exception is raised 995 with self.assertRaises(ExceptionMock): 996 self.assertRaises(ValueError, Stub) 997 998 def testAssertRaisesRegexp(self): 999 class ExceptionMock(Exception): 1000 pass 1001 1002 def Stub(): 1003 raise ExceptionMock('We expect') 1004 1005 self.assertRaisesRegexp(ExceptionMock, re.compile('expect$'), Stub) 1006 self.assertRaisesRegexp(ExceptionMock, 'expect$', Stub) 1007 self.assertRaisesRegexp(ExceptionMock, u'expect$', Stub) 1008 1009 def testAssertNotRaisesRegexp(self): 1010 self.assertRaisesRegexp( 1011 self.failureException, '^Exception not raised$', 1012 self.assertRaisesRegexp, Exception, re.compile('x'), 1013 lambda: None) 1014 self.assertRaisesRegexp( 1015 self.failureException, '^Exception not raised$', 1016 self.assertRaisesRegexp, Exception, 'x', 1017 lambda: None) 1018 self.assertRaisesRegexp( 1019 self.failureException, '^Exception not raised$', 1020 self.assertRaisesRegexp, Exception, u'x', 1021 lambda: None) 1022 1023 def testAssertRaisesRegexpInvalidRegexp(self): 1024 # Issue 20145. 1025 class MyExc(Exception): 1026 pass 1027 self.assertRaises(TypeError, self.assertRaisesRegexp, MyExc, lambda: True) 1028 1029 def testAssertRaisesRegexpMismatch(self): 1030 def Stub(): 1031 raise Exception('Unexpected') 1032 1033 self.assertRaisesRegexp( 1034 self.failureException, 1035 r'"\^Expected\$" does not match "Unexpected"', 1036 self.assertRaisesRegexp, Exception, '^Expected$', 1037 Stub) 1038 self.assertRaisesRegexp( 1039 self.failureException, 1040 r'"\^Expected\$" does not match "Unexpected"', 1041 self.assertRaisesRegexp, Exception, u'^Expected$', 1042 Stub) 1043 self.assertRaisesRegexp( 1044 self.failureException, 1045 r'"\^Expected\$" does not match "Unexpected"', 1046 self.assertRaisesRegexp, Exception, 1047 re.compile('^Expected$'), Stub) 1048 1049 def testAssertRaisesExcValue(self): 1050 class ExceptionMock(Exception): 1051 pass 1052 1053 def Stub(foo): 1054 raise ExceptionMock(foo) 1055 v = "particular value" 1056 1057 ctx = self.assertRaises(ExceptionMock) 1058 with ctx: 1059 Stub(v) 1060 e = ctx.exception 1061 self.assertIsInstance(e, ExceptionMock) 1062 self.assertEqual(e.args[0], v) 1063 1064 def testSynonymAssertMethodNames(self): 1065 """Test undocumented method name synonyms. 1066 1067 Please do not use these methods names in your own code. 1068 1069 This test confirms their continued existence and functionality 1070 in order to avoid breaking existing code. 1071 """ 1072 self.assertNotEquals(3, 5) 1073 self.assertEquals(3, 3) 1074 self.assertAlmostEquals(2.0, 2.0) 1075 self.assertNotAlmostEquals(3.0, 5.0) 1076 self.assert_(True) 1077 1078 def testPendingDeprecationMethodNames(self): 1079 """Test fail* methods pending deprecation, they will warn in 3.2. 1080 1081 Do not use these methods. They will go away in 3.3. 1082 """ 1083 with test_support.check_warnings(): 1084 self.failIfEqual(3, 5) 1085 self.failUnlessEqual(3, 3) 1086 self.failUnlessAlmostEqual(2.0, 2.0) 1087 self.failIfAlmostEqual(3.0, 5.0) 1088 self.failUnless(True) 1089 self.failUnlessRaises(TypeError, lambda _: 3.14 + u'spam') 1090 self.failIf(False) 1091 1092 def testDeepcopy(self): 1093 # Issue: 5660 1094 class TestableTest(unittest.TestCase): 1095 def testNothing(self): 1096 pass 1097 1098 test = TestableTest('testNothing') 1099 1100 # This shouldn't blow up 1101 deepcopy(test) 1102 1103 def testKeyboardInterrupt(self): 1104 def _raise(self=None): 1105 raise KeyboardInterrupt 1106 def nothing(self): 1107 pass 1108 1109 class Test1(unittest.TestCase): 1110 test_something = _raise 1111 1112 class Test2(unittest.TestCase): 1113 setUp = _raise 1114 test_something = nothing 1115 1116 class Test3(unittest.TestCase): 1117 test_something = nothing 1118 tearDown = _raise 1119 1120 class Test4(unittest.TestCase): 1121 def test_something(self): 1122 self.addCleanup(_raise) 1123 1124 for klass in (Test1, Test2, Test3, Test4): 1125 with self.assertRaises(KeyboardInterrupt): 1126 klass('test_something').run() 1127 1128 def testSystemExit(self): 1129 def _raise(self=None): 1130 raise SystemExit 1131 def nothing(self): 1132 pass 1133 1134 class Test1(unittest.TestCase): 1135 test_something = _raise 1136 1137 class Test2(unittest.TestCase): 1138 setUp = _raise 1139 test_something = nothing 1140 1141 class Test3(unittest.TestCase): 1142 test_something = nothing 1143 tearDown = _raise 1144 1145 class Test4(unittest.TestCase): 1146 def test_something(self): 1147 self.addCleanup(_raise) 1148 1149 for klass in (Test1, Test2, Test3, Test4): 1150 result = unittest.TestResult() 1151 klass('test_something').run(result) 1152 self.assertEqual(len(result.errors), 1) 1153 self.assertEqual(result.testsRun, 1) 1154 1155 def testPickle(self): 1156 # Issue 10326 1157 1158 # Can't use TestCase classes defined in Test class as 1159 # pickle does not work with inner classes 1160 test = unittest.TestCase('run') 1161 for protocol in range(pickle.HIGHEST_PROTOCOL + 1): 1162 1163 # blew up prior to fix 1164 pickled_test = pickle.dumps(test, protocol=protocol) 1165 1166 unpickled_test = pickle.loads(pickled_test) 1167 self.assertEqual(test, unpickled_test) 1168 1169 1170if __name__ == '__main__': 1171 unittest.main() 1172