test_setups.py revision 75e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74
175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenimport sys 275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenfrom cStringIO import StringIO 475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenimport unittest2 675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenfrom unittest2.test.support import resultFactory 775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenclass TestSetups(unittest2.TestCase): 1075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 1175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def getRunner(self): 1275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return unittest2.TextTestRunner(resultclass=resultFactory, 1375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen stream=StringIO()) 1475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def runTests(self, *cases): 1575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen suite = unittest2.TestSuite() 1675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen for case in cases: 1775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen tests = unittest2.defaultTestLoader.loadTestsFromTestCase(case) 1875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen suite.addTests(tests) 1975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 2075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen runner = self.getRunner() 2175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 2275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # creating a nested suite exposes some potential bugs 2375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen realSuite = unittest2.TestSuite() 2475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen realSuite.addTest(suite) 2575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # adding empty suites to the end exposes potential bugs 2675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen suite.addTest(unittest2.TestSuite()) 2775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen realSuite.addTest(unittest2.TestSuite()) 2875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen return runner.run(realSuite) 2975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 3075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_setup_class(self): 3175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 3275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen setUpCalled = 0 3375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 3475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpClass(cls): 3575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.setUpCalled += 1 3675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unittest2.TestCase.setUpClass() 3775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 3875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 3975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_two(self): 4075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 4175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 4275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = self.runTests(Test) 4375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 4475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(Test.setUpCalled, 1) 4575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(result.testsRun, 2) 4675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(len(result.errors), 0) 4775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 4875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_teardown_class(self): 4975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 5075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen tearDownCalled = 0 5175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 5275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownClass(cls): 5375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.tearDownCalled += 1 5475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unittest2.TestCase.tearDownClass() 5575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 5675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 5775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_two(self): 5875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 5975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 6075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = self.runTests(Test) 6175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 6275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(Test.tearDownCalled, 1) 6375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(result.testsRun, 2) 6475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(len(result.errors), 0) 6575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 6675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_teardown_class_two_classes(self): 6775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 6875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen tearDownCalled = 0 6975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 7075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownClass(cls): 7175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.tearDownCalled += 1 7275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unittest2.TestCase.tearDownClass() 7375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 7475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 7575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_two(self): 7675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 7775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 7875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test2(unittest2.TestCase): 7975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen tearDownCalled = 0 8075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 8175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownClass(cls): 8275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test2.tearDownCalled += 1 8375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unittest2.TestCase.tearDownClass() 8475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 8575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 8675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_two(self): 8775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 8875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 8975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = self.runTests(Test, Test2) 9075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 9175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(Test.tearDownCalled, 1) 9275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(Test2.tearDownCalled, 1) 9375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(result.testsRun, 4) 9475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(len(result.errors), 0) 9575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 9675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_error_in_setupclass(self): 9775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class BrokenTest(unittest2.TestCase): 9875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 9975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpClass(cls): 10075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise TypeError('foo') 10175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 10275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 10375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_two(self): 10475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 10575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 10675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = self.runTests(BrokenTest) 10775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 10875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(result.testsRun, 0) 10975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(len(result.errors), 1) 11075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen error, _ = result.errors[0] 11175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(str(error), 11275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'setUpClass (%s.BrokenTest)' % __name__) 11375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 11475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_error_in_teardown_class(self): 11575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 11675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen tornDown = 0 11775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 11875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownClass(cls): 11975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.tornDown += 1 12075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise TypeError('foo') 12175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 12275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 12375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_two(self): 12475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 12575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 12675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test2(unittest2.TestCase): 12775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen tornDown = 0 12875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 12975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownClass(cls): 13075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test2.tornDown += 1 13175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise TypeError('foo') 13275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 13375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 13475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_two(self): 13575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 13675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 13775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = self.runTests(Test, Test2) 13875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(result.testsRun, 4) 13975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(len(result.errors), 2) 14075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(Test.tornDown, 1) 14175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(Test2.tornDown, 1) 14275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 14375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen error, _ = result.errors[0] 14475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(str(error), 14575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'tearDownClass (%s.Test)' % __name__) 14675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 14775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_class_not_torndown_when_setup_fails(self): 14875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 14975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen tornDown = False 15075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 15175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpClass(cls): 15275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise TypeError 15375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 15475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownClass(cls): 15575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.tornDown = True 15675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise TypeError('foo') 15775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 15875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 15975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 16075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.runTests(Test) 16175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertFalse(Test.tornDown) 16275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 16375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_class_not_setup_or_torndown_when_skipped(self): 16475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 16575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen classSetUp = False 16675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen tornDown = False 16775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 16875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpClass(cls): 16975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.classSetUp = True 17075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 17175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownClass(cls): 17275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.tornDown = True 17375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 17475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 17575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 17675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test = unittest2.skip("hop")(Test) 17775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.runTests(Test) 17875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertFalse(Test.classSetUp) 17975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertFalse(Test.tornDown) 18075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 18175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_setup_teardown_order_with_pathological_suite(self): 18275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results = [] 18375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 18475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Module1(object): 18575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @staticmethod 18675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpModule(): 18775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results.append('Module1.setUpModule') 18875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @staticmethod 18975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownModule(): 19075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results.append('Module1.tearDownModule') 19175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 19275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Module2(object): 19375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @staticmethod 19475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpModule(): 19575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results.append('Module2.setUpModule') 19675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @staticmethod 19775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownModule(): 19875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results.append('Module2.tearDownModule') 19975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 20075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test1(unittest2.TestCase): 20175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 20275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpClass(cls): 20375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results.append('setup 1') 20475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 20575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownClass(cls): 20675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results.append('teardown 1') 20775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testOne(self): 20875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results.append('Test1.testOne') 20975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testTwo(self): 21075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results.append('Test1.testTwo') 21175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 21275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test2(unittest2.TestCase): 21375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 21475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpClass(cls): 21575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results.append('setup 2') 21675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 21775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownClass(cls): 21875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results.append('teardown 2') 21975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testOne(self): 22075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results.append('Test2.testOne') 22175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testTwo(self): 22275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results.append('Test2.testTwo') 22375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 22475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test3(unittest2.TestCase): 22575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 22675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpClass(cls): 22775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results.append('setup 3') 22875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 22975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownClass(cls): 23075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results.append('teardown 3') 23175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testOne(self): 23275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results.append('Test3.testOne') 23375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def testTwo(self): 23475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen results.append('Test3.testTwo') 23575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 23675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test1.__module__ = Test2.__module__ = 'Module' 23775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test3.__module__ = 'Module2' 23875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen sys.modules['Module'] = Module1 23975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen sys.modules['Module2'] = Module2 24075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 24175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen first = unittest2.TestSuite((Test1('testOne'),)) 24275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen second = unittest2.TestSuite((Test1('testTwo'),)) 24375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen third = unittest2.TestSuite((Test2('testOne'),)) 24475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen fourth = unittest2.TestSuite((Test2('testTwo'),)) 24575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen fifth = unittest2.TestSuite((Test3('testOne'),)) 24675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen sixth = unittest2.TestSuite((Test3('testTwo'),)) 24775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen suite = unittest2.TestSuite((first, second, third, fourth, fifth, sixth)) 24875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 24975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen runner = self.getRunner() 25075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = runner.run(suite) 25175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(result.testsRun, 6) 25275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(len(result.errors), 0) 25375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 25475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(results, 25575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ['Module1.setUpModule', 'setup 1', 25675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'Test1.testOne', 'Test1.testTwo', 'teardown 1', 25775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'setup 2', 'Test2.testOne', 'Test2.testTwo', 25875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'teardown 2', 'Module1.tearDownModule', 25975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'Module2.setUpModule', 'setup 3', 26075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'Test3.testOne', 'Test3.testTwo', 26175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'teardown 3', 'Module2.tearDownModule']) 26275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 26375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_setup_module(self): 26475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Module(object): 26575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen moduleSetup = 0 26675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @staticmethod 26775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpModule(): 26875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Module.moduleSetup += 1 26975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 27075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 27175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 27275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 27375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_two(self): 27475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 27575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.__module__ = 'Module' 27675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen sys.modules['Module'] = Module 27775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 27875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = self.runTests(Test) 27975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(Module.moduleSetup, 1) 28075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(result.testsRun, 2) 28175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(len(result.errors), 0) 28275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 28375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_error_in_setup_module(self): 28475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Module(object): 28575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen moduleSetup = 0 28675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen moduleTornDown = 0 28775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @staticmethod 28875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpModule(): 28975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Module.moduleSetup += 1 29075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise TypeError('foo') 29175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @staticmethod 29275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownModule(): 29375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Module.moduleTornDown += 1 29475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 29575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 29675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen classSetUp = False 29775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen classTornDown = False 29875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 29975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpClass(cls): 30075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.classSetUp = True 30175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 30275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownClass(cls): 30375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.classTornDown = True 30475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 30575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 30675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_two(self): 30775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 30875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 30975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test2(unittest2.TestCase): 31075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 31175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 31275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_two(self): 31375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 31475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.__module__ = 'Module' 31575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test2.__module__ = 'Module' 31675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen sys.modules['Module'] = Module 31775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 31875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = self.runTests(Test, Test2) 31975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(Module.moduleSetup, 1) 32075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(Module.moduleTornDown, 0) 32175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(result.testsRun, 0) 32275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertFalse(Test.classSetUp) 32375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertFalse(Test.classTornDown) 32475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(len(result.errors), 1) 32575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen error, _ = result.errors[0] 32675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(str(error), 'setUpModule (Module)') 32775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 32875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_testcase_with_missing_module(self): 32975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 33075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 33175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 33275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_two(self): 33375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 33475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.__module__ = 'Module' 33575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen sys.modules.pop('Module', None) 33675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 33775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = self.runTests(Test) 33875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(result.testsRun, 2) 33975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 34075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_teardown_module(self): 34175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Module(object): 34275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen moduleTornDown = 0 34375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @staticmethod 34475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownModule(): 34575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Module.moduleTornDown += 1 34675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 34775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 34875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 34975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 35075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_two(self): 35175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 35275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.__module__ = 'Module' 35375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen sys.modules['Module'] = Module 35475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 35575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = self.runTests(Test) 35675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(Module.moduleTornDown, 1) 35775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(result.testsRun, 2) 35875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(len(result.errors), 0) 35975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 36075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_error_in_teardown_module(self): 36175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Module(object): 36275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen moduleTornDown = 0 36375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @staticmethod 36475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownModule(): 36575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Module.moduleTornDown += 1 36675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise TypeError('foo') 36775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 36875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 36975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen classSetUp = False 37075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen classTornDown = False 37175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 37275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpClass(cls): 37375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.classSetUp = True 37475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 37575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownClass(cls): 37675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.classTornDown = True 37775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 37875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 37975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_two(self): 38075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 38175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 38275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test2(unittest2.TestCase): 38375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 38475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 38575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_two(self): 38675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 38775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.__module__ = 'Module' 38875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test2.__module__ = 'Module' 38975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen sys.modules['Module'] = Module 39075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 39175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = self.runTests(Test, Test2) 39275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(Module.moduleTornDown, 1) 39375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(result.testsRun, 4) 39475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTrue(Test.classSetUp) 39575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertTrue(Test.classTornDown) 39675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(len(result.errors), 1) 39775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen error, _ = result.errors[0] 39875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(str(error), 'tearDownModule (Module)') 39975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 40075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_skiptest_in_setupclass(self): 40175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 40275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 40375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpClass(cls): 40475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise unittest2.SkipTest('foo') 40575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 40675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 40775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_two(self): 40875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 40975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 41075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = self.runTests(Test) 41175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(result.testsRun, 0) 41275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(len(result.errors), 0) 41375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(len(result.skipped), 1) 41475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen skipped = result.skipped[0][0] 41575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(str(skipped), 'setUpClass (%s.Test)' % __name__) 41675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 41775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_skiptest_in_setupmodule(self): 41875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 41975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_one(self): 42075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 42175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_two(self): 42275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen pass 42375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 42475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Module(object): 42575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @staticmethod 42675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpModule(): 42775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise unittest2.SkipTest('foo') 42875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 42975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.__module__ = 'Module' 43075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen sys.modules['Module'] = Module 43175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 43275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = self.runTests(Test) 43375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(result.testsRun, 0) 43475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(len(result.errors), 0) 43575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(len(result.skipped), 1) 43675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen skipped = result.skipped[0][0] 43775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(str(skipped), 'setUpModule (Module)') 43875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 43975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_suite_debug_executes_setups_and_teardowns(self): 44075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering = [] 44175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 44275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Module(object): 44375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @staticmethod 44475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpModule(): 44575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering.append('setUpModule') 44675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @staticmethod 44775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownModule(): 44875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering.append('tearDownModule') 44975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 45075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 45175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 45275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpClass(cls): 45375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering.append('setUpClass') 45475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 45575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownClass(cls): 45675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering.append('tearDownClass') 45775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_something(self): 45875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen ordering.append('test_something') 45975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 46075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.__module__ = 'Module' 46175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen sys.modules['Module'] = Module 46275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 46375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen suite = unittest2.defaultTestLoader.loadTestsFromTestCase(Test) 46475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen suite.debug() 46575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen expectedOrder = ['setUpModule', 'setUpClass', 'test_something', 'tearDownClass', 'tearDownModule'] 46675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(ordering, expectedOrder) 46775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 46875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_suite_debug_propagates_exceptions(self): 46975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Module(object): 47075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @staticmethod 47175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpModule(): 47275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen if phase == 0: 47375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise Exception('setUpModule') 47475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @staticmethod 47575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownModule(): 47675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen if phase == 1: 47775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise Exception('tearDownModule') 47875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 47975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen class Test(unittest2.TestCase): 48075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 48175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUpClass(cls): 48275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen if phase == 2: 48375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise Exception('setUpClass') 48475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen @classmethod 48575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDownClass(cls): 48675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen if phase == 3: 48775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise Exception('tearDownClass') 48875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_something(self): 48975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen if phase == 4: 49075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise Exception('test_something') 49175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 49275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen Test.__module__ = 'Module' 49375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen sys.modules['Module'] = Module 49475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 49575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen _suite = unittest2.defaultTestLoader.loadTestsFromTestCase(Test) 49675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen suite = unittest2.TestSuite() 49775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 49875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # nesting a suite again exposes a bug in the initial implementation 49975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen suite.addTest(_suite) 50075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen messages = ('setUpModule', 'tearDownModule', 'setUpClass', 'tearDownClass', 'test_something') 50175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen for phase, msg in enumerate(messages): 50275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertRaisesRegexp(Exception, msg, suite.debug) 503