[Python-checkins] r86101 - in python/branches/py3k/Lib/unittest: result.py suite.py test/test_suite.py

michael.foord python-checkins at python.org
Mon Nov 1 22:09:03 CET 2010


Author: michael.foord
Date: Mon Nov  1 22:09:03 2010
New Revision: 86101

Log:
Fix issue 9926. TestSuite subclasses that override __call__ are called correctly.

Modified:
   python/branches/py3k/Lib/unittest/result.py
   python/branches/py3k/Lib/unittest/suite.py
   python/branches/py3k/Lib/unittest/test/test_suite.py

Modified: python/branches/py3k/Lib/unittest/result.py
==============================================================================
--- python/branches/py3k/Lib/unittest/result.py	(original)
+++ python/branches/py3k/Lib/unittest/result.py	Mon Nov  1 22:09:03 2010
@@ -34,6 +34,7 @@
     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

Modified: python/branches/py3k/Lib/unittest/suite.py
==============================================================================
--- python/branches/py3k/Lib/unittest/suite.py	(original)
+++ python/branches/py3k/Lib/unittest/suite.py	Mon Nov  1 22:09:03 2010
@@ -77,23 +77,11 @@
     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 @@
                     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__

Modified: python/branches/py3k/Lib/unittest/test/test_suite.py
==============================================================================
--- python/branches/py3k/Lib/unittest/test/test_suite.py	(original)
+++ python/branches/py3k/Lib/unittest/test/test_suite.py	Mon Nov  1 22:09:03 2010
@@ -345,5 +345,19 @@
         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()


More information about the Python-checkins mailing list