]> granicus.if.org Git - python/commitdiff
Fix issue 9926. TestSuite subclasses that override __call__ are called correctly.
authorMichael Foord <fuzzyman@voidspace.org.uk>
Mon, 1 Nov 2010 21:09:03 +0000 (21:09 +0000)
committerMichael Foord <fuzzyman@voidspace.org.uk>
Mon, 1 Nov 2010 21:09:03 +0000 (21:09 +0000)
Lib/unittest/result.py
Lib/unittest/suite.py
Lib/unittest/test/test_suite.py

index 1dbd04cf752038920f47f4c9e5c9180c7e6548c9..3dc7154b732b45c495c1f5746d0652ee6d7d7d34 100644 (file)
@@ -34,6 +34,7 @@ class TestResult(object):
     formatted traceback of the error that occurred.
     """
     _previousTestClass = None
+    _testRunEntered = False
     _moduleSetUpFailed = False
     def __init__(self, stream=None, descriptions=None, verbosity=None):
         self.failfast = False
index b6ae68c31a5a5a2cd4d5c1b964a174878ee0e2ac..73f0e17f8a661f9c55ab3675e12c18eab24d0de1 100644 (file)
@@ -77,23 +77,11 @@ class TestSuite(BaseTestSuite):
     subclassing, do not forget to call the base class constructor.
     """
 
+    def run(self, result, debug=False):
+        topLevel = False
+        if getattr(result, '_testRunEntered', False) is False:
+            result._testRunEntered = topLevel = True
 
-    def run(self, result):
-        self._wrapped_run(result)
-        self._tearDownPreviousClass(None, result)
-        self._handleModuleTearDown(result)
-        return result
-
-    def debug(self):
-        """Run the tests without collecting errors in a TestResult"""
-        debug = _DebugResult()
-        self._wrapped_run(debug, True)
-        self._tearDownPreviousClass(None, debug)
-        self._handleModuleTearDown(debug)
-
-    ################################
-    # private methods
-    def _wrapped_run(self, result, debug=False):
         for test in self:
             if result.shouldStop:
                 break
@@ -108,13 +96,23 @@ class TestSuite(BaseTestSuite):
                     getattr(result, '_moduleSetUpFailed', False)):
                     continue
 
-            if hasattr(test, '_wrapped_run'):
-                test._wrapped_run(result, debug)
-            elif not debug:
+            if not debug:
                 test(result)
             else:
                 test.debug()
 
+        if topLevel:
+            self._tearDownPreviousClass(None, result)
+            self._handleModuleTearDown(result)
+        return result
+
+    def debug(self):
+        """Run the tests without collecting errors in a TestResult"""
+        debug = _DebugResult()
+        self.run(debug, True)
+
+    ################################
+
     def _handleClassSetUp(self, test, result):
         previousClass = getattr(result, '_previousTestClass', None)
         currentClass = test.__class__
index 47b57de3b74dcbae32ef8f4bc790ea555979d850..fa32247013f39dacf2728e3ee97463d9c768121a 100644 (file)
@@ -345,5 +345,19 @@ class Test_TestSuite(unittest.TestCase, TestEquality):
         self.assertEqual(result.testsRun, 2)
 
 
+    def test_overriding_call(self):
+        class MySuite(unittest.TestSuite):
+            called = False
+            def __call__(self, *args, **kw):
+                self.called = True
+                unittest.TestSuite.__call__(self, *args, **kw)
+
+        suite = MySuite()
+        wrapper = unittest.TestSuite()
+        wrapper.addTest(suite)
+        wrapper(unittest.TestResult())
+        self.assertTrue(suite.called)
+
+
 if __name__ == '__main__':
     unittest.main()