12560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foordimport unittest 22560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 31241c47aa2af26d90be1fdb6b0990a08bf9beafdEzio Melottifrom unittest.test.support import LoggingResult 42560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 52560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 62560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foordclass Test_FunctionTestCase(unittest.TestCase): 72560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 82560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # "Return the number of tests represented by the this test object. For 92560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # TestCase instances, this will always be 1" 102560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def test_countTestCases(self): 112560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord test = unittest.FunctionTestCase(lambda: None) 122560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 132560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord self.assertEqual(test.countTestCases(), 1) 142560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 152560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # "When a setUp() method is defined, the test runner will run that method 162560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # prior to each test. Likewise, if a tearDown() method is defined, the 172560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # test runner will invoke that method after each test. In the example, 182560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # setUp() was used to create a fresh sequence for each test." 192560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # 202560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # Make sure the proper call order is maintained, even if setUp() raises 212560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # an exception. 222560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def test_run_call_order__error_in_setUp(self): 232560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord events = [] 242560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord result = LoggingResult(events) 252560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 262560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def setUp(): 272560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord events.append('setUp') 282560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord raise RuntimeError('raised by setUp') 292560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 302560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def test(): 312560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord events.append('test') 322560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 332560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def tearDown(): 342560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord events.append('tearDown') 352560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 362560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord expected = ['startTest', 'setUp', 'addError', 'stopTest'] 372560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord unittest.FunctionTestCase(test, setUp, tearDown).run(result) 382560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord self.assertEqual(events, expected) 392560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 402560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # "When a setUp() method is defined, the test runner will run that method 412560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # prior to each test. Likewise, if a tearDown() method is defined, the 422560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # test runner will invoke that method after each test. In the example, 432560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # setUp() was used to create a fresh sequence for each test." 442560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # 452560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # Make sure the proper call order is maintained, even if the test raises 462560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # an error (as opposed to a failure). 472560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def test_run_call_order__error_in_test(self): 482560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord events = [] 492560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord result = LoggingResult(events) 502560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 512560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def setUp(): 522560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord events.append('setUp') 532560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 542560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def test(): 552560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord events.append('test') 562560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord raise RuntimeError('raised by test') 572560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 582560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def tearDown(): 592560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord events.append('tearDown') 602560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 61b3468f79efa45c8adaf86c0b9b797b9b3d4c12a2Michael Foord expected = ['startTest', 'setUp', 'test', 'tearDown', 62b3468f79efa45c8adaf86c0b9b797b9b3d4c12a2Michael Foord 'addError', 'stopTest'] 632560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord unittest.FunctionTestCase(test, setUp, tearDown).run(result) 642560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord self.assertEqual(events, expected) 652560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 662560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # "When a setUp() method is defined, the test runner will run that method 672560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # prior to each test. Likewise, if a tearDown() method is defined, the 682560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # test runner will invoke that method after each test. In the example, 692560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # setUp() was used to create a fresh sequence for each test." 702560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # 712560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # Make sure the proper call order is maintained, even if the test signals 722560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # a failure (as opposed to an error). 732560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def test_run_call_order__failure_in_test(self): 742560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord events = [] 752560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord result = LoggingResult(events) 762560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 772560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def setUp(): 782560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord events.append('setUp') 792560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 802560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def test(): 812560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord events.append('test') 822560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord self.fail('raised by test') 832560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 842560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def tearDown(): 852560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord events.append('tearDown') 862560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 87b3468f79efa45c8adaf86c0b9b797b9b3d4c12a2Michael Foord expected = ['startTest', 'setUp', 'test', 'tearDown', 88b3468f79efa45c8adaf86c0b9b797b9b3d4c12a2Michael Foord 'addFailure', 'stopTest'] 892560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord unittest.FunctionTestCase(test, setUp, tearDown).run(result) 902560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord self.assertEqual(events, expected) 912560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 922560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # "When a setUp() method is defined, the test runner will run that method 932560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # prior to each test. Likewise, if a tearDown() method is defined, the 942560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # test runner will invoke that method after each test. In the example, 952560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # setUp() was used to create a fresh sequence for each test." 962560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # 972560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # Make sure the proper call order is maintained, even if tearDown() raises 982560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # an exception. 992560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def test_run_call_order__error_in_tearDown(self): 1002560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord events = [] 1012560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord result = LoggingResult(events) 1022560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 1032560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def setUp(): 1042560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord events.append('setUp') 1052560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 1062560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def test(): 1072560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord events.append('test') 1082560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 1092560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def tearDown(): 1102560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord events.append('tearDown') 1112560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord raise RuntimeError('raised by tearDown') 1122560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 1132560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError', 1142560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 'stopTest'] 1152560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord unittest.FunctionTestCase(test, setUp, tearDown).run(result) 1162560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord self.assertEqual(events, expected) 1172560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 1182560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # "Return a string identifying the specific test case." 1192560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # 1202560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # Because of the vague nature of the docs, I'm not going to lock this 1212560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # test down too much. Really all that can be asserted is that the id() 1222560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # will be a string (either 8-byte or unicode -- again, because the docs 1232560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # just say "string") 1242560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def test_id(self): 1252560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord test = unittest.FunctionTestCase(lambda: None) 1262560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 1272560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord self.assertIsInstance(test.id(), str) 1282560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 1292560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # "Returns a one-line description of the test, or None if no description 1302560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # has been provided. The default implementation of this method returns 1312560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # the first line of the test method's docstring, if available, or None." 1322560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def test_shortDescription__no_docstring(self): 1332560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord test = unittest.FunctionTestCase(lambda: None) 1342560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 1352560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord self.assertEqual(test.shortDescription(), None) 1362560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 1372560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # "Returns a one-line description of the test, or None if no description 1382560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # has been provided. The default implementation of this method returns 1392560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord # the first line of the test method's docstring, if available, or None." 1402560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord def test_shortDescription__singleline_docstring(self): 1412560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord desc = "this tests foo" 1422560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord test = unittest.FunctionTestCase(lambda: None, description=desc) 1432560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord 1442560e5cf5335011a3cb81562fe4028a0fb8c35daMichael Foord self.assertEqual(test.shortDescription(), "this tests foo") 1451d7c8c9a00f57df32b6b5c055b3e5a2d11ed1f05Antoine Pitrou 1461d7c8c9a00f57df32b6b5c055b3e5a2d11ed1f05Antoine Pitrou 1471d7c8c9a00f57df32b6b5c055b3e5a2d11ed1f05Antoine Pitrouif __name__ == "__main__": 1481d7c8c9a00f57df32b6b5c055b3e5a2d11ed1f05Antoine Pitrou unittest.main() 149