Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 18f2298

Browse files
committed
Issue #22894: TestCase.subTest() would cause the test suite to be stopped when in failfast mode, even in the absence of failures.
1 parent a21de3d commit 18f2298

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

Lib/unittest/result.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ def addFailure(self, test, err):
121121
self.failures.append((test, self._exc_info_to_string(err, test)))
122122
self._mirrorOutput = True
123123

124-
@failfast
125124
def addSubTest(self, test, subtest, err):
126125
"""Called at the end of a subtest.
127126
'err' is None if the subtest ended successfully, otherwise it's a
@@ -130,6 +129,8 @@ def addSubTest(self, test, subtest, err):
130129
# By default, we don't do anything with successful subtests, but
131130
# more sophisticated test results might want to record them.
132131
if err is not None:
132+
if getattr(self, 'failfast', False):
133+
self.stop()
133134
if issubclass(err[0], test.failureException):
134135
errors = self.failures
135136
else:

Lib/unittest/test/test_case.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,34 @@ def test(self):
397397
Foo(events).run(result)
398398
self.assertEqual(events, expected)
399399

400+
def test_subtests_failfast(self):
401+
# Ensure proper test flow with subtests and failfast (issue #22894)
402+
events = []
403+
404+
class Foo(unittest.TestCase):
405+
def test_a(self):
406+
with self.subTest():
407+
events.append('a1')
408+
events.append('a2')
409+
410+
def test_b(self):
411+
with self.subTest():
412+
events.append('b1')
413+
with self.subTest():
414+
self.fail('failure')
415+
events.append('b2')
416+
417+
def test_c(self):
418+
events.append('c')
419+
420+
result = unittest.TestResult()
421+
result.failfast = True
422+
suite = unittest.makeSuite(Foo)
423+
suite.run(result)
424+
425+
expected = ['a1', 'a2', 'b1']
426+
self.assertEqual(events, expected)
427+
400428
# "This class attribute gives the exception raised by the test() method.
401429
# If a test framework needs to use a specialized exception, possibly to
402430
# carry additional information, it must subclass this exception in

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ Core and Builtins
3636
Library
3737
-------
3838

39+
- Issue #22894: TestCase.subTest() would cause the test suite to be stopped
40+
when in failfast mode, even in the absence of failures.
41+
3942
- Issue #22638: SSLv3 is now disabled throughout the standard library.
4043
It can still be enabled by instantiating a SSLContext manually.
4144

0 commit comments

Comments
 (0)