Add more details in test_unittest (GH-99626)

(cherry picked from commit 653e563d80)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2022-11-21 04:23:09 -08:00 committed by GitHub
parent 9dda9020ab
commit 555e76e907
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 42 deletions

View file

@ -49,69 +49,87 @@ class TestAsyncCase(unittest.TestCase):
self.addCleanup(support.gc_collect) self.addCleanup(support.gc_collect)
def test_full_cycle(self): def test_full_cycle(self):
expected = ['setUp',
'asyncSetUp',
'test',
'asyncTearDown',
'tearDown',
'cleanup6',
'cleanup5',
'cleanup4',
'cleanup3',
'cleanup2',
'cleanup1']
class Test(unittest.IsolatedAsyncioTestCase): class Test(unittest.IsolatedAsyncioTestCase):
def setUp(self): def setUp(self):
self.assertEqual(events, []) self.assertEqual(events, [])
events.append('setUp') events.append('setUp')
VAR.set(VAR.get() + ('setUp',)) VAR.set(VAR.get() + ('setUp',))
self.addCleanup(self.on_cleanup1)
async def asyncSetUp(self):
self.assertEqual(events, ['setUp'])
events.append('asyncSetUp')
VAR.set(VAR.get() + ('asyncSetUp',))
self.addAsyncCleanup(self.on_cleanup1)
async def test_func(self):
self.assertEqual(events, ['setUp',
'asyncSetUp'])
events.append('test')
VAR.set(VAR.get() + ('test',))
self.addAsyncCleanup(self.on_cleanup2) self.addAsyncCleanup(self.on_cleanup2)
async def asyncSetUp(self):
self.assertEqual(events, expected[:1])
events.append('asyncSetUp')
VAR.set(VAR.get() + ('asyncSetUp',))
self.addCleanup(self.on_cleanup3)
self.addAsyncCleanup(self.on_cleanup4)
async def test_func(self):
self.assertEqual(events, expected[:2])
events.append('test')
VAR.set(VAR.get() + ('test',))
self.addCleanup(self.on_cleanup5)
self.addAsyncCleanup(self.on_cleanup6)
async def asyncTearDown(self): async def asyncTearDown(self):
self.assertEqual(events, ['setUp', self.assertEqual(events, expected[:3])
'asyncSetUp',
'test'])
VAR.set(VAR.get() + ('asyncTearDown',)) VAR.set(VAR.get() + ('asyncTearDown',))
events.append('asyncTearDown') events.append('asyncTearDown')
def tearDown(self): def tearDown(self):
self.assertEqual(events, ['setUp', self.assertEqual(events, expected[:4])
'asyncSetUp',
'test',
'asyncTearDown'])
events.append('tearDown') events.append('tearDown')
VAR.set(VAR.get() + ('tearDown',)) VAR.set(VAR.get() + ('tearDown',))
async def on_cleanup1(self): def on_cleanup1(self):
self.assertEqual(events, ['setUp', self.assertEqual(events, expected[:10])
'asyncSetUp',
'test',
'asyncTearDown',
'tearDown',
'cleanup2'])
events.append('cleanup1') events.append('cleanup1')
VAR.set(VAR.get() + ('cleanup1',)) VAR.set(VAR.get() + ('cleanup1',))
nonlocal cvar nonlocal cvar
cvar = VAR.get() cvar = VAR.get()
async def on_cleanup2(self): async def on_cleanup2(self):
self.assertEqual(events, ['setUp', self.assertEqual(events, expected[:9])
'asyncSetUp',
'test',
'asyncTearDown',
'tearDown'])
events.append('cleanup2') events.append('cleanup2')
VAR.set(VAR.get() + ('cleanup2',)) VAR.set(VAR.get() + ('cleanup2',))
def on_cleanup3(self):
self.assertEqual(events, expected[:8])
events.append('cleanup3')
VAR.set(VAR.get() + ('cleanup3',))
async def on_cleanup4(self):
self.assertEqual(events, expected[:7])
events.append('cleanup4')
VAR.set(VAR.get() + ('cleanup4',))
def on_cleanup5(self):
self.assertEqual(events, expected[:6])
events.append('cleanup5')
VAR.set(VAR.get() + ('cleanup5',))
async def on_cleanup6(self):
self.assertEqual(events, expected[:5])
events.append('cleanup6')
VAR.set(VAR.get() + ('cleanup6',))
events = [] events = []
cvar = () cvar = ()
test = Test("test_func") test = Test("test_func")
result = test.run() result = test.run()
self.assertEqual(result.errors, []) self.assertEqual(result.errors, [])
self.assertEqual(result.failures, []) self.assertEqual(result.failures, [])
expected = ['setUp', 'asyncSetUp', 'test',
'asyncTearDown', 'tearDown', 'cleanup2', 'cleanup1']
self.assertEqual(events, expected) self.assertEqual(events, expected)
self.assertEqual(cvar, tuple(expected)) self.assertEqual(cvar, tuple(expected))

View file

@ -134,11 +134,13 @@ class TestCleanUp(unittest.TestCase):
class TestableTest(unittest.TestCase): class TestableTest(unittest.TestCase):
def setUp(self): def setUp(self):
ordering.append('setUp') ordering.append('setUp')
test.addCleanup(cleanup2)
if blowUp: if blowUp:
raise Exception('foo') raise Exception('foo')
def testNothing(self): def testNothing(self):
ordering.append('test') ordering.append('test')
test.addCleanup(cleanup3)
def tearDown(self): def tearDown(self):
ordering.append('tearDown') ordering.append('tearDown')
@ -149,8 +151,9 @@ class TestCleanUp(unittest.TestCase):
ordering.append('cleanup1') ordering.append('cleanup1')
def cleanup2(): def cleanup2():
ordering.append('cleanup2') ordering.append('cleanup2')
def cleanup3():
ordering.append('cleanup3')
test.addCleanup(cleanup1) test.addCleanup(cleanup1)
test.addCleanup(cleanup2)
def success(some_test): def success(some_test):
self.assertEqual(some_test, test) self.assertEqual(some_test, test)
@ -160,7 +163,7 @@ class TestCleanUp(unittest.TestCase):
result.addSuccess = success result.addSuccess = success
test.run(result) test.run(result)
self.assertEqual(ordering, ['setUp', 'test', 'tearDown', self.assertEqual(ordering, ['setUp', 'test', 'tearDown', 'cleanup3',
'cleanup2', 'cleanup1', 'success']) 'cleanup2', 'cleanup1', 'success'])
blowUp = True blowUp = True
@ -168,7 +171,7 @@ class TestCleanUp(unittest.TestCase):
test = TestableTest('testNothing') test = TestableTest('testNothing')
test.addCleanup(cleanup1) test.addCleanup(cleanup1)
test.run(result) test.run(result)
self.assertEqual(ordering, ['setUp', 'cleanup1']) self.assertEqual(ordering, ['setUp', 'cleanup2', 'cleanup1'])
def testTestCaseDebugExecutesCleanups(self): def testTestCaseDebugExecutesCleanups(self):
ordering = [] ordering = []
@ -180,9 +183,11 @@ class TestCleanUp(unittest.TestCase):
def testNothing(self): def testNothing(self):
ordering.append('test') ordering.append('test')
self.addCleanup(cleanup3)
def tearDown(self): def tearDown(self):
ordering.append('tearDown') ordering.append('tearDown')
test.addCleanup(cleanup4)
test = TestableTest('testNothing') test = TestableTest('testNothing')
@ -191,9 +196,14 @@ class TestCleanUp(unittest.TestCase):
test.addCleanup(cleanup2) test.addCleanup(cleanup2)
def cleanup2(): def cleanup2():
ordering.append('cleanup2') ordering.append('cleanup2')
def cleanup3():
ordering.append('cleanup3')
def cleanup4():
ordering.append('cleanup4')
test.debug() test.debug()
self.assertEqual(ordering, ['setUp', 'test', 'tearDown', 'cleanup1', 'cleanup2']) self.assertEqual(ordering, ['setUp', 'test', 'tearDown', 'cleanup4',
'cleanup3', 'cleanup1', 'cleanup2'])
def test_enterContext(self): def test_enterContext(self):
@ -352,13 +362,14 @@ class TestClassCleanup(unittest.TestCase):
ordering.append('test') ordering.append('test')
@classmethod @classmethod
def tearDownClass(cls): def tearDownClass(cls):
ordering.append('tearDownClass')
raise Exception('TearDownClassExc') raise Exception('TearDownClassExc')
suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest) suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
with self.assertRaises(Exception) as cm: with self.assertRaises(Exception) as cm:
suite.debug() suite.debug()
self.assertEqual(str(cm.exception), 'TearDownClassExc') self.assertEqual(str(cm.exception), 'TearDownClassExc')
self.assertEqual(ordering, ['setUpClass', 'test']) self.assertEqual(ordering, ['setUpClass', 'test', 'tearDownClass'])
self.assertTrue(TestableTest._class_cleanups) self.assertTrue(TestableTest._class_cleanups)
TestableTest._class_cleanups.clear() TestableTest._class_cleanups.clear()
@ -368,7 +379,7 @@ class TestClassCleanup(unittest.TestCase):
with self.assertRaises(Exception) as cm: with self.assertRaises(Exception) as cm:
suite.debug() suite.debug()
self.assertEqual(str(cm.exception), 'TearDownClassExc') self.assertEqual(str(cm.exception), 'TearDownClassExc')
self.assertEqual(ordering, ['setUpClass', 'test']) self.assertEqual(ordering, ['setUpClass', 'test', 'tearDownClass'])
self.assertTrue(TestableTest._class_cleanups) self.assertTrue(TestableTest._class_cleanups)
TestableTest._class_cleanups.clear() TestableTest._class_cleanups.clear()
@ -747,6 +758,7 @@ class TestModuleCleanUp(unittest.TestCase):
unittest.addModuleCleanup(cleanup, ordering) unittest.addModuleCleanup(cleanup, ordering)
@staticmethod @staticmethod
def tearDownModule(): def tearDownModule():
ordering.append('tearDownModule')
raise Exception('CleanUpExc') raise Exception('CleanUpExc')
class TestableTest(unittest.TestCase): class TestableTest(unittest.TestCase):
@ -765,7 +777,8 @@ class TestModuleCleanUp(unittest.TestCase):
self.assertEqual(result.errors[0][1].splitlines()[-1], self.assertEqual(result.errors[0][1].splitlines()[-1],
'Exception: CleanUpExc') 'Exception: CleanUpExc')
self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test', self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
'tearDownClass', 'cleanup_good']) 'tearDownClass', 'tearDownModule',
'cleanup_good'])
self.assertEqual(unittest.case._module_cleanups, []) self.assertEqual(unittest.case._module_cleanups, [])
def test_debug_module_executes_cleanUp(self): def test_debug_module_executes_cleanUp(self):
@ -819,6 +832,7 @@ class TestModuleCleanUp(unittest.TestCase):
unittest.addModuleCleanup(cleanup, ordering, blowUp=blowUp) unittest.addModuleCleanup(cleanup, ordering, blowUp=blowUp)
@staticmethod @staticmethod
def tearDownModule(): def tearDownModule():
ordering.append('tearDownModule')
raise Exception('TearDownModuleExc') raise Exception('TearDownModuleExc')
class TestableTest(unittest.TestCase): class TestableTest(unittest.TestCase):
@ -838,7 +852,7 @@ class TestModuleCleanUp(unittest.TestCase):
suite.debug() suite.debug()
self.assertEqual(str(cm.exception), 'TearDownModuleExc') self.assertEqual(str(cm.exception), 'TearDownModuleExc')
self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test', self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
'tearDownClass']) 'tearDownClass', 'tearDownModule'])
self.assertTrue(unittest.case._module_cleanups) self.assertTrue(unittest.case._module_cleanups)
unittest.case._module_cleanups.clear() unittest.case._module_cleanups.clear()
@ -849,7 +863,7 @@ class TestModuleCleanUp(unittest.TestCase):
suite.debug() suite.debug()
self.assertEqual(str(cm.exception), 'TearDownModuleExc') self.assertEqual(str(cm.exception), 'TearDownModuleExc')
self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test', self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
'tearDownClass']) 'tearDownClass', 'tearDownModule'])
self.assertTrue(unittest.case._module_cleanups) self.assertTrue(unittest.case._module_cleanups)
unittest.case._module_cleanups.clear() unittest.case._module_cleanups.clear()