]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-99645: Fix a bug in handling class cleanups in unittest.TestCase (GH-99646)
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 22 Nov 2022 15:49:37 +0000 (17:49 +0200)
committerGitHub <noreply@github.com>
Tue, 22 Nov 2022 15:49:37 +0000 (17:49 +0200)
Now addClassCleanup() uses separate lists for different TestCase subclasses,
and doClassCleanups() only cleans up the particular class.

Lib/test/test_unittest/test_runner.py
Lib/unittest/case.py
Misc/NEWS.d/next/Library/2022-11-21-13-49-03.gh-issue-99645.9w1QKq.rst [new file with mode: 0644]

index 569b2654aa12eb23c3bf0156f58a33b5dbe0e520..df584b7620d092fc1b5101a4e6ca44cf4921151e 100644 (file)
@@ -547,6 +547,33 @@ class TestClassCleanup(unittest.TestCase):
 
         self.assertEqual(TestableTest._class_cleanups, [])
 
+    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):
index b01f6605e23e3915d5b029d6e7ae80e07883d48a..5167c5f843f085c0bb8bcb66b12a993855a208e9 100644 (file)
@@ -384,11 +384,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/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 (file)
index 0000000..f6ee449
--- /dev/null
@@ -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.