diff options
author | Dan Wandschneider <daniel.wandschneider@schrodinger.com> | 2016-11-07 18:24:10 -0800 |
---|---|---|
committer | Dan Wandschneider <daniel.wandschneider@schrodinger.com> | 2016-11-07 18:32:56 -0800 |
commit | e46e653794e3b72d7cc02d374086191224221501 (patch) | |
tree | ace251d52c2070d4b1fa021755cc4ae5eca0a34e /testing | |
parent | 07af307e4ae7670f52569a12993e715b79a97dd4 (diff) | |
download | pytest-e46e653794e3b72d7cc02d374086191224221501.tar.gz |
Clean up unittest TestCase objects after tests are complete (#1649).
Fix #1649
Users of unittest style TestCases will create expensive objects
in setUp. We should clean up TestCase instances that are lying
around so that they don't fill up memory.
Diffstat (limited to 'testing')
-rw-r--r-- | testing/test_unittest.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/testing/test_unittest.py b/testing/test_unittest.py index b6be95fa5..9625ae0f8 100644 --- a/testing/test_unittest.py +++ b/testing/test_unittest.py @@ -1,5 +1,6 @@ from _pytest.main import EXIT_NOTESTSCOLLECTED import pytest +import gc def test_simple_unittest(testdir): testpath = testdir.makepyfile(""" @@ -134,6 +135,28 @@ def test_teardown(testdir): assert passed == 2 assert passed + skipped + failed == 2 +def test_teardown_issue1649(testdir): + """ + Are TestCase objects cleaned up? Often unittest TestCase objects set + attributes that are large and expensive during setUp. + + The TestCase will not be cleaned up if the test fails, because it + would then exist in the stackframe. + """ + testpath = testdir.makepyfile(""" + import unittest + class TestCaseObjectsShouldBeCleanedUp(unittest.TestCase): + def setUp(self): + self.an_expensive_object = 1 + def test_demo(self): + pass + + """) + testdir.inline_run("-s", testpath) + gc.collect() + for obj in gc.get_objects(): + assert type(obj).__name__ != 'TestCaseObjectsShouldBeCleanedUp' + @pytest.mark.skipif("sys.version_info < (2,7)") def test_unittest_skip_issue148(testdir): testpath = testdir.makepyfile(""" |