Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[3.10] gh-99645: Fix a bug in handling class cleanups in unittest.TestCase (GH-99646) #99699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Lib/unittest/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,11 @@ class TestCase(object):
# of difflib. See #11763.
_diffThreshold = 2**16

# Attribute used by TestSuite for classSetUp

_classSetupFailed = False

_class_cleanups = []
def __init_subclass__(cls, *args, **kwargs):
# Attribute used by TestSuite for classSetUp
cls._classSetupFailed = False
cls._class_cleanups = []
super().__init_subclass__(*args, **kwargs)

def __init__(self, methodName='runTest'):
"""Create an instance of the class that will use the named test
Expand Down
27 changes: 27 additions & 0 deletions Lib/unittest/test/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,33 @@ def tearDownClass(cls):
self.assertEqual(ordering,
['setUpClass', 'test', 'tearDownClass', 'cleanup_good'])

def test_run_nested_test(self):
ordering = []

class InnerTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
ordering.append('inner setup')
cls.addClassCleanup(ordering.append, 'inner cleanup')
def test(self):
ordering.append('inner test')

class OuterTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
ordering.append('outer setup')
cls.addClassCleanup(ordering.append, 'outer cleanup')
def test(self):
ordering.append('start outer test')
runTests(InnerTest)
ordering.append('end outer test')

runTests(OuterTest)
self.assertEqual(ordering, [
'outer setup', 'start outer test',
'inner setup', 'inner test', 'inner cleanup',
'end outer test', 'outer cleanup'])


class TestModuleCleanUp(unittest.TestCase):
def test_add_and_do_ModuleCleanup(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a bug in handling class cleanups in :class:`unittest.TestCase`. Now
``addClassCleanup()`` uses separate lists for different ``TestCase``
subclasses, and ``doClassCleanups()`` only cleans up the particular class.