10a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport difflib 20a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport pprint 30a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport pickle 40a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport re 50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 70a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom copy import deepcopy 80a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test import test_support 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest 110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom .support import ( 130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao TestEquality, TestHashing, LoggingResult, ResultWithNoStartTestRunStopTestRun 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao) 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Test(object): 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "Keep these TestCase classes out of the main namespace" 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(unittest.TestCase): 210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def runTest(self): pass 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test1(self): pass 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Bar(Foo): 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test2(self): pass 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class LoggingTestCase(unittest.TestCase): 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """A test case which logs its calls.""" 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, events): 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao super(Test.LoggingTestCase, self).__init__('test') 320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.events = events 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.events.append('setUp') 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test(self): 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.events.append('test') 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def tearDown(self): 410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.events.append('tearDown') 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Test_TestCase(unittest.TestCase, TestEquality, TestHashing): 450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ### Set up attributes used by inherited tests 470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ################################################################ 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Used by TestHashing.test_hash and TestEquality.test_eq 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao eq_pairs = [(Test.Foo('test1'), Test.Foo('test1'))] 510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Used by TestEquality.test_ne 530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ne_pairs = [(Test.Foo('test1'), Test.Foo('runTest')) 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ,(Test.Foo('test1'), Test.Bar('test1')) 550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ,(Test.Foo('test1'), Test.Bar('test2'))] 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ################################################################ 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ### /Set up attributes used by inherited tests 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "class TestCase([methodName])" 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # ... 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "Each instance of TestCase will run a single test method: the 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # method named methodName." 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # ... 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "methodName defaults to "runTest"." 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Make sure it really is optional, and that it defaults to the proper 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # thing. 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_init__no_test_name(self): 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Test(unittest.TestCase): 720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def runTest(self): raise TypeError() 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test(self): pass 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(Test().id()[-13:], '.Test.runTest') 760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "class TestCase([methodName])" 780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # ... 790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "Each instance of TestCase will run a single test method: the 800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # method named methodName." 810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_init__test_name__valid(self): 820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Test(unittest.TestCase): 830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def runTest(self): raise TypeError() 840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test(self): pass 850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(Test('test').id()[-10:], '.Test.test') 870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "class TestCase([methodName])" 890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # ... 900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "Each instance of TestCase will run a single test method: the 910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # method named methodName." 920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_init__test_name__invalid(self): 930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Test(unittest.TestCase): 940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def runTest(self): raise TypeError() 950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test(self): pass 960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Test('testfoo') 990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except ValueError: 1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail("Failed to raise ValueError") 1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "Return the number of tests represented by the this test object. For 1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # TestCase instances, this will always be 1" 1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_countTestCases(self): 1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(unittest.TestCase): 1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test(self): pass 1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(Foo('test').countTestCases(), 1) 1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "Return the default type of test result object to be used to run this 1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # test. For TestCase instances, this will always be 1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # unittest.TestResult; subclasses of TestCase should 1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # override this as necessary." 1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_defaultTestResult(self): 1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(unittest.TestCase): 1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def runTest(self): 1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = Foo().defaultTestResult() 1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(type(result), unittest.TestResult) 1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "When a setUp() method is defined, the test runner will run that method 1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # prior to each test. Likewise, if a tearDown() method is defined, the 1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # test runner will invoke that method after each test. In the example, 1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # setUp() was used to create a fresh sequence for each test." 1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Make sure the proper call order is maintained, even if setUp() raises 1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # an exception. 1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_run_call_order__error_in_setUp(self): 1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao events = [] 1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = LoggingResult(events) 1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(Test.LoggingTestCase): 1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao super(Foo, self).setUp() 1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise RuntimeError('raised by Foo.setUp') 1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Foo(events).run(result) 1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = ['startTest', 'setUp', 'addError', 'stopTest'] 1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(events, expected) 1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "With a temporary result stopTestRun is called when setUp errors. 1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_run_call_order__error_in_setUp_default_result(self): 1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao events = [] 1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(Test.LoggingTestCase): 1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def defaultTestResult(self): 1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return LoggingResult(self.events) 1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def setUp(self): 1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao super(Foo, self).setUp() 1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise RuntimeError('raised by Foo.setUp') 1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Foo(events).run() 1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = ['startTestRun', 'startTest', 'setUp', 'addError', 1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'stopTest', 'stopTestRun'] 1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(events, expected) 1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "When a setUp() method is defined, the test runner will run that method 1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # prior to each test. Likewise, if a tearDown() method is defined, the 1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # test runner will invoke that method after each test. In the example, 1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # setUp() was used to create a fresh sequence for each test." 1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Make sure the proper call order is maintained, even if the test raises 1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # an error (as opposed to a failure). 1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_run_call_order__error_in_test(self): 1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao events = [] 1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = LoggingResult(events) 1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(Test.LoggingTestCase): 1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test(self): 1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao super(Foo, self).test() 1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise RuntimeError('raised by Foo.test') 1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown', 1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'stopTest'] 1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Foo(events).run(result) 1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(events, expected) 1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "With a default result, an error in the test still results in stopTestRun 1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # being called." 1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_run_call_order__error_in_test_default_result(self): 1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao events = [] 1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(Test.LoggingTestCase): 1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def defaultTestResult(self): 1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return LoggingResult(self.events) 1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test(self): 1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao super(Foo, self).test() 1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise RuntimeError('raised by Foo.test') 1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = ['startTestRun', 'startTest', 'setUp', 'test', 'addError', 1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'tearDown', 'stopTest', 'stopTestRun'] 1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Foo(events).run() 1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(events, expected) 1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "When a setUp() method is defined, the test runner will run that method 2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # prior to each test. Likewise, if a tearDown() method is defined, the 2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # test runner will invoke that method after each test. In the example, 2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # setUp() was used to create a fresh sequence for each test." 2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Make sure the proper call order is maintained, even if the test signals 2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # a failure (as opposed to an error). 2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_run_call_order__failure_in_test(self): 2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao events = [] 2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = LoggingResult(events) 2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(Test.LoggingTestCase): 2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test(self): 2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao super(Foo, self).test() 2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail('raised by Foo.test') 2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown', 2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'stopTest'] 2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Foo(events).run(result) 2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(events, expected) 2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "When a test fails with a default result stopTestRun is still called." 2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_run_call_order__failure_in_test_default_result(self): 2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(Test.LoggingTestCase): 2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def defaultTestResult(self): 2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return LoggingResult(self.events) 2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test(self): 2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao super(Foo, self).test() 2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail('raised by Foo.test') 2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = ['startTestRun', 'startTest', 'setUp', 'test', 'addFailure', 2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'tearDown', 'stopTest', 'stopTestRun'] 2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao events = [] 2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Foo(events).run() 2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(events, expected) 2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "When a setUp() method is defined, the test runner will run that method 2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # prior to each test. Likewise, if a tearDown() method is defined, the 2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # test runner will invoke that method after each test. In the example, 2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # setUp() was used to create a fresh sequence for each test." 2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Make sure the proper call order is maintained, even if tearDown() raises 2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # an exception. 2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_run_call_order__error_in_tearDown(self): 2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao events = [] 2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = LoggingResult(events) 2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(Test.LoggingTestCase): 2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def tearDown(self): 2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao super(Foo, self).tearDown() 2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise RuntimeError('raised by Foo.tearDown') 2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Foo(events).run(result) 2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError', 2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'stopTest'] 2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(events, expected) 2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "When tearDown errors with a default result stopTestRun is still called." 2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_run_call_order__error_in_tearDown_default_result(self): 2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(Test.LoggingTestCase): 2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def defaultTestResult(self): 2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return LoggingResult(self.events) 2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def tearDown(self): 2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao super(Foo, self).tearDown() 2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise RuntimeError('raised by Foo.tearDown') 2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao events = [] 2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Foo(events).run() 2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = ['startTestRun', 'startTest', 'setUp', 'test', 'tearDown', 2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'addError', 'stopTest', 'stopTestRun'] 2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(events, expected) 2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "TestCase.run() still works when the defaultTestResult is a TestResult 2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # that does not support startTestRun and stopTestRun. 2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_run_call_order_default_result(self): 2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(unittest.TestCase): 2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def defaultTestResult(self): 2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return ResultWithNoStartTestRunStopTestRun() 2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test(self): 2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Foo('test').run() 2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "This class attribute gives the exception raised by the test() method. 2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # If a test framework needs to use a specialized exception, possibly to 2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # carry additional information, it must subclass this exception in 2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # order to ``play fair'' with the framework. The initial value of this 2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # attribute is AssertionError" 2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_failureException__default(self): 2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(unittest.TestCase): 2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test(self): 2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(Foo('test').failureException is AssertionError) 2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "This class attribute gives the exception raised by the test() method. 2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # If a test framework needs to use a specialized exception, possibly to 3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # carry additional information, it must subclass this exception in 3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # order to ``play fair'' with the framework." 3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Make sure TestCase.run() respects the designated failureException 3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_failureException__subclassing__explicit_raise(self): 3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao events = [] 3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = LoggingResult(events) 3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(unittest.TestCase): 3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test(self): 3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise RuntimeError() 3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao failureException = RuntimeError 3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(Foo('test').failureException is RuntimeError) 3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Foo('test').run(result) 3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = ['startTest', 'addFailure', 'stopTest'] 3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(events, expected) 3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "This class attribute gives the exception raised by the test() method. 3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # If a test framework needs to use a specialized exception, possibly to 3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # carry additional information, it must subclass this exception in 3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # order to ``play fair'' with the framework." 3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Make sure TestCase.run() respects the designated failureException 3270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_failureException__subclassing__implicit_raise(self): 3280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao events = [] 3290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = LoggingResult(events) 3300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(unittest.TestCase): 3320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test(self): 3330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail("foo") 3340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao failureException = RuntimeError 3360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(Foo('test').failureException is RuntimeError) 3380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Foo('test').run(result) 3410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = ['startTest', 'addFailure', 'stopTest'] 3420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(events, expected) 3430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "The default implementation does nothing." 3450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_setUp(self): 3460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(unittest.TestCase): 3470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def runTest(self): 3480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 3490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # ... and nothing should happen 3510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Foo().setUp() 3520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "The default implementation does nothing." 3540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_tearDown(self): 3550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(unittest.TestCase): 3560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def runTest(self): 3570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 3580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # ... and nothing should happen 3600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Foo().tearDown() 3610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "Return a string identifying the specific test case." 3630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # 3640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Because of the vague nature of the docs, I'm not going to lock this 3650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # test down too much. Really all that can be asserted is that the id() 3660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # will be a string (either 8-byte or unicode -- again, because the docs 3670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # just say "string") 3680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_id(self): 3690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(unittest.TestCase): 3700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def runTest(self): 3710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 3720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIsInstance(Foo().id(), basestring) 3740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # "If result is omitted or None, a temporary result object is created 3760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # and used, but is not made available to the caller. As TestCase owns the 3770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # temporary result startTestRun and stopTestRun are called. 3780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_run__uses_defaultTestResult(self): 3800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao events = [] 3810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(unittest.TestCase): 3830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test(self): 3840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao events.append('test') 3850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def defaultTestResult(self): 3870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return LoggingResult(events) 3880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Make run() find a result object on its own 3900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Foo('test').run() 3910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = ['startTestRun', 'startTest', 'test', 'addSuccess', 3930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'stopTest', 'stopTestRun'] 3940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(events, expected) 3950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testShortDescriptionWithoutDocstring(self): 3970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIsNone(self.shortDescription()) 3980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao @unittest.skipIf(sys.flags.optimize >= 2, 4000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "Docstrings are omitted with -O2 and above") 4010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testShortDescriptionWithOneLineDocstring(self): 4020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Tests shortDescription() for a method with a docstring.""" 4030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual( 4040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.shortDescription(), 4050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Tests shortDescription() for a method with a docstring.') 4060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao @unittest.skipIf(sys.flags.optimize >= 2, 4080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "Docstrings are omitted with -O2 and above") 4090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testShortDescriptionWithMultiLineDocstring(self): 4100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Tests shortDescription() for a method with a longer docstring. 4110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This method ensures that only the first line of a docstring is 4130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao returned used in the short description, no matter how long the 4140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao whole thing is. 4150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 4160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual( 4170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.shortDescription(), 4180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'Tests shortDescription() for a method with a longer ' 4190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'docstring.') 4200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAddTypeEqualityFunc(self): 4220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class SadSnake(object): 4230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Dummy class for test_addTypeEqualityFunc.""" 4240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s1, s2 = SadSnake(), SadSnake() 4250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEqual(s1, s2) 4260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def AllSnakesCreatedEqual(a, b, msg=None): 4270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return type(a) is type(b) is SadSnake 4280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.addTypeEqualityFunc(SadSnake, AllSnakesCreatedEqual) 4290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s1, s2) 4300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # No this doesn't clean up and remove the SadSnake equality func 4310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # from this TestCase instance but since its a local nothing else 4320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # will ever notice that. 4330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertIs(self): 4350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao thing = object() 4360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIs(thing, thing) 4370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertIs, thing, object()) 4380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertIsNot(self): 4400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao thing = object() 4410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIsNot(thing, object()) 4420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertIsNot, thing, thing) 4430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertIsInstance(self): 4450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao thing = [] 4460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIsInstance(thing, list) 4470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertIsInstance, 4480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao thing, dict) 4490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertNotIsInstance(self): 4510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao thing = [] 4520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIsInstance(thing, dict) 4530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertNotIsInstance, 4540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao thing, list) 4550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertIn(self): 4570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'} 4580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn('a', 'abc') 4600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(2, [1, 2, 3]) 4610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn('monkey', animals) 4620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn('d', 'abc') 4640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn(0, [1, 2, 3]) 4650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn('otter', animals) 4660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertIn, 'x', 'abc') 4680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertIn, 4, [1, 2, 3]) 4690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertIn, 'elephant', 4700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao animals) 4710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertNotIn, 'c', 'abc') 4730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertNotIn, 1, [1, 2, 3]) 4740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertNotIn, 'cow', 4750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao animals) 4760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertDictContainsSubset(self): 4780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertDictContainsSubset({}, {}) 4790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertDictContainsSubset({}, {'a': 1}) 4800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertDictContainsSubset({'a': 1}, {'a': 1}) 4810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertDictContainsSubset({'a': 1}, {'a': 1, 'b': 2}) 4820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertDictContainsSubset({'a': 1, 'b': 2}, {'a': 1, 'b': 2}) 4830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with self.assertRaises(self.failureException): 4850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertDictContainsSubset({1: "one"}, {}) 4860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with self.assertRaises(self.failureException): 4880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertDictContainsSubset({'a': 2}, {'a': 1}) 4890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with self.assertRaises(self.failureException): 4910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertDictContainsSubset({'c': 1}, {'a': 1}) 4920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with self.assertRaises(self.failureException): 4940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1}) 4950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with self.assertRaises(self.failureException): 4970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertDictContainsSubset({'a': 1, 'c': 1}, {'a': 1}) 4980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with test_support.check_warnings(("", UnicodeWarning)): 5000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao one = ''.join(chr(i) for i in range(255)) 5010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # this used to cause a UnicodeDecodeError constructing the failure msg 5020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with self.assertRaises(self.failureException): 5030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertDictContainsSubset({'foo': one}, {'foo': u'\uFFFD'}) 5040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertEqual(self): 5060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao equal_pairs = [ 5070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ((), ()), 5080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ({}, {}), 5090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ([], []), 5100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (set(), set()), 5110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (frozenset(), frozenset())] 5120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for a, b in equal_pairs: 5130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # This mess of try excepts is to test the assertEqual behavior 5140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # itself. 5150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 5160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(a, b) 5170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except self.failureException: 5180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail('assertEqual(%r, %r) failed' % (a, b)) 5190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 5200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(a, b, msg='foo') 5210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except self.failureException: 5220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail('assertEqual(%r, %r) with msg= failed' % (a, b)) 5230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 5240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(a, b, 'foo') 5250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except self.failureException: 5260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail('assertEqual(%r, %r) with third parameter failed' % 5270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (a, b)) 5280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao unequal_pairs = [ 5300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ((), []), 5310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ({}, set()), 5320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (set([4,1]), frozenset([4,2])), 5330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (frozenset([4,5]), set([2,3])), 5340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (set([3,4]), set([5,4]))] 5350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for a, b in unequal_pairs: 5360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertEqual, a, b) 5370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertEqual, a, b, 5380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'foo') 5390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertEqual, a, b, 5400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao msg='foo') 5410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testEquality(self): 5430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertListEqual([], []) 5440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTupleEqual((), ()) 5450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertSequenceEqual([], ()) 5460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = [0, 'a', []] 5480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao b = [] 5490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(unittest.TestCase.failureException, 5500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertListEqual, a, b) 5510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(unittest.TestCase.failureException, 5520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertListEqual, tuple(a), tuple(b)) 5530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(unittest.TestCase.failureException, 5540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertSequenceEqual, a, tuple(b)) 5550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao b.extend(a) 5570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertListEqual(a, b) 5580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTupleEqual(tuple(a), tuple(b)) 5590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertSequenceEqual(a, tuple(b)) 5600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertSequenceEqual(tuple(a), b) 5610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertListEqual, 5630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a, tuple(b)) 5640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertTupleEqual, 5650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao tuple(a), b) 5660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertListEqual, None, b) 5670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertTupleEqual, None, 5680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao tuple(b)) 5690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertSequenceEqual, 5700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao None, tuple(b)) 5710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertListEqual, 1, 1) 5720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertTupleEqual, 1, 1) 5730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertSequenceEqual, 5740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1, 1) 5750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertDictEqual({}, {}) 5770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao c = { 'x': 1 } 5790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = {} 5800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(unittest.TestCase.failureException, 5810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertDictEqual, c, d) 5820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d.update(c) 5840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertDictEqual(c, d) 5850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d['x'] = 0 5870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(unittest.TestCase.failureException, 5880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertDictEqual, c, d, 'These are unequal') 5890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertDictEqual, None, d) 5910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertDictEqual, [], d) 5920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertDictEqual, 1, 1) 5930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertSequenceEqualMaxDiff(self): 5950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self.maxDiff, 80*8) 5960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao seq1 = 'a' + 'x' * 80**2 5970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao seq2 = 'b' + 'x' * 80**2 5980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao diff = '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(), 5990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pprint.pformat(seq2).splitlines())) 6000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # the +1 is the leading \n added by assertSequenceEqual 6010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao omitted = unittest.case.DIFF_OMITTED % (len(diff) + 1,) 6020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.maxDiff = len(diff)//2 6040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 6050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertSequenceEqual(seq1, seq2) 6060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except self.failureException as e: 6070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao msg = e.args[0] 6080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 6090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail('assertSequenceEqual did not fail.') 6100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(len(msg) < len(diff)) 6110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn(omitted, msg) 6120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.maxDiff = len(diff) * 2 6140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 6150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertSequenceEqual(seq1, seq2) 6160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except self.failureException as e: 6170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao msg = e.args[0] 6180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 6190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail('assertSequenceEqual did not fail.') 6200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(len(msg) > len(diff)) 6210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn(omitted, msg) 6220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.maxDiff = None 6240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 6250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertSequenceEqual(seq1, seq2) 6260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except self.failureException as e: 6270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao msg = e.args[0] 6280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 6290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail('assertSequenceEqual did not fail.') 6300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(len(msg) > len(diff)) 6310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn(omitted, msg) 6320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testTruncateMessage(self): 6340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.maxDiff = 1 6350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao message = self._truncateMessage('foo', 'bar') 6360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao omitted = unittest.case.DIFF_OMITTED % len('bar') 6370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(message, 'foo' + omitted) 6380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.maxDiff = None 6400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao message = self._truncateMessage('foo', 'bar') 6410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(message, 'foobar') 6420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.maxDiff = 4 6440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao message = self._truncateMessage('foo', 'bar') 6450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(message, 'foobar') 6460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertDictEqualTruncates(self): 6480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test = unittest.TestCase('assertEqual') 6490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def truncate(msg, diff): 6500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 'foo' 6510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test._truncateMessage = truncate 6520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 6530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test.assertDictEqual({}, {1: 0}) 6540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except self.failureException as e: 6550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(str(e), 'foo') 6560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 6570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail('assertDictEqual did not fail') 6580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertMultiLineEqualTruncates(self): 6600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test = unittest.TestCase('assertEqual') 6610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def truncate(msg, diff): 6620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return 'foo' 6630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test._truncateMessage = truncate 6640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 6650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test.assertMultiLineEqual('foo', 'bar') 6660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except self.failureException as e: 6670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(str(e), 'foo') 6680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao else: 6690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.fail('assertMultiLineEqual did not fail') 6700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertEqual_diffThreshold(self): 6720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # check threshold value 6730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(self._diffThreshold, 2**16) 6740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # disable madDiff to get diff markers 6750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.maxDiff = None 6760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # set a lower threshold value and add a cleanup to restore it 6780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao old_threshold = self._diffThreshold 6790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._diffThreshold = 2**8 6800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.addCleanup(lambda: setattr(self, '_diffThreshold', old_threshold)) 6810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # under the threshold: diff marker (^) in error message 6830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = u'x' * (2**7) 6840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with self.assertRaises(self.failureException) as cm: 6850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s + 'a', s + 'b') 6860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIn('^', str(cm.exception)) 6870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s + 'a', s + 'a') 6880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # over the threshold: diff not used and marker (^) not in error message 6900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s = u'x' * (2**9) 6910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # if the path that uses difflib is taken, _truncateMessage will be 6920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # called -- replace it with explodingTruncation to verify that this 6930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # doesn't happen 6940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def explodingTruncation(message, diff): 6950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise SystemError('this should not be raised') 6960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao old_truncate = self._truncateMessage 6970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._truncateMessage = explodingTruncation 6980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.addCleanup(lambda: setattr(self, '_truncateMessage', old_truncate)) 6990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao s1, s2 = s + 'a', s + 'b' 7010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with self.assertRaises(self.failureException) as cm: 7020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s1, s2) 7030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn('^', str(cm.exception)) 7040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(str(cm.exception), '%r != %r' % (s1, s2)) 7050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(s + 'a', s + 'a') 7060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertItemsEqual(self): 7080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = object() 7090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertItemsEqual([1, 2, 3], [3, 2, 1]) 7100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertItemsEqual(['foo', 'bar', 'baz'], ['bar', 'baz', 'foo']) 7110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertItemsEqual([a, a, 2, 2, 3], (a, 2, 3, a, 2)) 7120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertItemsEqual([1, "2", "a", "a"], ["a", "2", True, "a"]) 7130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertItemsEqual, 7140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao [1, 2] + [3] * 100, [1] * 100 + [2, 3]) 7150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertItemsEqual, 7160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao [1, "2", "a", "a"], ["a", "2", True, 1]) 7170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertItemsEqual, 7180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao [10], [10, 11]) 7190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertItemsEqual, 7200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao [10, 11], [10]) 7210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertItemsEqual, 7220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao [10, 11, 10], [10, 11]) 7230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Test that sequences of unhashable objects can be tested for sameness: 7250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertItemsEqual([[1, 2], [3, 4], 0], [False, [3, 4], [1, 2]]) 7260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Test that iterator of unhashable objects can be tested for sameness: 7270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertItemsEqual(iter([1, 2, [], 3, 4]), 7280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao iter([1, 2, [], 3, 4])) 7290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # hashable types, but not orderable 7310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertItemsEqual, 7320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao [], [divmod, 'x', 1, 5j, 2j, frozenset()]) 7330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # comparing dicts 7340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertItemsEqual([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}]) 7350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # comparing heterogenous non-hashable sequences 7360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertItemsEqual([1, 'x', divmod, []], [divmod, [], 'x', 1]) 7370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertItemsEqual, 7380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao [], [divmod, [], 'x', 1, 5j, 2j, set()]) 7390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertItemsEqual, 7400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao [[1]], [[2]]) 7410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Same elements, but not same sequence length 7430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertItemsEqual, 7440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao [1, 1, 2], [2, 1]) 7450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertItemsEqual, 7460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao [1, 1, "2", "a", "a"], ["2", "2", True, "a"]) 7470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertItemsEqual, 7480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao [1, {'b': 2}, None, True], [{'b': 2}, True, None]) 7490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Same elements which don't reliably compare, in 7510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # different order, see issue 10242 7520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a = [{2,4}, {1,2}] 7530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao b = a[::-1] 7540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertItemsEqual(a, b) 7550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # test utility functions supporting assertItemsEqual() 7570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao diffs = set(unittest.util._count_diff_all_purpose('aaabccd', 'abbbcce')) 7590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = {(3,1,'a'), (1,3,'b'), (1,0,'d'), (0,1,'e')} 7600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(diffs, expected) 7610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao diffs = unittest.util._count_diff_all_purpose([[]], []) 7630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(diffs, [(1, 0, [])]) 7640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao diffs = set(unittest.util._count_diff_hashable('aaabccd', 'abbbcce')) 7660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao expected = {(3,1,'a'), (1,3,'b'), (1,0,'d'), (0,1,'e')} 7670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(diffs, expected) 7680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertSetEqual(self): 7700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set1 = set() 7710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set2 = set() 7720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertSetEqual(set1, set2) 7730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertSetEqual, None, set2) 7750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertSetEqual, [], set2) 7760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertSetEqual, set1, None) 7770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertSetEqual, set1, []) 7780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set1 = set(['a']) 7800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set2 = set() 7810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) 7820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set1 = set(['a']) 7840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set2 = set(['a']) 7850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertSetEqual(set1, set2) 7860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set1 = set(['a']) 7880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set2 = set(['a', 'b']) 7890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) 7900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set1 = set(['a']) 7920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set2 = frozenset(['a', 'b']) 7930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) 7940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set1 = set(['a', 'b']) 7960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set2 = frozenset(['a', 'b']) 7970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertSetEqual(set1, set2) 7980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set1 = set() 8000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set2 = "foo" 8010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) 8020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertSetEqual, set2, set1) 8030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # make sure any string formatting is tuple-safe 8050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set1 = set([(0, 1), (2, 3)]) 8060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao set2 = set([(4, 5)]) 8070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) 8080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testInequality(self): 8100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Try ints 8110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreater(2, 1) 8120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreaterEqual(2, 1) 8130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreaterEqual(1, 1) 8140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLess(1, 2) 8150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLessEqual(1, 2) 8160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLessEqual(1, 1) 8170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreater, 1, 2) 8180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreater, 1, 1) 8190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreaterEqual, 1, 2) 8200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLess, 2, 1) 8210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLess, 1, 1) 8220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLessEqual, 2, 1) 8230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Try Floats 8250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreater(1.1, 1.0) 8260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreaterEqual(1.1, 1.0) 8270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreaterEqual(1.0, 1.0) 8280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLess(1.0, 1.1) 8290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLessEqual(1.0, 1.1) 8300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLessEqual(1.0, 1.0) 8310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.1) 8320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.0) 8330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreaterEqual, 1.0, 1.1) 8340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLess, 1.1, 1.0) 8350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLess, 1.0, 1.0) 8360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLessEqual, 1.1, 1.0) 8370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Try Strings 8390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreater('bug', 'ant') 8400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreaterEqual('bug', 'ant') 8410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreaterEqual('ant', 'ant') 8420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLess('ant', 'bug') 8430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLessEqual('ant', 'bug') 8440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLessEqual('ant', 'ant') 8450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreater, 'ant', 'bug') 8460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreater, 'ant', 'ant') 8470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant', 'bug') 8480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLess, 'bug', 'ant') 8490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLess, 'ant', 'ant') 8500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLessEqual, 'bug', 'ant') 8510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Try Unicode 8530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreater(u'bug', u'ant') 8540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreaterEqual(u'bug', u'ant') 8550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreaterEqual(u'ant', u'ant') 8560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLess(u'ant', u'bug') 8570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLessEqual(u'ant', u'bug') 8580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLessEqual(u'ant', u'ant') 8590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreater, u'ant', u'bug') 8600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreater, u'ant', u'ant') 8610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreaterEqual, u'ant', 8620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao u'bug') 8630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLess, u'bug', u'ant') 8640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLess, u'ant', u'ant') 8650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLessEqual, u'bug', u'ant') 8660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Try Mixed String/Unicode 8680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreater('bug', u'ant') 8690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreater(u'bug', 'ant') 8700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreaterEqual('bug', u'ant') 8710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreaterEqual(u'bug', 'ant') 8720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreaterEqual('ant', u'ant') 8730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertGreaterEqual(u'ant', 'ant') 8740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLess('ant', u'bug') 8750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLess(u'ant', 'bug') 8760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLessEqual('ant', u'bug') 8770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLessEqual(u'ant', 'bug') 8780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLessEqual('ant', u'ant') 8790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertLessEqual(u'ant', 'ant') 8800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreater, 'ant', u'bug') 8810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreater, u'ant', 'bug') 8820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreater, 'ant', u'ant') 8830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreater, u'ant', 'ant') 8840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant', 8850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao u'bug') 8860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertGreaterEqual, u'ant', 8870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'bug') 8880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLess, 'bug', u'ant') 8890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLess, u'bug', 'ant') 8900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLess, 'ant', u'ant') 8910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLess, u'ant', 'ant') 8920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLessEqual, 'bug', u'ant') 8930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertLessEqual, u'bug', 'ant') 8940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 8950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertMultiLineEqual(self): 8960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sample_text = b"""\ 8970a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohttp://www.python.org/doc/2.3/lib/module-unittest.html 8980a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotest case 8990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao A test case is the smallest unit of testing. [...] 9000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""" 9010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao revised_sample_text = b"""\ 9020a8c90248264a8b26970b4473770bcc3df8515fJosh Gaohttp://www.python.org/doc/2.4.1/lib/module-unittest.html 9030a8c90248264a8b26970b4473770bcc3df8515fJosh Gaotest case 9040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao A test case is the smallest unit of testing. [...] You may provide your 9050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao own implementation that does not subclass from TestCase, of course. 9060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""" 9070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sample_text_error = b"""\ 9080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao- http://www.python.org/doc/2.3/lib/module-unittest.html 9090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao? ^ 9100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao+ http://www.python.org/doc/2.4.1/lib/module-unittest.html 9110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao? ^^^ 9120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test case 9130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao- A test case is the smallest unit of testing. [...] 9140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao+ A test case is the smallest unit of testing. [...] You may provide your 9150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao? +++++++++++++++++++++ 9160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao+ own implementation that does not subclass from TestCase, of course. 9170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""" 9180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.maxDiff = None 9190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for type_changer in (lambda x: x, lambda x: x.decode('utf8')): 9200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 9210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertMultiLineEqual(type_changer(sample_text), 9220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao type_changer(revised_sample_text)) 9230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except self.failureException, e: 9240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # need to remove the first line of the error message 9250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao error = str(e).encode('utf8').split('\n', 1)[1] 9260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # assertMultiLineEqual is hooked up as the default for 9280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # unicode strings - so we can't use it for this check 9290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(sample_text_error == error) 9300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAsertEqualSingleLine(self): 9320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sample_text = u"laden swallows fly slowly" 9330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao revised_sample_text = u"unladen swallows fly quickly" 9340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao sample_text_error = """\ 9350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao- laden swallows fly slowly 9360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao? ^^^^ 9370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao+ unladen swallows fly quickly 9380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao? ++ ^^^^^ 9390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao""" 9400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao try: 9410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(sample_text, revised_sample_text) 9420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao except self.failureException as e: 9430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao error = str(e).split('\n', 1)[1] 9440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(sample_text_error == error) 9450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertIsNone(self): 9470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIsNone(None) 9480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertIsNone, False) 9490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIsNotNone('DjZoPloGears on Rails') 9500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertIsNotNone, None) 9510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertRegexpMatches(self): 9530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRegexpMatches('asdfabasdf', r'ab+') 9540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(self.failureException, self.assertRegexpMatches, 9550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 'saaas', r'aaaa') 9560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertRaisesRegexp(self): 9580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class ExceptionMock(Exception): 9590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 9600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def Stub(): 9620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ExceptionMock('We expect') 9630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaisesRegexp(ExceptionMock, re.compile('expect$'), Stub) 9650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaisesRegexp(ExceptionMock, 'expect$', Stub) 9660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaisesRegexp(ExceptionMock, u'expect$', Stub) 9670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertNotRaisesRegexp(self): 9690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaisesRegexp( 9700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.failureException, '^Exception not raised$', 9710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaisesRegexp, Exception, re.compile('x'), 9720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao lambda: None) 9730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaisesRegexp( 9740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.failureException, '^Exception not raised$', 9750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaisesRegexp, Exception, 'x', 9760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao lambda: None) 9770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaisesRegexp( 9780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.failureException, '^Exception not raised$', 9790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaisesRegexp, Exception, u'x', 9800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao lambda: None) 9810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertRaisesRegexpMismatch(self): 9830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def Stub(): 9840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise Exception('Unexpected') 9850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 9860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaisesRegexp( 9870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.failureException, 9880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao r'"\^Expected\$" does not match "Unexpected"', 9890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaisesRegexp, Exception, '^Expected$', 9900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Stub) 9910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaisesRegexp( 9920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.failureException, 9930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao r'"\^Expected\$" does not match "Unexpected"', 9940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaisesRegexp, Exception, u'^Expected$', 9950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Stub) 9960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaisesRegexp( 9970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.failureException, 9980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao r'"\^Expected\$" does not match "Unexpected"', 9990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaisesRegexp, Exception, 10000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao re.compile('^Expected$'), Stub) 10010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testAssertRaisesExcValue(self): 10030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class ExceptionMock(Exception): 10040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 10050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def Stub(foo): 10070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ExceptionMock(foo) 10080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao v = "particular value" 10090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao ctx = self.assertRaises(ExceptionMock) 10110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with ctx: 10120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Stub(v) 10130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao e = ctx.exception 10140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertIsInstance(e, ExceptionMock) 10150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(e.args[0], v) 10160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testSynonymAssertMethodNames(self): 10180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Test undocumented method name synonyms. 10190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Please do not use these methods names in your own code. 10210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao This test confirms their continued existence and functionality 10230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao in order to avoid breaking existing code. 10240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 10250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEquals(3, 5) 10260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEquals(3, 3) 10270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertAlmostEquals(2.0, 2.0) 10280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotAlmostEquals(3.0, 5.0) 10290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assert_(True) 10300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testPendingDeprecationMethodNames(self): 10320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """Test fail* methods pending deprecation, they will warn in 3.2. 10330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao Do not use these methods. They will go away in 3.3. 10350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao """ 10360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with test_support.check_warnings(): 10370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.failIfEqual(3, 5) 10380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.failUnlessEqual(3, 3) 10390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.failUnlessAlmostEqual(2.0, 2.0) 10400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.failIfAlmostEqual(3.0, 5.0) 10410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.failUnless(True) 10420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.failUnlessRaises(TypeError, lambda _: 3.14 + u'spam') 10430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.failIf(False) 10440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testDeepcopy(self): 10460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Issue: 5660 10470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class TestableTest(unittest.TestCase): 10480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testNothing(self): 10490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 10500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test = TestableTest('testNothing') 10520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # This shouldn't blow up 10540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao deepcopy(test) 10550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testKeyboardInterrupt(self): 10570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _raise(self=None): 10580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise KeyboardInterrupt 10590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def nothing(self): 10600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 10610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Test1(unittest.TestCase): 10630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_something = _raise 10640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Test2(unittest.TestCase): 10660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao setUp = _raise 10670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_something = nothing 10680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Test3(unittest.TestCase): 10700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_something = nothing 10710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao tearDown = _raise 10720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Test4(unittest.TestCase): 10740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_something(self): 10750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.addCleanup(_raise) 10760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for klass in (Test1, Test2, Test3, Test4): 10780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao with self.assertRaises(KeyboardInterrupt): 10790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao klass('test_something').run() 10800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testSystemExit(self): 10820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _raise(self=None): 10830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise SystemExit 10840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def nothing(self): 10850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 10860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Test1(unittest.TestCase): 10880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_something = _raise 10890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Test2(unittest.TestCase): 10910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao setUp = _raise 10920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_something = nothing 10930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Test3(unittest.TestCase): 10950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_something = nothing 10960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao tearDown = _raise 10970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 10980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Test4(unittest.TestCase): 10990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_something(self): 11000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.addCleanup(_raise) 11010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for klass in (Test1, Test2, Test3, Test4): 11030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao result = unittest.TestResult() 11040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao klass('test_something').run(result) 11050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(result.errors), 1) 11060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(result.testsRun, 1) 11070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def testPickle(self): 11090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Issue 10326 11100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Can't use TestCase classes defined in Test class as 11120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # pickle does not work with inner classes 11130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test = unittest.TestCase('run') 11140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for protocol in range(pickle.HIGHEST_PROTOCOL + 1): 11150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # blew up prior to fix 11170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pickled_test = pickle.dumps(test, protocol=protocol) 11180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao unpickled_test = pickle.loads(pickled_test) 11200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(test, unpickled_test) 11210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 11230a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == '__main__': 11240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao unittest.main() 1125