Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unittest instance objects exist for the lifetime of the py.test run #1649

Closed
d-b-w opened this issue Jun 22, 2016 · 4 comments · Fixed by #2046
Closed

unittest instance objects exist for the lifetime of the py.test run #1649

d-b-w opened this issue Jun 22, 2016 · 4 comments · Fixed by #2046

Comments

@d-b-w
Copy link

d-b-w commented Jun 22, 2016

unittest.TestCase instance objects are never cleaned up - the py.test unittest module retains references to them for the whole of the py.test run. In my case, this can cause problems because the unittest.TestCase instance objects can have large objects associated with them. I believe that people writing unittest-based tests expect that their objects will be cleaned up after test execution.

I've seen this on Linux/Darwin/Windows
pytest==2.8.7
pytest-cov==2.2.1
pytest-faulthandler==1.3.0
pytest-xdist==1.14

Example:

Given this test:

import unittest

class Demo(unittest.TestCase):
    def setUp(self):
        self.large_object = list(range(10**6))
    def testDemo(self):
        assert len(self.large_object) == 10**6

The memory allocated for larg_object will be in use for the whole py.test run, even after it is done with this test.

@d-b-w
Copy link
Author

d-b-w commented Jun 22, 2016

I believe that it would be sufficient to set the TestCaseFunction instance's _testcase and _obj attributes to None after all cleanup is complete.

@d-b-w
Copy link
Author

d-b-w commented Jun 22, 2016

@RonnyPfannschmidt
Copy link
Member

Feel like trying to do a PR?

@d-b-w
Copy link
Author

d-b-w commented Jun 24, 2016

Yeah, I'd like to. I have some testing to do with my idea, though - it seems to cause some problems with our test set.

d-b-w pushed a commit to d-b-w/pytest that referenced this issue Nov 8, 2016
…ev#1649).

Fix pytest-dev#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.
nicoddemus added a commit that referenced this issue Nov 8, 2016
Clean up unittest TestCase objects after tests are complete (#1649).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants