175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenimport difflib 275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenimport pprint 375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenimport re 475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenfrom copy import deepcopy 675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenimport unittest2 875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenfrom unittest2.test.support import ( 1075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen OldTestResult, EqualityMixin, HashingMixin, LoggingResult 1175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen) 1275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 1375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 1475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenclass MyException(Exception): 1575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 1675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 1775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 1875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenclass Test(object): 1975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen "Keep these TestCase classes out of the main namespace" 2075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 2175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(unittest2.TestCase): 2275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def runTest(self): pass 2375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test1(self): pass 2475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 2575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Bar(Foo): 2675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test2(self): pass 2775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 2875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class LoggingTestCase(unittest2.TestCase): 2975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen """A test case which logs its calls.""" 3075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 3175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def __init__(self, events): 3275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(Test.LoggingTestCase, self).__init__('test') 3375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.events = events 3475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 3575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUp(self): 3675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.events.append('setUp') 3775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 3875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(self): 3975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.events.append('test') 4075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 4175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDown(self): 4275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.events.append('tearDown') 4375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 4475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 4575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 4675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenclass TestCleanUp(unittest2.TestCase): 4775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 4875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testCleanUp(self): 4975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class TestableTest(unittest2.TestCase): 5075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testNothing(self): 5175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 5275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 5375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test = TestableTest('testNothing') 5475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(test._cleanups, []) 5575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 5675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen cleanups = [] 5775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 5875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def cleanup1(*args, **kwargs): 5975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen cleanups.append((1, args, kwargs)) 6075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 6175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def cleanup2(*args, **kwargs): 6275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen cleanups.append((2, args, kwargs)) 6375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 6475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test.addCleanup(cleanup1, 1, 2, 3, four='hello', five='goodbye') 6575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test.addCleanup(cleanup2) 6675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 6775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(test._cleanups, 6875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen [(cleanup1, (1, 2, 3), dict(four='hello', five='goodbye')), 6975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen (cleanup2, (), {})]) 7075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 7175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = test.doCleanups() 7275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTrue(result) 7375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 7475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(cleanups, [(2, (), {}), (1, (1, 2, 3), dict(four='hello', five='goodbye'))]) 7575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 7675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testCleanUpWithErrors(self): 7775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class TestableTest(unittest2.TestCase): 7875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testNothing(self): 7975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 8075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 8175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class MockResult(object): 8275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen errors = [] 8375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def addError(self, test, exc_info): 8475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.errors.append((test, exc_info)) 8575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 8675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = MockResult() 8775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test = TestableTest('testNothing') 8875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test._resultForDoCleanups = result 8975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 9075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen exc1 = Exception('foo') 9175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen exc2 = Exception('bar') 9275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def cleanup1(): 9375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise exc1 9475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 9575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def cleanup2(): 9675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise exc2 9775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 9875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test.addCleanup(cleanup1) 9975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test.addCleanup(cleanup2) 10075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 10175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertFalse(test.doCleanups()) 10275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 10375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen (test1, (Type1, instance1, _)), (test2, (Type2, instance2, _)) = reversed(MockResult.errors) 10475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual((test1, Type1, instance1), (test, Exception, exc1)) 10575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual((test2, Type2, instance2), (test, Exception, exc2)) 10675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 10775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testCleanupInRun(self): 10875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen blowUp = False 10975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering = [] 11075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 11175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class TestableTest(unittest2.TestCase): 11275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUp(self): 11375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering.append('setUp') 11475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen if blowUp: 11575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise Exception('foo') 11675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 11775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testNothing(self): 11875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering.append('test') 11975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 12075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDown(self): 12175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering.append('tearDown') 12275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 12375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test = TestableTest('testNothing') 12475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 12575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def cleanup1(): 12675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering.append('cleanup1') 12775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def cleanup2(): 12875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering.append('cleanup2') 12975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test.addCleanup(cleanup1) 13075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test.addCleanup(cleanup2) 13175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 13275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def success(some_test): 13375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(some_test, test) 13475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering.append('success') 13575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 13675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = unittest2.TestResult() 13775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result.addSuccess = success 13875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 13975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test.run(result) 14075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(ordering, ['setUp', 'test', 'tearDown', 14175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'cleanup2', 'cleanup1', 'success']) 14275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 14375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen blowUp = True 14475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering = [] 14575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test = TestableTest('testNothing') 14675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test.addCleanup(cleanup1) 14775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test.run(result) 14875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(ordering, ['setUp', 'cleanup1']) 14975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 15075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testTestCaseDebugExecutesCleanups(self): 15175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering = [] 15275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 15375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class TestableTest(unittest2.TestCase): 15475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUp(self): 15575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering.append('setUp') 15675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.addCleanup(cleanup1) 15775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 15875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testNothing(self): 15975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering.append('test') 16075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 16175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDown(self): 16275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering.append('tearDown') 16375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 16475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test = TestableTest('testNothing') 16575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 16675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def cleanup1(): 16775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering.append('cleanup1') 16875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test.addCleanup(cleanup2) 16975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def cleanup2(): 17075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering.append('cleanup2') 17175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 17275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test.debug() 17375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(ordering, ['setUp', 'test', 'tearDown', 'cleanup1', 'cleanup2']) 17475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 17575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 17675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenclass Test_TestCase(unittest2.TestCase, EqualityMixin, HashingMixin): 17775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 17875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ### Set up attributes used by inherited tests 17975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ################################################################ 18075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 18175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Used by HashingMixin.test_hash and EqualityMixin.test_eq 18275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen eq_pairs = [(Test.Foo('test1'), Test.Foo('test1'))] 18375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 18475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Used by EqualityMixin.test_ne 18575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ne_pairs = [(Test.Foo('test1'), Test.Foo('runTest')), 18675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen (Test.Foo('test1'), Test.Bar('test1')), 18775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen (Test.Foo('test1'), Test.Bar('test2'))] 18875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 18975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ################################################################ 19075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ### /Set up attributes used by inherited tests 19175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 19275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 19375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "class TestCase([methodName])" 19475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # ... 19575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "Each instance of TestCase will run a single test method: the 19675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # method named methodName." 19775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # ... 19875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "methodName defaults to "runTest"." 19975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # 20075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Make sure it really is optional, and that it defaults to the proper 20175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # thing. 20275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_init__no_test_name(self): 20375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 20475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def runTest(self): raise MyException() 20575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(self): pass 20675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 20775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(Test().id()[-13:], '.Test.runTest') 20875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 20975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "class TestCase([methodName])" 21075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # ... 21175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "Each instance of TestCase will run a single test method: the 21275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # method named methodName." 21375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_init__test_name__valid(self): 21475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 21575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def runTest(self): raise MyException() 21675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(self): pass 21775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 21875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(Test('test').id()[-10:], '.Test.test') 21975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 22075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "class unittest2.TestCase([methodName])" 22175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # ... 22275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "Each instance of TestCase will run a single test method: the 22375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # method named methodName." 22475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_init__test_name__invalid(self): 22575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 22675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def runTest(self): raise MyException() 22775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(self): pass 22875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 22975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen try: 23075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test('testfoo') 23175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen except ValueError: 23275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 23375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen else: 23475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail("Failed to raise ValueError") 23575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 23675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "Return the number of tests represented by the this test object. For 23775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # TestCase instances, this will always be 1" 23875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_countTestCases(self): 23975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(unittest2.TestCase): 24075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(self): pass 24175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 24275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(Foo('test').countTestCases(), 1) 24375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 24475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "Return the default type of test result object to be used to run this 24575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # test. For TestCase instances, this will always be 24675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # unittest2.TestResult; subclasses of TestCase should 24775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # override this as necessary." 24875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_defaultTestResult(self): 24975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(unittest2.TestCase): 25075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def runTest(self): 25175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 25275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 25375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = Foo().defaultTestResult() 25475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(type(result), unittest2.TestResult) 25575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 25675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "When a setUp() method is defined, the test runner will run that method 25775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # prior to each test. Likewise, if a tearDown() method is defined, the 25875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # test runner will invoke that method after each test. In the example, 25975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # setUp() was used to create a fresh sequence for each test." 26075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # 26175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Make sure the proper call order is maintained, even if setUp() raises 26275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # an exception. 26375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_run_call_order__error_in_setUp(self): 26475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events = [] 26575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = LoggingResult(events) 26675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 26775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(Test.LoggingTestCase): 26875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUp(self): 26975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(Foo, self).setUp() 27075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise RuntimeError('raised by Foo.setUp') 27175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 27275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Foo(events).run(result) 27375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen expected = ['startTest', 'setUp', 'addError', 'stopTest'] 27475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(events, expected) 27575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 27675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "With a temporary result stopTestRun is called when setUp errors. 27775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_run_call_order__error_in_setUp_default_result(self): 27875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events = [] 27975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 28075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(Test.LoggingTestCase): 28175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def defaultTestResult(self): 28275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return LoggingResult(self.events) 28375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 28475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUp(self): 28575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(Foo, self).setUp() 28675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise RuntimeError('raised by Foo.setUp') 28775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 28875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Foo(events).run() 28975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen expected = ['startTestRun', 'startTest', 'setUp', 'addError', 29075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'stopTest', 'stopTestRun'] 29175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(events, expected) 29275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 29375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "When a setUp() method is defined, the test runner will run that method 29475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # prior to each test. Likewise, if a tearDown() method is defined, the 29575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # test runner will invoke that method after each test. In the example, 29675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # setUp() was used to create a fresh sequence for each test." 29775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # 29875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Make sure the proper call order is maintained, even if the test raises 29975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # an error (as opposed to a failure). 30075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_run_call_order__error_in_test(self): 30175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events = [] 30275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = LoggingResult(events) 30375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 30475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(Test.LoggingTestCase): 30575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(self): 30675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(Foo, self).test() 30775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise RuntimeError('raised by Foo.test') 30875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 30975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown', 31075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'stopTest'] 31175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Foo(events).run(result) 31275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(events, expected) 31375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 31475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "With a default result, an error in the test still results in stopTestRun 31575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # being called." 31675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_run_call_order__error_in_test_default_result(self): 31775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events = [] 31875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 31975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(Test.LoggingTestCase): 32075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def defaultTestResult(self): 32175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return LoggingResult(self.events) 32275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 32375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(self): 32475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(Foo, self).test() 32575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise RuntimeError('raised by Foo.test') 32675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 32775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen expected = ['startTestRun', 'startTest', 'setUp', 'test', 'addError', 32875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'tearDown', 'stopTest', 'stopTestRun'] 32975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Foo(events).run() 33075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(events, expected) 33175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 33275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "When a setUp() method is defined, the test runner will run that method 33375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # prior to each test. Likewise, if a tearDown() method is defined, the 33475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # test runner will invoke that method after each test. In the example, 33575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # setUp() was used to create a fresh sequence for each test." 33675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # 33775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Make sure the proper call order is maintained, even if the test signals 33875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # a failure (as opposed to an error). 33975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_run_call_order__failure_in_test(self): 34075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events = [] 34175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = LoggingResult(events) 34275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 34375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(Test.LoggingTestCase): 34475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(self): 34575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(Foo, self).test() 34675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail('raised by Foo.test') 34775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 34875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown', 34975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'stopTest'] 35075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Foo(events).run(result) 35175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(events, expected) 35275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 35375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "When a test fails with a default result stopTestRun is still called." 35475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_run_call_order__failure_in_test_default_result(self): 35575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 35675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(Test.LoggingTestCase): 35775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def defaultTestResult(self): 35875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return LoggingResult(self.events) 35975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(self): 36075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(Foo, self).test() 36175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail('raised by Foo.test') 36275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 36375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen expected = ['startTestRun', 'startTest', 'setUp', 'test', 'addFailure', 36475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'tearDown', 'stopTest', 'stopTestRun'] 36575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events = [] 36675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Foo(events).run() 36775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(events, expected) 36875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 36975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "When a setUp() method is defined, the test runner will run that method 37075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # prior to each test. Likewise, if a tearDown() method is defined, the 37175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # test runner will invoke that method after each test. In the example, 37275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # setUp() was used to create a fresh sequence for each test." 37375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # 37475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Make sure the proper call order is maintained, even if tearDown() raises 37575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # an exception. 37675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_run_call_order__error_in_tearDown(self): 37775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events = [] 37875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = LoggingResult(events) 37975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 38075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(Test.LoggingTestCase): 38175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDown(self): 38275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(Foo, self).tearDown() 38375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise RuntimeError('raised by Foo.tearDown') 38475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 38575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Foo(events).run(result) 38675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError', 38775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'stopTest'] 38875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(events, expected) 38975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 39075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "When tearDown errors with a default result stopTestRun is still called." 39175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_run_call_order__error_in_tearDown_default_result(self): 39275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 39375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(Test.LoggingTestCase): 39475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def defaultTestResult(self): 39575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return LoggingResult(self.events) 39675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDown(self): 39775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen super(Foo, self).tearDown() 39875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise RuntimeError('raised by Foo.tearDown') 39975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 40075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events = [] 40175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Foo(events).run() 40275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen expected = ['startTestRun', 'startTest', 'setUp', 'test', 'tearDown', 40375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'addError', 'stopTest', 'stopTestRun'] 40475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(events, expected) 40575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 40675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "TestCase.run() still works when the defaultTestResult is a TestResult 40775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # that does not support startTestRun and stopTestRun. 40875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_run_call_order_default_result(self): 40975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 41075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(unittest2.TestCase): 41175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def defaultTestResult(self): 41275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return OldTestResult() 41375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(self): 41475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 41575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 41675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Foo('test').run() 41775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 41875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "This class attribute gives the exception raised by the test() method. 41975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # If a test framework needs to use a specialized exception, possibly to 42075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # carry additional information, it must subclass this exception in 42175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # order to ``play fair'' with the framework. The initial value of this 42275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # attribute is AssertionError" 42375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_failureException__default(self): 42475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(unittest2.TestCase): 42575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(self): 42675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 42775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 42875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTrue(Foo('test').failureException is AssertionError) 42975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 43075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "This class attribute gives the exception raised by the test() method. 43175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # If a test framework needs to use a specialized exception, possibly to 43275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # carry additional information, it must subclass this exception in 43375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # order to ``play fair'' with the framework." 43475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # 43575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Make sure TestCase.run() respects the designated failureException 43675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_failureException__subclassing__explicit_raise(self): 43775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events = [] 43875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = LoggingResult(events) 43975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 44075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(unittest2.TestCase): 44175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(self): 44275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise RuntimeError() 44375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 44475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen failureException = RuntimeError 44575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 44675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTrue(Foo('test').failureException is RuntimeError) 44775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 44875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 44975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Foo('test').run(result) 45075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen expected = ['startTest', 'addFailure', 'stopTest'] 45175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(events, expected) 45275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 45375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "This class attribute gives the exception raised by the test() method. 45475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # If a test framework needs to use a specialized exception, possibly to 45575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # carry additional information, it must subclass this exception in 45675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # order to ``play fair'' with the framework." 45775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # 45875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Make sure TestCase.run() respects the designated failureException 45975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_failureException__subclassing__implicit_raise(self): 46075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events = [] 46175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = LoggingResult(events) 46275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 46375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(unittest2.TestCase): 46475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(self): 46575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail("foo") 46675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 46775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen failureException = RuntimeError 46875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 46975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTrue(Foo('test').failureException is RuntimeError) 47075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 47175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 47275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Foo('test').run(result) 47375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen expected = ['startTest', 'addFailure', 'stopTest'] 47475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(events, expected) 47575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 47675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "The default implementation does nothing." 47775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_setUp(self): 47875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(unittest2.TestCase): 47975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def runTest(self): 48075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 48175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 48275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # ... and nothing should happen 48375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Foo().setUp() 48475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 48575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "The default implementation does nothing." 48675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_tearDown(self): 48775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(unittest2.TestCase): 48875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def runTest(self): 48975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 49075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 49175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # ... and nothing should happen 49275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Foo().tearDown() 49375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 49475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "Return a string identifying the specific test case." 49575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # 49675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Because of the vague nature of the docs, I'm not going to lock this 49775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # test down too much. Really all that can be asserted is that the id() 49875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # will be a string (either 8-byte or unicode -- again, because the docs 49975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # just say "string") 50075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_id(self): 50175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(unittest2.TestCase): 50275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def runTest(self): 50375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 50475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 50575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIsInstance(Foo().id(), basestring) 50675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 50775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "If result is omitted or None, a temporary result object is created 50875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # and used, but is not made available to the caller. As TestCase owns the 50975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # temporary result startTestRun and stopTestRun are called. 51075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 51175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_run__uses_defaultTestResult(self): 51275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events = [] 51375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 51475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Foo(unittest2.TestCase): 51575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(self): 51675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events.append('test') 51775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 51875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def defaultTestResult(self): 51975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return LoggingResult(events) 52075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 52175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Make run() find a result object on its own 52275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Foo('test').run() 52375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 52475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen expected = ['startTestRun', 'startTest', 'test', 'addSuccess', 52575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'stopTest', 'stopTestRun'] 52675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(events, expected) 52775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 52875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testShortDescriptionWithoutDocstring(self): 52975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIsNone(self.shortDescription()) 53075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 53175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testShortDescriptionWithOneLineDocstring(self): 53275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen """Tests shortDescription() for a method with a docstring.""" 53375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual( 53475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.shortDescription(), 53575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'Tests shortDescription() for a method with a docstring.') 53675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 53775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testShortDescriptionWithMultiLineDocstring(self): 53875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen """Tests shortDescription() for a method with a longer docstring. 53975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 54075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen This method ensures that only the first line of a docstring is 54175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen returned used in the short description, no matter how long the 54275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen whole thing is. 54375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen """ 54475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual( 54575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.shortDescription(), 54675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'Tests shortDescription() for a method with a longer ' 54775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'docstring.') 54875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 54975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAddTypeEqualityFunc(self): 55075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class SadSnake(object): 55175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen """Dummy class for test_addTypeEqualityFunc.""" 55275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen s1, s2 = SadSnake(), SadSnake() 55375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertNotEqual(s1, s2) 55475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def AllSnakesCreatedEqual(a, b, msg=None): 55575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return type(a) is type(b) is SadSnake 55675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.addTypeEqualityFunc(SadSnake, AllSnakesCreatedEqual) 55775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(s1, s2) 55875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # No this doesn't clean up and remove the SadSnake equality func 55975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # from this TestCase instance but since its a local nothing else 56075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # will ever notice that. 56175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 56275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertIs(self): 56375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen thing = object() 56475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIs(thing, thing) 56575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertIs, thing, object()) 56675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 56775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertIsNot(self): 56875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen thing = object() 56975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIsNot(thing, object()) 57075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertIsNot, thing, thing) 57175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 57275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertIsInstance(self): 57375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen thing = [] 57475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIsInstance(thing, list) 57575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertIsInstance, 57675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen thing, dict) 57775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 57875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertNotIsInstance(self): 57975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen thing = [] 58075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertNotIsInstance(thing, dict) 58175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertNotIsInstance, 58275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen thing, list) 58375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 58475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertIn(self): 58575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen animals = {'monkey': 'banana', 'cow': 'grass', 'seal': 'fish'} 58675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 58775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIn('a', 'abc') 58875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIn(2, [1, 2, 3]) 58975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIn('monkey', animals) 59075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 59175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertNotIn('d', 'abc') 59275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertNotIn(0, [1, 2, 3]) 59375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertNotIn('otter', animals) 59475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 59575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertIn, 'x', 'abc') 59675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertIn, 4, [1, 2, 3]) 59775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertIn, 'elephant', 59875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen animals) 59975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 60075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertNotIn, 'c', 'abc') 60175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertNotIn, 1, [1, 2, 3]) 60275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertNotIn, 'cow', 60375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen animals) 60475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 60575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertDictContainsSubset(self): 60675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertDictContainsSubset({}, {}) 60775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertDictContainsSubset({}, {'a': 1}) 60875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertDictContainsSubset({'a': 1}, {'a': 1}) 60975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertDictContainsSubset({'a': 1}, {'a': 1, 'b': 2}) 61075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertDictContainsSubset({'a': 1, 'b': 2}, {'a': 1, 'b': 2}) 61175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 61275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(unittest2.TestCase.failureException, 61375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertDictContainsSubset, {'a': 2}, {'a': 1}, 61475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen '.*Mismatched values:.*') 61575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 61675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(unittest2.TestCase.failureException, 61775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertDictContainsSubset, {'c': 1}, {'a': 1}, 61875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen '.*Missing:.*') 61975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 62075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(unittest2.TestCase.failureException, 62175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertDictContainsSubset, {'a': 1, 'c': 1}, 62275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen {'a': 1}, '.*Missing:.*') 62375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 62475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(unittest2.TestCase.failureException, 62575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertDictContainsSubset, {'a': 1, 'c': 1}, 62675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen {'a': 1}, '.*Missing:.*Mismatched values:.*') 62775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 62875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, 62975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertDictContainsSubset, {1: "one"}, {}) 63075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 63175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertEqual(self): 63275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen equal_pairs = [ 63375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ((), ()), 63475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ({}, {}), 63575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ([], []), 63675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen (set(), set()), 63775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen (frozenset(), frozenset())] 63875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen for a, b in equal_pairs: 63975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # This mess of try excepts is to test the assertEqual behavior 64075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # itself. 64175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen try: 64275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(a, b) 64375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen except self.failureException: 64475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail('assertEqual(%r, %r) failed' % (a, b)) 64575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen try: 64675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(a, b, msg='foo') 64775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen except self.failureException: 64875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail('assertEqual(%r, %r) with msg= failed' % (a, b)) 64975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen try: 65075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(a, b, 'foo') 65175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen except self.failureException: 65275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail('assertEqual(%r, %r) with third parameter failed' % 65375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen (a, b)) 65475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 65575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unequal_pairs = [ 65675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ((), []), 65775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ({}, set()), 65875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen (set([4,1]), frozenset([4,2])), 65975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen (frozenset([4,5]), set([2,3])), 66075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen (set([3,4]), set([5,4]))] 66175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen for a, b in unequal_pairs: 66275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertEqual, a, b) 66375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertEqual, a, b, 66475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'foo') 66575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertEqual, a, b, 66675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen msg='foo') 66775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 66875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testEquality(self): 66975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertListEqual([], []) 67075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTupleEqual((), ()) 67175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertSequenceEqual([], ()) 67275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 67375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen a = [0, 'a', []] 67475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen b = [] 67575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(unittest2.TestCase.failureException, 67675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertListEqual, a, b) 67775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(unittest2.TestCase.failureException, 67875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertListEqual, tuple(a), tuple(b)) 67975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(unittest2.TestCase.failureException, 68075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertSequenceEqual, a, tuple(b)) 68175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 68275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen b.extend(a) 68375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertListEqual(a, b) 68475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTupleEqual(tuple(a), tuple(b)) 68575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertSequenceEqual(a, tuple(b)) 68675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertSequenceEqual(tuple(a), b) 68775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 68875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertListEqual, 68975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen a, tuple(b)) 69075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertTupleEqual, 69175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen tuple(a), b) 69275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertListEqual, None, b) 69375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertTupleEqual, None, 69475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen tuple(b)) 69575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertSequenceEqual, 69675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen None, tuple(b)) 69775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertListEqual, 1, 1) 69875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertTupleEqual, 1, 1) 69975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertSequenceEqual, 70075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 1, 1) 70175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 70275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertDictEqual({}, {}) 70375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 70475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen c = { 'x': 1 } 70575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen d = {} 70675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(unittest2.TestCase.failureException, 70775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertDictEqual, c, d) 70875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 70975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen d.update(c) 71075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertDictEqual(c, d) 71175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 71275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen d['x'] = 0 71375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(unittest2.TestCase.failureException, 71475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertDictEqual, c, d, 'These are unequal') 71575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 71675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertDictEqual, None, d) 71775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertDictEqual, [], d) 71875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertDictEqual, 1, 1) 71975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 72075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertItemsEqual(self): 72175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertItemsEqual([1, 2, 3], [3, 2, 1]) 72275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertItemsEqual(['foo', 'bar', 'baz'], ['bar', 'baz', 'foo']) 72375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertItemsEqual, 72475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen [10], [10, 11]) 72575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertItemsEqual, 72675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen [10, 11], [10]) 72775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertItemsEqual, 72875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen [10, 11, 10], [10, 11]) 72975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 73075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Test that sequences of unhashable objects can be tested for sameness: 73175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertItemsEqual([[1, 2], [3, 4]], [[3, 4], [1, 2]]) 73275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 73375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertItemsEqual([{'a': 1}, {'b': 2}], [{'b': 2}, {'a': 1}]) 73475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertItemsEqual, 73575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen [[1]], [[2]]) 73675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 73775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Test unsortable objects 73875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertItemsEqual([2j, None], [None, 2j]) 73975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertItemsEqual, 74075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen [2j, None], [None, 3j]) 74175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 74275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertSetEqual(self): 74375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen set1 = set() 74475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen set2 = set() 74575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertSetEqual(set1, set2) 74675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 74775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertSetEqual, None, set2) 74875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertSetEqual, [], set2) 74975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertSetEqual, set1, None) 75075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertSetEqual, set1, []) 75175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 75275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen set1 = set(['a']) 75375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen set2 = set() 75475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) 75575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 75675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen set1 = set(['a']) 75775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen set2 = set(['a']) 75875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertSetEqual(set1, set2) 75975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 76075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen set1 = set(['a']) 76175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen set2 = set(['a', 'b']) 76275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) 76375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 76475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen set1 = set(['a']) 76575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen set2 = frozenset(['a', 'b']) 76675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) 76775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 76875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen set1 = set(['a', 'b']) 76975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen set2 = frozenset(['a', 'b']) 77075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertSetEqual(set1, set2) 77175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 77275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen set1 = set() 77375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen set2 = "foo" 77475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) 77575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertSetEqual, set2, set1) 77675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 77775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # make sure any string formatting is tuple-safe 77875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen set1 = set([(0, 1), (2, 3)]) 77975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen set2 = set([(4, 5)]) 78075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertSetEqual, set1, set2) 78175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 78275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testInequality(self): 78375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Try ints 78475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreater(2, 1) 78575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreaterEqual(2, 1) 78675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreaterEqual(1, 1) 78775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLess(1, 2) 78875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLessEqual(1, 2) 78975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLessEqual(1, 1) 79075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreater, 1, 2) 79175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreater, 1, 1) 79275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreaterEqual, 1, 2) 79375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLess, 2, 1) 79475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLess, 1, 1) 79575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLessEqual, 2, 1) 79675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 79775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Try Floats 79875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreater(1.1, 1.0) 79975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreaterEqual(1.1, 1.0) 80075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreaterEqual(1.0, 1.0) 80175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLess(1.0, 1.1) 80275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLessEqual(1.0, 1.1) 80375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLessEqual(1.0, 1.0) 80475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.1) 80575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreater, 1.0, 1.0) 80675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreaterEqual, 1.0, 1.1) 80775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLess, 1.1, 1.0) 80875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLess, 1.0, 1.0) 80975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLessEqual, 1.1, 1.0) 81075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 81175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Try Strings 81275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreater('bug', 'ant') 81375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreaterEqual('bug', 'ant') 81475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreaterEqual('ant', 'ant') 81575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLess('ant', 'bug') 81675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLessEqual('ant', 'bug') 81775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLessEqual('ant', 'ant') 81875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreater, 'ant', 'bug') 81975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreater, 'ant', 'ant') 82075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant', 'bug') 82175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLess, 'bug', 'ant') 82275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLess, 'ant', 'ant') 82375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLessEqual, 'bug', 'ant') 82475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 82575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Try Unicode 82675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreater(u'bug', u'ant') 82775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreaterEqual(u'bug', u'ant') 82875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreaterEqual(u'ant', u'ant') 82975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLess(u'ant', u'bug') 83075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLessEqual(u'ant', u'bug') 83175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLessEqual(u'ant', u'ant') 83275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreater, u'ant', u'bug') 83375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreater, u'ant', u'ant') 83475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreaterEqual, u'ant', 83575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen u'bug') 83675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLess, u'bug', u'ant') 83775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLess, u'ant', u'ant') 83875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLessEqual, u'bug', u'ant') 83975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 84075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Try Mixed String/Unicode 84175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreater('bug', u'ant') 84275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreater(u'bug', 'ant') 84375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreaterEqual('bug', u'ant') 84475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreaterEqual(u'bug', 'ant') 84575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreaterEqual('ant', u'ant') 84675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertGreaterEqual(u'ant', 'ant') 84775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLess('ant', u'bug') 84875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLess(u'ant', 'bug') 84975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLessEqual('ant', u'bug') 85075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLessEqual(u'ant', 'bug') 85175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLessEqual('ant', u'ant') 85275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertLessEqual(u'ant', 'ant') 85375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreater, 'ant', u'bug') 85475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreater, u'ant', 'bug') 85575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreater, 'ant', u'ant') 85675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreater, u'ant', 'ant') 85775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreaterEqual, 'ant', 85875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen u'bug') 85975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertGreaterEqual, u'ant', 86075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'bug') 86175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLess, 'bug', u'ant') 86275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLess, u'bug', 'ant') 86375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLess, 'ant', u'ant') 86475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLess, u'ant', 'ant') 86575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLessEqual, 'bug', u'ant') 86675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertLessEqual, u'bug', 'ant') 86775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 86875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertMultiLineEqual(self): 86975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen sample_text = """\ 87075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenhttp://www.python.org/doc/2.3/lib/module-unittest.html 87175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chentest case 87275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen A test case is the smallest unit of testing. [...] 87375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen""" 87475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen revised_sample_text = """\ 87575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenhttp://www.python.org/doc/2.4.1/lib/module-unittest.html 87675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chentest case 87775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen A test case is the smallest unit of testing. [...] You may provide your 87875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen own implementation that does not subclass from TestCase, of course. 87975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen""" 88075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen sample_text_error = """\ 88175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen- http://www.python.org/doc/2.3/lib/module-unittest.html 88275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen? ^ 88375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen+ http://www.python.org/doc/2.4.1/lib/module-unittest.html 88475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen? ^^^ 88575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test case 88675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen- A test case is the smallest unit of testing. [...] 88775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen+ A test case is the smallest unit of testing. [...] You may provide your 88875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen? +++++++++++++++++++++ 88975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen+ own implementation that does not subclass from TestCase, of course. 89075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen""" 89175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.maxDiff = None 89275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen for type_changer in (lambda x: x, lambda x: x.decode('utf8')): 89375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen try: 89475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertMultiLineEqual(type_changer(sample_text), 89575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen type_changer(revised_sample_text)) 89675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen except self.failureException, e: 89775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # need to remove the first line of the error message 89875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen error = str(e).encode('utf8').split('\n', 1)[1] 89975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 90075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # assertMultiLineEqual is hooked up as the default for 90175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # unicode strings - so we can't use it for this check 90275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTrue(sample_text_error == error) 90375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 90475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertSequenceEqualMaxDiff(self): 90575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(self.maxDiff, 80*8) 90675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen seq1 = 'a' + 'x' * 80**2 90775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen seq2 = 'b' + 'x' * 80**2 90875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen diff = '\n'.join(difflib.ndiff(pprint.pformat(seq1).splitlines(), 90975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pprint.pformat(seq2).splitlines())) 91075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # the +1 is the leading \n added by assertSequenceEqual 91175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen omitted = unittest2.case.DIFF_OMITTED % (len(diff) + 1,) 91275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 91375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.maxDiff = len(diff)//2 91475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen try: 91575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertSequenceEqual(seq1, seq2) 91675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen except self.failureException, e: 91775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen msg = e.args[0] 91875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen else: 91975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail('assertSequenceEqual did not fail.') 92075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTrue(len(msg) < len(diff)) 92175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIn(omitted, msg) 92275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 92375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.maxDiff = len(diff) * 2 92475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen try: 92575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertSequenceEqual(seq1, seq2) 92675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen except self.failureException, e: 92775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen msg = e.args[0] 92875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen else: 92975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail('assertSequenceEqual did not fail.') 93075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTrue(len(msg) > len(diff)) 93175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertNotIn(omitted, msg) 93275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 93375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.maxDiff = None 93475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen try: 93575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertSequenceEqual(seq1, seq2) 93675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen except self.failureException, e: 93775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen msg = e.args[0] 93875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen else: 93975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail('assertSequenceEqual did not fail.') 94075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTrue(len(msg) > len(diff)) 94175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertNotIn(omitted, msg) 94275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 94375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testTruncateMessage(self): 94475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.maxDiff = 1 94575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen message = self._truncateMessage('foo', 'bar') 94675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen omitted = unittest2.case.DIFF_OMITTED % len('bar') 94775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(message, 'foo' + omitted) 94875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 94975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.maxDiff = None 95075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen message = self._truncateMessage('foo', 'bar') 95175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(message, 'foobar') 95275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 95375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.maxDiff = 4 95475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen message = self._truncateMessage('foo', 'bar') 95575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(message, 'foobar') 95675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 95775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertDictEqualTruncates(self): 95875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test = unittest2.TestCase('assertEqual') 95975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def truncate(msg, diff): 96075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return 'foo' 96175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test._truncateMessage = truncate 96275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen try: 96375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test.assertDictEqual({}, {1: 0}) 96475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen except self.failureException, e: 96575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(str(e), 'foo') 96675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen else: 96775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail('assertDictEqual did not fail') 96875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 96975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertMultiLineEqualTruncates(self): 97075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test = unittest2.TestCase('assertEqual') 97175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def truncate(msg, diff): 97275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return 'foo' 97375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test._truncateMessage = truncate 97475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen try: 97575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test.assertMultiLineEqual('foo', 'bar') 97675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen except self.failureException, e: 97775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(str(e), 'foo') 97875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen else: 97975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail('assertMultiLineEqual did not fail') 98075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 98175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertIsNone(self): 98275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIsNone(None) 98375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertIsNone, False) 98475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIsNotNone('DjZoPloGears on Rails') 98575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertIsNotNone, None) 98675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 98775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertRegexpMatches(self): 98875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRegexpMatches('asdfabasdf', r'ab+') 98975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaises(self.failureException, self.assertRegexpMatches, 99075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'saaas', r'aaaa') 99175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 99275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertRaisesRegexp(self): 99375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class ExceptionMock(Exception): 99475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 99575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 99675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def Stub(): 99775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise ExceptionMock('We expect') 99875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 99975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaisesRegexp(ExceptionMock, re.compile('expect$'), Stub) 100075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaisesRegexp(ExceptionMock, 'expect$', Stub) 100175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaisesRegexp(ExceptionMock, u'expect$', Stub) 100275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 100375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertNotRaisesRegexp(self): 100475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaisesRegexp( 100575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.failureException, '^Exception not raised$', 100675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaisesRegexp, Exception, re.compile('x'), 100775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen lambda: None) 100875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaisesRegexp( 100975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.failureException, '^Exception not raised$', 101075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaisesRegexp, Exception, 'x', 101175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen lambda: None) 101275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaisesRegexp( 101375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.failureException, '^Exception not raised$', 101475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaisesRegexp, Exception, u'x', 101575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen lambda: None) 101675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 101775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testAssertRaisesRegexpMismatch(self): 101875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def Stub(): 101975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise Exception('Unexpected') 102075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 102175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaisesRegexp( 102275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.failureException, 102375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen r'"\^Expected\$" does not match "Unexpected"', 102475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaisesRegexp, Exception, '^Expected$', 102575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Stub) 102675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaisesRegexp( 102775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.failureException, 102875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen r'"\^Expected\$" does not match "Unexpected"', 102975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaisesRegexp, Exception, u'^Expected$', 103075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Stub) 103175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaisesRegexp( 103275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.failureException, 103375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen r'"\^Expected\$" does not match "Unexpected"', 103475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaisesRegexp, Exception, 103575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen re.compile('^Expected$'), Stub) 103675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 103775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 103875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testSynonymAssertMethodNames(self): 103975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen """Test undocumented method name synonyms. 104075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 104175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Please do not use these methods names in your own code. 104275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 104375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen This test confirms their continued existence and functionality 104475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen in order to avoid breaking existing code. 104575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen """ 104675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertNotEquals(3, 5) 104775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEquals(3, 3) 104875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertAlmostEquals(2.0, 2.0) 104975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertNotAlmostEquals(3.0, 5.0) 105075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assert_(True) 105175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 105275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testDeepcopy(self): 105375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Issue: 5660 105475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class TestableTest(unittest2.TestCase): 105575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testNothing(self): 105675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 105775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 105875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test = TestableTest('testNothing') 105975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 106075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # This shouldn't blow up 106175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen deepcopy(test) 106275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 106375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 106475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenif __name__ == "__main__": 106575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unittest2.main() 1066