Clean up unittest TestCase objects after tests are complete (#1649).#2046
Conversation
…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.
|
Looking at http://stackoverflow.com/questions/40478600/mem-leak-in-pyqt-test-with-pytest should we do the same with other test classes too? |
|
Oh, yeah, almost certainly. I didn't think people would do add attributes to the test instances for pytest style test cases because they could just use fixtures. I'll put together a second PR for non-unittest cases. |
|
@d-b-w, I did try out your branch with this code: import unittest
class ExpensiveObject(object):
def __del__(self):
print('ExpensiveObject deleted')
class TestCaseObjectsShouldBeCleanedUp(unittest.TestCase):
def setUp(self):
print('Created')
self.an_expensive_object = ExpensiveObject()
def test_demo(self):
pass
def test_demo2(self):
pass On master: On your branch: So it seems to work just fine, thanks for this. 😁 @The-Compiler, you got a point: class ExpensiveObject(object):
def __del__(self):
print('ExpensiveObject deleted')
class TestCaseObjectsShouldBeCleanedUp(object):
def setup_method(self, m):
print('Created')
self.an_expensive_object = ExpensiveObject()
def test_demo(self):
pass
def test_demo2(self):
pass So we should probably do something similar for normal test instances. @d-b-w would be willing to work on that as well? Otherwise I would rather we merge this as it is because it is already an improvement and create a separate issue. |
Oh sorry I didn't see your reply before I posted mine. I will merge this as it is then if you prefer a separate PR. Thanks again! |
|
Heh - I'm accustomed to making my commits as small as they can be and still be functional. I suppose that for contributions to pytest, PRs should resolve complete issues/ or be complete lines on the release notes :) |
|
No problem at all having two separate PRs for this. I went ahead and merged because I don't know when you can find time to also work on the issue of the non-unittest cases so I thought best to get this into |
|
I'm having some trouble with the fix for non-unittest tests - when I add a teardown to the Instance class, the nose generator tests fail (from python.py): The failure message:
I think this means that the setup wasn't performed for some of the tests? |
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.
Here's a quick checklist that should be present in PRs:
master; for new features, targetfeatures; (master)test_teardown_issue1649(), fails before and passes after)AUTHORS; (as Daniel Wandschneider)CHANGELOG.rst(Added first line of this description)