Matt McClure avatar Matt McClure committed bf43956

[11798] Apply patch by tomwardill, 2012-09-30 10:12

http://bugs.python.org/issue11798

Comments (0)

Files changed (3)

Lib/unittest/suite.py

             self.addTest(test)
 
     def run(self, result):
-        for test in self:
+        for index, test in enumerate(self):
             if result.shouldStop:
                 break
             test(result)
+            self._removeTestAtIndex(index)
         return result
+        
+    def _removeTestAtIndex(self, index):
+        try:
+            self._tests[index] = None
+        except TypeError:
+            # support for suite implementations that have overriden self._test
+            pass
 
     def __call__(self, *args, **kwds):
         return self.run(*args, **kwds)
         if getattr(result, '_testRunEntered', False) is False:
             result._testRunEntered = topLevel = True
 
-        for test in self:
+        for index, test in enumerate(self):
             if result.shouldStop:
                 break
 
                 test(result)
             else:
                 test.debug()
+                
+            self._removeTestAtIndex(index)
 
         if topLevel:
             self._tearDownPreviousClass(None, result)

Lib/unittest/test/test_setups.py

         Test.__module__ = 'Module'
         sys.modules['Module'] = Module
 
-        _suite = unittest.defaultTestLoader.loadTestsFromTestCase(Test)
-        suite = unittest.TestSuite()
-        suite.addTest(_suite)
-
         messages = ('setUpModule', 'tearDownModule', 'setUpClass', 'tearDownClass', 'test_something')
         for phase, msg in enumerate(messages):
+            _suite = unittest.defaultTestLoader.loadTestsFromTestCase(Test)
+            suite = unittest.TestSuite([_suite])
             with self.assertRaisesRegex(Exception, msg):
                 suite.debug()
 

Lib/unittest/test/test_suite.py

 import unittest
 
+import gc
 import sys
+import weakref
 from .support import LoggingResult, TestEquality
 
 
         # when the bug is fixed this line will not crash
         suite.run(unittest.TestResult())
 
+    def test_remove_test_at_index(self):
+        suite = unittest.TestSuite()
+        
+        suite._tests = [1, 2, 3]
+        suite._removeTestAtIndex(1)
+        
+        self.assertEqual(suite._tests[1], None)
 
+    def test_remove_test_at_index_not_indexable(self):
+        suite = unittest.TestSuite()      
+        suite._tests = None
+        
+        # if _removeAtIndex raises for noniterables this next line will break
+        suite._removeTestAtIndex(2)
+        
+    def assert_garbage_collect_test_after_run(self, TestSuiteClass):
+        
+        class Foo(unittest.TestCase):
+            def test_nothing(self):
+                pass
+        
+        test = Foo('test_nothing')
+        wref = weakref.ref(test)       
+        
+        suite = TestSuiteClass([wref()])
+        suite.run(unittest.TestResult())
+        
+        del test
+        
+        # for the benefit of non-reference counting implementations
+        gc.collect()
+        
+        self.assertEqual(suite._tests, [None])
+        self.assertIsNone(wref())
+    
+    def test_garbage_collect_test_after_run_BaseTestSuite(self):
+        
+        self.assert_garbage_collect_test_after_run(unittest.BaseTestSuite)
+        
+    def test_garbage_collect_test_after_run_TestSuite(self):
+        
+        self.assert_garbage_collect_test_after_run(unittest.TestSuite)
+        
 
     def test_basetestsuite(self):
         class Test(unittest.TestCase):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.