175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenimport unittest2 275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenfrom unittest2.test.support import LoggingResult 475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenclass Test_FunctionTestCase(unittest2.TestCase): 775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "Return the number of tests represented by the this test object. For 975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # unittest2.TestCase instances, this will always be 1" 1075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_countTestCases(self): 1175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test = unittest2.FunctionTestCase(lambda: None) 1275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 1375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(test.countTestCases(), 1) 1475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 1575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "When a setUp() method is defined, the test runner will run that method 1675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # prior to each test. Likewise, if a tearDown() method is defined, the 1775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # test runner will invoke that method after each test. In the example, 1875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # setUp() was used to create a fresh sequence for each test." 1975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # 2075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Make sure the proper call order is maintained, even if setUp() raises 2175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # an exception. 2275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_run_call_order__error_in_setUp(self): 2375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events = [] 2475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = LoggingResult(events) 2575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 2675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUp(): 2775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events.append('setUp') 2875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise RuntimeError('raised by setUp') 2975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 3075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(): 3175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events.append('test') 3275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 3375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDown(): 3475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events.append('tearDown') 3575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 3675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen expected = ['startTest', 'setUp', 'addError', 'stopTest'] 3775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unittest2.FunctionTestCase(test, setUp, tearDown).run(result) 3875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(events, expected) 3975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 4075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "When a setUp() method is defined, the test runner will run that method 4175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # prior to each test. Likewise, if a tearDown() method is defined, the 4275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # test runner will invoke that method after each test. In the example, 4375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # setUp() was used to create a fresh sequence for each test." 4475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # 4575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Make sure the proper call order is maintained, even if the test raises 4675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # an error (as opposed to a failure). 4775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_run_call_order__error_in_test(self): 4875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events = [] 4975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = LoggingResult(events) 5075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 5175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUp(): 5275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events.append('setUp') 5375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 5475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(): 5575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events.append('test') 5675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise RuntimeError('raised by test') 5775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 5875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDown(): 5975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events.append('tearDown') 6075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 6175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown', 6275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'stopTest'] 6375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unittest2.FunctionTestCase(test, setUp, tearDown).run(result) 6475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(events, expected) 6575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 6675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "When a setUp() method is defined, the test runner will run that method 6775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # prior to each test. Likewise, if a tearDown() method is defined, the 6875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # test runner will invoke that method after each test. In the example, 6975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # setUp() was used to create a fresh sequence for each test." 7075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # 7175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Make sure the proper call order is maintained, even if the test signals 7275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # a failure (as opposed to an error). 7375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_run_call_order__failure_in_test(self): 7475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events = [] 7575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = LoggingResult(events) 7675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 7775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUp(): 7875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events.append('setUp') 7975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 8075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(): 8175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events.append('test') 8275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.fail('raised by test') 8375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 8475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDown(): 8575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events.append('tearDown') 8675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 8775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown', 8875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'stopTest'] 8975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unittest2.FunctionTestCase(test, setUp, tearDown).run(result) 9075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(events, expected) 9175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 9275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "When a setUp() method is defined, the test runner will run that method 9375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # prior to each test. Likewise, if a tearDown() method is defined, the 9475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # test runner will invoke that method after each test. In the example, 9575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # setUp() was used to create a fresh sequence for each test." 9675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # 9775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Make sure the proper call order is maintained, even if tearDown() raises 9875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # an exception. 9975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_run_call_order__error_in_tearDown(self): 10075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events = [] 10175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen result = LoggingResult(events) 10275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 10375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def setUp(): 10475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events.append('setUp') 10575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 10675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test(): 10775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events.append('test') 10875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 10975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def tearDown(): 11075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen events.append('tearDown') 11175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen raise RuntimeError('raised by tearDown') 11275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 11375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError', 11475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 'stopTest'] 11575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unittest2.FunctionTestCase(test, setUp, tearDown).run(result) 11675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(events, expected) 11775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 11875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "Return a string identifying the specific test case." 11975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # 12075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # Because of the vague nature of the docs, I'm not going to lock this 12175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # test down too much. Really all that can be asserted is that the id() 12275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # will be a string (either 8-byte or unicode -- again, because the docs 12375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # just say "string") 12475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_id(self): 12575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test = unittest2.FunctionTestCase(lambda: None) 12675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 12775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertIsInstance(test.id(), basestring) 12875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 12975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "Returns a one-line description of the test, or None if no description 13075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # has been provided. The default implementation of this method returns 13175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # the first line of the test method's docstring, if available, or None." 13275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_shortDescription__no_docstring(self): 13375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test = unittest2.FunctionTestCase(lambda: None) 13475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 13575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(test.shortDescription(), None) 13675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 13775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # "Returns a one-line description of the test, or None if no description 13875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # has been provided. The default implementation of this method returns 13975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen # the first line of the test method's docstring, if available, or None." 14075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen def test_shortDescription__singleline_docstring(self): 14175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen desc = "this tests foo" 14275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen test = unittest2.FunctionTestCase(lambda: None, description=desc) 14375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 14475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen self.assertEqual(test.shortDescription(), "this tests foo") 14575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 14675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 14775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen 14875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenif __name__ == '__main__': 14975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen unittest2.main() 150