[Python-checkins] bpo-36674: Honour the skipping decorators in TestCase.debug() (GH-28446)

miss-islington webhook-mailer at python.org
Sat Sep 18 09:22:36 EDT 2021


https://github.com/python/cpython/commit/753f7af22e78456726496fd5d8bf38e7e6567e4e
commit: 753f7af22e78456726496fd5d8bf38e7e6567e4e
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-09-18T06:22:31-07:00
summary:

bpo-36674: Honour the skipping decorators in TestCase.debug() (GH-28446)


unittest.TestCase.debug() raises now a SkipTest if the class or
the test method are decorated with the skipping decorator.

Previously it only raised a SkipTest if the test method was decorated
with other decorator in addition to the skipping decorator, or
if SkipTest was explicitly raised in the test or setup methods.
(cherry picked from commit dea59cf88adf5d20812edda330e085a4695baba4)

Co-authored-by: Serhiy Storchaka <storchaka at gmail.com>

files:
A Misc/NEWS.d/next/Library/2021-09-18-13-14-57.bpo-36674.a2k5Zb.rst
M Lib/unittest/case.py
M Lib/unittest/test/test_skipping.py

diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index e8d9c05b02337..b771ce06332ae 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -651,8 +651,16 @@ def __call__(self, *args, **kwds):
 
     def debug(self):
         """Run the test without collecting errors in a TestResult"""
+        testMethod = getattr(self, self._testMethodName)
+        if (getattr(self.__class__, "__unittest_skip__", False) or
+            getattr(testMethod, "__unittest_skip__", False)):
+            # If the class or method was skipped.
+            skip_why = (getattr(self.__class__, '__unittest_skip_why__', '')
+                        or getattr(testMethod, '__unittest_skip_why__', ''))
+            raise SkipTest(skip_why)
+
         self.setUp()
-        getattr(self, self._testMethodName)()
+        testMethod()
         self.tearDown()
         while self._cleanups:
             function, args, kwargs = self._cleanups.pop(-1)
diff --git a/Lib/unittest/test/test_skipping.py b/Lib/unittest/test/test_skipping.py
index c14410afbefdc..7cb9d33f5e5f3 100644
--- a/Lib/unittest/test/test_skipping.py
+++ b/Lib/unittest/test/test_skipping.py
@@ -460,5 +460,71 @@ def test_1(self):
         self.assertIs(suite.run(result), result)
         self.assertEqual(result.skipped, [(test, "")])
 
+    def test_debug_skipping(self):
+        class Foo(unittest.TestCase):
+            def setUp(self):
+                events.append("setUp")
+            def tearDown(self):
+                events.append("tearDown")
+            def test1(self):
+                self.skipTest('skipping exception')
+                events.append("test1")
+            @unittest.skip("skipping decorator")
+            def test2(self):
+                events.append("test2")
+
+        events = []
+        test = Foo("test1")
+        with self.assertRaises(unittest.SkipTest) as cm:
+            test.debug()
+        self.assertIn("skipping exception", str(cm.exception))
+        self.assertEqual(events, ["setUp"])
+
+        events = []
+        test = Foo("test2")
+        with self.assertRaises(unittest.SkipTest) as cm:
+            test.debug()
+        self.assertIn("skipping decorator", str(cm.exception))
+        self.assertEqual(events, [])
+
+    def test_debug_skipping_class(self):
+        @unittest.skip("testing")
+        class Foo(unittest.TestCase):
+            def setUp(self):
+                events.append("setUp")
+            def tearDown(self):
+                events.append("tearDown")
+            def test(self):
+                events.append("test")
+
+        events = []
+        test = Foo("test")
+        with self.assertRaises(unittest.SkipTest) as cm:
+            test.debug()
+        self.assertIn("testing", str(cm.exception))
+        self.assertEqual(events, [])
+
+    def test_debug_skipping_subtests(self):
+        class Foo(unittest.TestCase):
+            def setUp(self):
+                events.append("setUp")
+            def tearDown(self):
+                events.append("tearDown")
+            def test(self):
+                with self.subTest(a=1):
+                    events.append('subtest')
+                    self.skipTest("skip subtest")
+                    events.append('end subtest')
+                events.append('end test')
+
+        events = []
+        result = LoggingResult(events)
+        test = Foo("test")
+        with self.assertRaises(unittest.SkipTest) as cm:
+            test.debug()
+        self.assertIn("skip subtest", str(cm.exception))
+        self.assertEqual(events, ['setUp', 'subtest'])
+
+
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2021-09-18-13-14-57.bpo-36674.a2k5Zb.rst b/Misc/NEWS.d/next/Library/2021-09-18-13-14-57.bpo-36674.a2k5Zb.rst
new file mode 100644
index 0000000000000..bc8c9247b080d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-09-18-13-14-57.bpo-36674.a2k5Zb.rst
@@ -0,0 +1,2 @@
+:meth:`unittest.TestCase.debug` raises now a :class:`unittest.SkipTest` if
+the class or the test method are decorated with the skipping decorator.



More information about the Python-checkins mailing list