summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2022-11-22 15:13:37 (GMT)
committerGitHub <noreply@github.com>2022-11-22 15:13:37 (GMT)
commit190331ed280ba292dc3d0419f65ed2ecf12aa06e (patch)
tree0cce78b3f6193a055565036ebbafd2d4a58d029c /Lib
parent1c5e8602922881604ee25fc9fd98f30630a7f105 (diff)
downloadcpython-190331ed280ba292dc3d0419f65ed2ecf12aa06e.zip
cpython-190331ed280ba292dc3d0419f65ed2ecf12aa06e.tar.gz
cpython-190331ed280ba292dc3d0419f65ed2ecf12aa06e.tar.bz2
[3.10] Add more details in test_unittest (GH-99626). (GH-99692)
(cherry picked from commit 653e563d80fabee8830e0b55f194f82a9beabe70)
Diffstat (limited to 'Lib')
-rw-r--r--Lib/unittest/test/test_async_case.py66
-rw-r--r--Lib/unittest/test/test_runner.py32
2 files changed, 63 insertions, 35 deletions
diff --git a/Lib/unittest/test/test_async_case.py b/Lib/unittest/test/test_async_case.py
index b97ad93..a68ae86 100644
--- a/Lib/unittest/test/test_async_case.py
+++ b/Lib/unittest/test/test_async_case.py
@@ -20,59 +20,73 @@ class TestAsyncCase(unittest.TestCase):
self.addCleanup(support.gc_collect)
def test_full_cycle(self):
+ expected = ['setUp',
+ 'asyncSetUp',
+ 'test',
+ 'asyncTearDown',
+ 'tearDown',
+ 'cleanup6',
+ 'cleanup5',
+ 'cleanup4',
+ 'cleanup3',
+ 'cleanup2',
+ 'cleanup1']
class Test(unittest.IsolatedAsyncioTestCase):
def setUp(self):
self.assertEqual(events, [])
events.append('setUp')
+ self.addCleanup(self.on_cleanup1)
+ self.addAsyncCleanup(self.on_cleanup2)
async def asyncSetUp(self):
- self.assertEqual(events, ['setUp'])
+ self.assertEqual(events, expected[:1])
events.append('asyncSetUp')
- self.addAsyncCleanup(self.on_cleanup1)
+ self.addCleanup(self.on_cleanup3)
+ self.addAsyncCleanup(self.on_cleanup4)
async def test_func(self):
- self.assertEqual(events, ['setUp',
- 'asyncSetUp'])
+ self.assertEqual(events, expected[:2])
events.append('test')
- self.addAsyncCleanup(self.on_cleanup2)
+ self.addCleanup(self.on_cleanup5)
+ self.addAsyncCleanup(self.on_cleanup6)
async def asyncTearDown(self):
- self.assertEqual(events, ['setUp',
- 'asyncSetUp',
- 'test'])
+ self.assertEqual(events, expected[:3])
events.append('asyncTearDown')
def tearDown(self):
- self.assertEqual(events, ['setUp',
- 'asyncSetUp',
- 'test',
- 'asyncTearDown'])
+ self.assertEqual(events, expected[:4])
events.append('tearDown')
- async def on_cleanup1(self):
- self.assertEqual(events, ['setUp',
- 'asyncSetUp',
- 'test',
- 'asyncTearDown',
- 'tearDown',
- 'cleanup2'])
+ def on_cleanup1(self):
+ self.assertEqual(events, expected[:10])
events.append('cleanup1')
async def on_cleanup2(self):
- self.assertEqual(events, ['setUp',
- 'asyncSetUp',
- 'test',
- 'asyncTearDown',
- 'tearDown'])
+ self.assertEqual(events, expected[:9])
events.append('cleanup2')
+ def on_cleanup3(self):
+ self.assertEqual(events, expected[:8])
+ events.append('cleanup3')
+
+ async def on_cleanup4(self):
+ self.assertEqual(events, expected[:7])
+ events.append('cleanup4')
+
+ def on_cleanup5(self):
+ self.assertEqual(events, expected[:6])
+ events.append('cleanup5')
+
+ async def on_cleanup6(self):
+ self.assertEqual(events, expected[:5])
+ events.append('cleanup6')
+
events = []
test = Test("test_func")
result = test.run()
self.assertEqual(result.errors, [])
self.assertEqual(result.failures, [])
- expected = ['setUp', 'asyncSetUp', 'test',
- 'asyncTearDown', 'tearDown', 'cleanup2', 'cleanup1']
self.assertEqual(events, expected)
events = []
diff --git a/Lib/unittest/test/test_runner.py b/Lib/unittest/test/test_runner.py
index 453e6c3..613ff5b 100644
--- a/Lib/unittest/test/test_runner.py
+++ b/Lib/unittest/test/test_runner.py
@@ -106,11 +106,13 @@ class TestCleanUp(unittest.TestCase):
class TestableTest(unittest.TestCase):
def setUp(self):
ordering.append('setUp')
+ test.addCleanup(cleanup2)
if blowUp:
raise Exception('foo')
def testNothing(self):
ordering.append('test')
+ test.addCleanup(cleanup3)
def tearDown(self):
ordering.append('tearDown')
@@ -121,8 +123,9 @@ class TestCleanUp(unittest.TestCase):
ordering.append('cleanup1')
def cleanup2():
ordering.append('cleanup2')
+ def cleanup3():
+ ordering.append('cleanup3')
test.addCleanup(cleanup1)
- test.addCleanup(cleanup2)
def success(some_test):
self.assertEqual(some_test, test)
@@ -132,7 +135,7 @@ class TestCleanUp(unittest.TestCase):
result.addSuccess = success
test.run(result)
- self.assertEqual(ordering, ['setUp', 'test', 'tearDown',
+ self.assertEqual(ordering, ['setUp', 'test', 'tearDown', 'cleanup3',
'cleanup2', 'cleanup1', 'success'])
blowUp = True
@@ -140,7 +143,7 @@ class TestCleanUp(unittest.TestCase):
test = TestableTest('testNothing')
test.addCleanup(cleanup1)
test.run(result)
- self.assertEqual(ordering, ['setUp', 'cleanup1'])
+ self.assertEqual(ordering, ['setUp', 'cleanup2', 'cleanup1'])
def testTestCaseDebugExecutesCleanups(self):
ordering = []
@@ -152,9 +155,11 @@ class TestCleanUp(unittest.TestCase):
def testNothing(self):
ordering.append('test')
+ self.addCleanup(cleanup3)
def tearDown(self):
ordering.append('tearDown')
+ test.addCleanup(cleanup4)
test = TestableTest('testNothing')
@@ -163,9 +168,14 @@ class TestCleanUp(unittest.TestCase):
test.addCleanup(cleanup2)
def cleanup2():
ordering.append('cleanup2')
+ def cleanup3():
+ ordering.append('cleanup3')
+ def cleanup4():
+ ordering.append('cleanup4')
test.debug()
- self.assertEqual(ordering, ['setUp', 'test', 'tearDown', 'cleanup1', 'cleanup2'])
+ self.assertEqual(ordering, ['setUp', 'test', 'tearDown', 'cleanup4',
+ 'cleanup3', 'cleanup1', 'cleanup2'])
class TestClassCleanup(unittest.TestCase):
@@ -291,13 +301,14 @@ class TestClassCleanup(unittest.TestCase):
ordering.append('test')
@classmethod
def tearDownClass(cls):
+ ordering.append('tearDownClass')
raise Exception('TearDownClassExc')
suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestableTest)
with self.assertRaises(Exception) as cm:
suite.debug()
self.assertEqual(str(cm.exception), 'TearDownClassExc')
- self.assertEqual(ordering, ['setUpClass', 'test'])
+ self.assertEqual(ordering, ['setUpClass', 'test', 'tearDownClass'])
self.assertTrue(TestableTest._class_cleanups)
TestableTest._class_cleanups.clear()
@@ -307,7 +318,7 @@ class TestClassCleanup(unittest.TestCase):
with self.assertRaises(Exception) as cm:
suite.debug()
self.assertEqual(str(cm.exception), 'TearDownClassExc')
- self.assertEqual(ordering, ['setUpClass', 'test'])
+ self.assertEqual(ordering, ['setUpClass', 'test', 'tearDownClass'])
self.assertTrue(TestableTest._class_cleanups)
TestableTest._class_cleanups.clear()
@@ -657,6 +668,7 @@ class TestModuleCleanUp(unittest.TestCase):
unittest.addModuleCleanup(cleanup, ordering)
@staticmethod
def tearDownModule():
+ ordering.append('tearDownModule')
raise Exception('CleanUpExc')
class TestableTest(unittest.TestCase):
@@ -675,7 +687,8 @@ class TestModuleCleanUp(unittest.TestCase):
self.assertEqual(result.errors[0][1].splitlines()[-1],
'Exception: CleanUpExc')
self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
- 'tearDownClass', 'cleanup_good'])
+ 'tearDownClass', 'tearDownModule',
+ 'cleanup_good'])
self.assertEqual(unittest.case._module_cleanups, [])
def test_debug_module_executes_cleanUp(self):
@@ -729,6 +742,7 @@ class TestModuleCleanUp(unittest.TestCase):
unittest.addModuleCleanup(cleanup, ordering, blowUp=blowUp)
@staticmethod
def tearDownModule():
+ ordering.append('tearDownModule')
raise Exception('TearDownModuleExc')
class TestableTest(unittest.TestCase):
@@ -748,7 +762,7 @@ class TestModuleCleanUp(unittest.TestCase):
suite.debug()
self.assertEqual(str(cm.exception), 'TearDownModuleExc')
self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
- 'tearDownClass'])
+ 'tearDownClass', 'tearDownModule'])
self.assertTrue(unittest.case._module_cleanups)
unittest.case._module_cleanups.clear()
@@ -759,7 +773,7 @@ class TestModuleCleanUp(unittest.TestCase):
suite.debug()
self.assertEqual(str(cm.exception), 'TearDownModuleExc')
self.assertEqual(ordering, ['setUpModule', 'setUpClass', 'test',
- 'tearDownClass'])
+ 'tearDownClass', 'tearDownModule'])
self.assertTrue(unittest.case._module_cleanups)
unittest.case._module_cleanups.clear()