FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add more details in test_unittest by serhiy-storchaka · Pull Request #99626 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
70 changes: 44 additions & 26 deletions Lib/test/test_unittest/test_async_case.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -49,69 +49,87 @@ def setUp(self):
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')
VAR.set(VAR.get() + ('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')
VAR.set(VAR.get() + ('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')
VAR.set(VAR.get() + ('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])
VAR.set(VAR.get() + ('asyncTearDown',))
events.append('asyncTearDown')

def tearDown(self):
self.assertEqual(events, ['setUp',
'asyncSetUp',
'test',
'asyncTearDown'])
self.assertEqual(events, expected[:4])
events.append('tearDown')
VAR.set(VAR.get() + ('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')
VAR.set(VAR.get() + ('cleanup1',))
nonlocal cvar
cvar = VAR.get()

async def on_cleanup2(self):
self.assertEqual(events, ['setUp',
'asyncSetUp',
'test',
'asyncTearDown',
'tearDown'])
self.assertEqual(events, expected[:9])
events.append('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 = []
cvar = ()
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)
self.assertEqual(cvar, tuple(expected))

Expand Down
32 changes: 23 additions & 9 deletions Lib/test/test_unittest/test_runner.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@ def testCleanupInRun(self):
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')
Expand All @@ -149,8 +151,9 @@ def cleanup1():
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)
Expand All @@ -160,15 +163,15 @@ def success(some_test):
result.addSuccess = success

test.run(result)
self.assertEqual(ordering, ['setUp', 'test', 'tearDown',
self.assertEqual(ordering, ['setUp', 'test', 'tearDown', 'cleanup3',
'cleanup2', 'cleanup1', 'success'])

blowUp = True
ordering = []
test = TestableTest('testNothing')
test.addCleanup(cleanup1)
test.run(result)
self.assertEqual(ordering, ['setUp', 'cleanup1'])
self.assertEqual(ordering, ['setUp', 'cleanup2', 'cleanup1'])

def testTestCaseDebugExecutesCleanups(self):
ordering = []
Expand All @@ -180,9 +183,11 @@ def setUp(self):

def testNothing(self):
ordering.append('test')
self.addCleanup(cleanup3)

def tearDown(self):
ordering.append('tearDown')
test.addCleanup(cleanup4)

test = TestableTest('testNothing')

Expand All @@ -191,9 +196,14 @@ def cleanup1():
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'])


def test_enterContext(self):
Expand Down Expand Up @@ -352,13 +362,14 @@ def testNothing(self):
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()

Expand All @@ -368,7 +379,7 @@ def tearDownClass(cls):
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()

Expand Down Expand Up @@ -747,6 +758,7 @@ def setUpModule():
unittest.addModuleCleanup(cleanup, ordering)
@staticmethod
def tearDownModule():
ordering.append('tearDownModule')
raise Exception('CleanUpExc')

class TestableTest(unittest.TestCase):
Expand All @@ -765,7 +777,8 @@ def tearDownClass(cls):
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):
Expand Down Expand Up @@ -819,6 +832,7 @@ def setUpModule():
unittest.addModuleCleanup(cleanup, ordering, blowUp=blowUp)
@staticmethod
def tearDownModule():
ordering.append('tearDownModule')
raise Exception('TearDownModuleExc')

class TestableTest(unittest.TestCase):
Expand All @@ -838,7 +852,7 @@ def tearDownClass(cls):
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()

Expand All @@ -849,7 +863,7 @@ def tearDownClass(cls):
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()

Expand Down

Back | FazBrowse Home | New Git URL