]> granicus.if.org Git - python/commitdiff
Issue 10611. SystemExit should not cause a unittest test run to exit.
authorMichael Foord <fuzzyman@voidspace.org.uk>
Sun, 19 Dec 2010 14:53:19 +0000 (14:53 +0000)
committerMichael Foord <fuzzyman@voidspace.org.uk>
Sun, 19 Dec 2010 14:53:19 +0000 (14:53 +0000)
Lib/unittest/case.py
Lib/unittest/test/test_case.py
Misc/NEWS

index 3940daa4c4e7cf37a77c27f91da07d9f1c83091c..33ab47a4b59625167f15a75c1bf3d9360c2e8a86 100644 (file)
@@ -311,11 +311,15 @@ class TestCase(object):
                 self.setUp()
             except SkipTest as e:
                 self._addSkip(result, str(e))
-            except Exception:
+            except KeyboardInterrupt:
+                raise
+            except:
                 result.addError(self, sys.exc_info())
             else:
                 try:
                     testMethod()
+                except KeyboardInterrupt:
+                    raise
                 except self.failureException:
                     result.addFailure(self, sys.exc_info())
                 except _ExpectedFailure as e:
@@ -336,14 +340,16 @@ class TestCase(object):
                         result.addFailure(self, sys.exc_info())
                 except SkipTest as e:
                     self._addSkip(result, str(e))
-                except Exception:
+                except:
                     result.addError(self, sys.exc_info())
                 else:
                     success = True
 
                 try:
                     self.tearDown()
-                except Exception:
+                except KeyboardInterrupt:
+                    raise
+                except:
                     result.addError(self, sys.exc_info())
                     success = False
 
@@ -367,7 +373,9 @@ class TestCase(object):
             function, args, kwargs = self._cleanups.pop(-1)
             try:
                 function(*args, **kwargs)
-            except Exception:
+            except KeyboardInterrupt:
+                raise
+            except:
                 ok = False
                 result.addError(self, sys.exc_info())
         return ok
index 4e9dfc69010fade16d63a166c6d5d44dce7537c9..c42d98d82de5833581a02af08b6a8d2378bf1f92 100644 (file)
@@ -999,6 +999,58 @@ test case
         # This shouldn't blow up
         deepcopy(test)
 
+    def testKeyboardInterrupt(self):
+        def _raise(self=None):
+            raise KeyboardInterrupt
+        def nothing(self):
+            pass
+
+        class Test1(unittest.TestCase):
+            test_something = _raise
+
+        class Test2(unittest.TestCase):
+            setUp = _raise
+            test_something = nothing
+
+        class Test3(unittest.TestCase):
+            test_something = nothing
+            tearDown = _raise
+
+        class Test4(unittest.TestCase):
+            def test_something(self):
+                self.addCleanup(_raise)
+
+        for klass in (Test1, Test2, Test3, Test4):
+            with self.assertRaises(KeyboardInterrupt):
+                klass('test_something').run()
+
+    def testSystemExit(self):
+        def _raise(self=None):
+            raise SystemExit
+        def nothing(self):
+            pass
+
+        class Test1(unittest.TestCase):
+            test_something = _raise
+
+        class Test2(unittest.TestCase):
+            setUp = _raise
+            test_something = nothing
+
+        class Test3(unittest.TestCase):
+            test_something = nothing
+            tearDown = _raise
+
+        class Test4(unittest.TestCase):
+            def test_something(self):
+                self.addCleanup(_raise)
+
+        for klass in (Test1, Test2, Test3, Test4):
+            result = unittest.TestResult()
+            klass('test_something').run(result)
+            self.assertEqual(len(result.errors), 1)
+            self.assertEqual(result.testsRun, 1)
+
 
 if __name__ == '__main__':
     unittest.main()
index 19d6c418e8b469be7ecb88987b7e48352eccd3d0..b1b026545de0ea3b6856594ac013f3fc50cb132e 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #10611: SystemExit should not cause a unittest test run to exit.
+
 - Issue #6791: Limit header line length (to 65535 bytes) in http.client,
   to avoid denial of services from the other party.