summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2022-11-22 17:11:57 (GMT)
committerGitHub <noreply@github.com>2022-11-22 17:11:57 (GMT)
commit400d41b8ea73e71ca583daca94da3db06417f210 (patch)
tree526b11eefc02dfb811a4e64026c084a9823db180
parent190331ed280ba292dc3d0419f65ed2ecf12aa06e (diff)
downloadcpython-400d41b8ea73e71ca583daca94da3db06417f210.zip
cpython-400d41b8ea73e71ca583daca94da3db06417f210.tar.gz
cpython-400d41b8ea73e71ca583daca94da3db06417f210.tar.bz2
[3.10] gh-99645: Fix a bug in handling class cleanups in unittest.TestCase (GH-99646) (GH-99699)
Now addClassCleanup() uses separate lists for different TestCase subclasses, and doClassCleanups() only cleans up the particular class. (cherry picked from commit c2102136be569e6fc8ed90181f229b46d07142f8)
-rw-r--r--Lib/unittest/case.py10
-rw-r--r--Lib/unittest/test/test_runner.py27
-rw-r--r--Misc/NEWS.d/next/Library/2022-11-21-13-49-03.gh-issue-99645.9w1QKq.rst3
3 files changed, 35 insertions, 5 deletions
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index 61003d0..50100e9 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -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
diff --git a/Lib/unittest/test/test_runner.py b/Lib/unittest/test/test_runner.py
index 613ff5b..0082d39 100644
--- a/Lib/unittest/test/test_runner.py
+++ b/Lib/unittest/test/test_runner.py
@@ -457,6 +457,33 @@ class TestClassCleanup(unittest.TestCase):
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):
diff --git a/Misc/NEWS.d/next/Library/2022-11-21-13-49-03.gh-issue-99645.9w1QKq.rst b/Misc/NEWS.d/next/Library/2022-11-21-13-49-03.gh-issue-99645.9w1QKq.rst
new file mode 100644
index 0000000..f6ee449
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-11-21-13-49-03.gh-issue-99645.9w1QKq.rst
@@ -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.