Skip to content

Commit c1fe49c

Browse files
miss-islingtonnicoddemus
authored andcommitted
bpo-34900: Make TestCase.debug() work with subtests (GH-9707)
(cherry picked from commit da2bf9f) Co-authored-by: Bruno Oliveira <[email protected]>
1 parent d918e98 commit c1fe49c

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

Lib/unittest/case.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def subTest(self, msg=_subtest_msg_sentinel, **params):
509509
case as failed but resumes execution at the end of the enclosed
510510
block, allowing further test code to be executed.
511511
"""
512-
if not self._outcome.result_supports_subtests:
512+
if self._outcome is None or not self._outcome.result_supports_subtests:
513513
yield
514514
return
515515
parent = self._subtest

Lib/unittest/test/test_case.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,20 @@ def test_c(self):
425425
expected = ['a1', 'a2', 'b1']
426426
self.assertEqual(events, expected)
427427

428+
def test_subtests_debug(self):
429+
# Test debug() with a test that uses subTest() (bpo-34900)
430+
events = []
431+
432+
class Foo(unittest.TestCase):
433+
def test_a(self):
434+
events.append('test case')
435+
with self.subTest():
436+
events.append('subtest 1')
437+
438+
Foo('test_a').debug()
439+
440+
self.assertEqual(events, ['test case', 'subtest 1'])
441+
428442
# "This class attribute gives the exception raised by the test() method.
429443
# If a test framework needs to use a specialized exception, possibly to
430444
# carry additional information, it must subclass this exception in
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed :meth:`unittest.TestCase.debug` when used to call test methods with
2+
subtests. Patch by Bruno Oliveira.

0 commit comments

Comments
 (0)