diff options
author | Andrew Svetlov <andrew.svetlov@gmail.com> | 2013-08-28 18:28:38 (GMT) |
---|---|---|
committer | Andrew Svetlov <andrew.svetlov@gmail.com> | 2013-08-28 18:28:38 (GMT) |
commit | eb9736845182a732b2fcfb32730984d7c6fff7f3 (patch) | |
tree | 8f613d582fd60002494fa0f8e4173d730c444abc /Lib/unittest/test | |
parent | 6a53af893ad2708af8dda71515254abd7118d7de (diff) | |
download | cpython-eb9736845182a732b2fcfb32730984d7c6fff7f3.zip cpython-eb9736845182a732b2fcfb32730984d7c6fff7f3.tar.gz cpython-eb9736845182a732b2fcfb32730984d7c6fff7f3.tar.bz2 |
Issue #11798: TestSuite now drops references to own tests after execution.
Diffstat (limited to 'Lib/unittest/test')
-rw-r--r-- | Lib/unittest/test/test_setups.py | 6 | ||||
-rw-r--r-- | Lib/unittest/test/test_suite.py | 42 |
2 files changed, 43 insertions, 5 deletions
diff --git a/Lib/unittest/test/test_setups.py b/Lib/unittest/test/test_setups.py index b8d5aa4..bcd69a8 100644 --- a/Lib/unittest/test/test_setups.py +++ b/Lib/unittest/test/test_setups.py @@ -494,12 +494,10 @@ class TestSetups(unittest.TestCase): 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() diff --git a/Lib/unittest/test/test_suite.py b/Lib/unittest/test/test_suite.py index 2db978d..1ad9b56 100644 --- a/Lib/unittest/test/test_suite.py +++ b/Lib/unittest/test/test_suite.py @@ -1,6 +1,8 @@ import unittest +import gc import sys +import weakref from .support import LoggingResult, TestEquality @@ -300,7 +302,46 @@ class Test_TestSuite(unittest.TestCase, 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([1, None, 3], suite._tests) + + 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): @@ -363,6 +404,5 @@ class Test_TestSuite(unittest.TestCase, TestEquality): self.assertFalse(result._testRunEntered) - if __name__ == '__main__': unittest.main() |