[Python-checkins] bpo-34900: Make TestCase.debug() work with subtests (GH-9707)

Berker Peksag webhook-mailer at python.org
Fri Oct 12 07:07:48 EDT 2018

commit: c1fe49c01f3850aaa32a7d75e47f90eb5c5f7efe
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Berker Peksag <berker.peksag at gmail.com>
date: 2018-10-12T14:07:44+03:00

bpo-34900: Make TestCase.debug() work with subtests (GH-9707)

(cherry picked from commit da2bf9f66d0c95b988c5d87646d168f65499b316)

Co-authored-by: Bruno Oliveira <nicoddemus at gmail.com>

A Misc/NEWS.d/next/Library/2018-10-05-05-55-53.bpo-34900.8RNiFu.rst
M Lib/unittest/case.py
M Lib/unittest/test/test_case.py

diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index c3634ec997f8..402fa47f285a 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -509,7 +509,7 @@ def subTest(self, msg=_subtest_msg_sentinel, **params):
         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:
         parent = self._subtest
diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py
index b84959150542..04da9d0cec7b 100644
--- a/Lib/unittest/test/test_case.py
+++ b/Lib/unittest/test/test_case.py
@@ -425,6 +425,20 @@ def test_c(self):
         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
index 000000000000..20e3a0e2d5a1
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-10-05-05-55-53.bpo-34900.8RNiFu.rst
@@ -0,0 +1,2 @@
+Fixed :meth:`unittest.TestCase.debug` when used to call test methods with
+subtests.  Patch by Bruno Oliveira.

More information about the Python-checkins mailing list