]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-34900: Make TestCase.debug() work with subtests (GH-9707)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 12 Oct 2018 11:07:01 +0000 (04:07 -0700)
committerBerker Peksag <berker.peksag@gmail.com>
Fri, 12 Oct 2018 11:07:01 +0000 (14:07 +0300)
(cherry picked from commit da2bf9f66d0c95b988c5d87646d168f65499b316)

Co-authored-by: Bruno Oliveira <nicoddemus@gmail.com>
Lib/unittest/case.py
Lib/unittest/test/test_case.py
Misc/NEWS.d/next/Library/2018-10-05-05-55-53.bpo-34900.8RNiFu.rst [new file with mode: 0644]

index c0170d182573cb3af5bd97bc87f9b846161e3315..758924d80113a980dcba73a6f51cf19986fa708b 100644 (file)
@@ -519,7 +519,7 @@ class TestCase(object):
         case as failed but resumes execution at the end of the enclosed
         block, allowing further test code to be executed.
         """
-        if not self._outcome.result_supports_subtests:
+        if self._outcome is None or not self._outcome.result_supports_subtests:
             yield
             return
         parent = self._subtest
index 6b3439781c9de24b13a6b301def11b96ef270758..6d58201ea8143a039d5cb0d9ca05d5400a78c0aa 100644 (file)
@@ -425,6 +425,20 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
         expected = ['a1', 'a2', 'b1']
         self.assertEqual(events, expected)
 
+    def test_subtests_debug(self):
+        # Test debug() with a test that uses subTest() (bpo-34900)
+        events = []
+
+        class Foo(unittest.TestCase):
+            def test_a(self):
+                events.append('test case')
+                with self.subTest():
+                    events.append('subtest 1')
+
+        Foo('test_a').debug()
+
+        self.assertEqual(events, ['test case', 'subtest 1'])
+
     # "This class attribute gives the exception raised by the test() method.
     # If a test framework needs to use a specialized exception, possibly to
     # carry additional information, it must subclass this exception in
diff --git a/Misc/NEWS.d/next/Library/2018-10-05-05-55-53.bpo-34900.8RNiFu.rst b/Misc/NEWS.d/next/Library/2018-10-05-05-55-53.bpo-34900.8RNiFu.rst
new file mode 100644 (file)
index 0000000..20e3a0e
--- /dev/null
@@ -0,0 +1,2 @@
+Fixed :meth:`unittest.TestCase.debug` when used to call test methods with
+subtests.  Patch by Bruno Oliveira.