Skip to content

Commit

Permalink
Merge pull request #2046 from d-b-w/clean-up-unittest-issue1649
Browse files Browse the repository at this point in the history
Clean up unittest TestCase objects after tests are complete (#1649).
  • Loading branch information
nicoddemus authored Nov 8, 2016
2 parents 07af307 + e46e653 commit 0b94c43
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Christopher Gilling
Daniel Grana
Daniel Hahler
Daniel Nuri
Daniel Wandschneider
Danielle Jenkins
Dave Hunt
David Díaz-Barquero
Expand Down
5 changes: 4 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
* Properly handle exceptions in ``multiprocessing`` tasks (`#1984`_).
Thanks `@adborden`_ for the report and `@nicoddemus`_ for the PR.

*
* Clean up unittest TestCase objects after tests are complete (`#1649`_).
Thanks `@d_b_w`_ for the report and PR.

*

Expand All @@ -38,13 +39,15 @@
.. _@okulynyak: https://github.com/okulynyak
.. _@matclab: https://github.com/matclab
.. _@gdyuldin: https://github.com/gdyuldin
.. _@d_b_w: https://github.com/d_b_w

.. _#442: https://github.com/pytest-dev/pytest/issues/442
.. _#1976: https://github.com/pytest-dev/pytest/issues/1976
.. _#1984: https://github.com/pytest-dev/pytest/issues/1984
.. _#1998: https://github.com/pytest-dev/pytest/issues/1998
.. _#2004: https://github.com/pytest-dev/pytest/issues/2004
.. _#2005: https://github.com/pytest-dev/pytest/issues/2005
.. _#1649: https://github.com/pytest-dev/pytest/issues/1649


3.0.3
Expand Down
3 changes: 3 additions & 0 deletions _pytest/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def _fix_unittest_skip_decorator(self):
def teardown(self):
if hasattr(self._testcase, 'teardown_method'):
self._testcase.teardown_method(self._obj)
# Allow garbage collection on TestCase instance attributes.
self._testcase = None
self._obj = None

def startTest(self, testcase):
pass
Expand Down
23 changes: 23 additions & 0 deletions testing/test_unittest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from _pytest.main import EXIT_NOTESTSCOLLECTED
import pytest
import gc

def test_simple_unittest(testdir):
testpath = testdir.makepyfile("""
Expand Down Expand Up @@ -134,6 +135,28 @@ def test_check(self):
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("""
Expand Down

0 comments on commit 0b94c43

Please sign in to comment.