]> granicus.if.org Git - python/commitdiff
Issue #22894: TestCase.subTest() would cause the test suite to be stopped when in...
authorAntoine Pitrou <solipsis@pitrou.net>
Sun, 23 Nov 2014 14:55:11 +0000 (15:55 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Sun, 23 Nov 2014 14:55:11 +0000 (15:55 +0100)
Lib/unittest/result.py
Lib/unittest/test/test_case.py
Misc/NEWS

index b967a60a01f7a9e3fabfe7816652a9fb1175fed4..8e0a64322bbe3edae863fe7f04c94a100c373de2 100644 (file)
@@ -121,7 +121,6 @@ class TestResult(object):
         self.failures.append((test, self._exc_info_to_string(err, test)))
         self._mirrorOutput = True
 
-    @failfast
     def addSubTest(self, test, subtest, err):
         """Called at the end of a subtest.
         'err' is None if the subtest ended successfully, otherwise it's a
@@ -130,6 +129,8 @@ class TestResult(object):
         # By default, we don't do anything with successful subtests, but
         # more sophisticated test results might want to record them.
         if err is not None:
+            if getattr(self, 'failfast', False):
+                self.stop()
             if issubclass(err[0], test.failureException):
                 errors = self.failures
             else:
index f0b327dd82677564be0f2dcdf0fcd0a7f6f66d8d..c7ff3b0e3c76bdad60acd59d726e7fa79cc2ffeb 100644 (file)
@@ -397,6 +397,34 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
         Foo(events).run(result)
         self.assertEqual(events, expected)
 
+    def test_subtests_failfast(self):
+        # Ensure proper test flow with subtests and failfast (issue #22894)
+        events = []
+
+        class Foo(unittest.TestCase):
+            def test_a(self):
+                with self.subTest():
+                    events.append('a1')
+                events.append('a2')
+
+            def test_b(self):
+                with self.subTest():
+                    events.append('b1')
+                with self.subTest():
+                    self.fail('failure')
+                events.append('b2')
+
+            def test_c(self):
+                events.append('c')
+
+        result = unittest.TestResult()
+        result.failfast = True
+        suite = unittest.makeSuite(Foo)
+        suite.run(result)
+
+        expected = ['a1', 'a2', 'b1']
+        self.assertEqual(events, expected)
+
     # "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
index 2d131bd9cc27eb935b5abc9161dc692ab9863378..1fedf61157c61536a1fc9b62148748c2ff0ae565 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #22894: TestCase.subTest() would cause the test suite to be stopped
+  when in failfast mode, even in the absence of failures.
+
 - Issue #22638: SSLv3 is now disabled throughout the standard library.
   It can still be enabled by instantiating a SSLContext manually.