Repository navigation
Closed
Description
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = 'https://github.com/voidspace'
closed_at = <Date 2014-09-21.08:15:26.276>
created_at = <Date 2011-04-07.16:15:56.969>
labels = ['type-bug', 'library']
title = 'Test cases not garbage collected after run'
updated_at = <Date 2017-06-30.16:52:46.736>
user = 'https://github.com/fabioz'bugs.python.org fields:
activity = <Date 2017-06-30.16:52:46.736>
actor = 'gvanrossum'
assignee = 'michael.foord'
closed = True
closed_date = <Date 2014-09-21.08:15:26.276>
closer = 'berker.peksag'
components = ['Library (Lib)']
creation = <Date 2011-04-07.16:15:56.969>
creator = 'fabioz'
dependencies = []
files = ['27352', '31154', '31483', '33131']
hgrepos = []
issue_num = 11798
keywords = ['patch']
message_count = 68.0
messages = ['133225', '133226', '133227', '133228', '133229', '133231', '133232', '133236', '133237', '133239', '133241', '136961', '136967', '137464', '171623', '171837', '188424', '194228', '194268', '194398', '194430', '194432', '196273', '196275', '196281', '196282', '196297', '196300', '196301', '196402', '196404', '196406', '196431', '196433', '196607', '196618', '196659', '196665', '196701', '196702', '196703', '196705', '196742', '196766', '197712', '197713', '197714', '197734', '197735', '197888', '198060', '198062', '198066', '198068', '198069', '198071', '198072', '198076', '198080', '206177', '207046', '207047', '207057', '207070', '207081', '297374', '297384', '297388']
nosy_count = 18.0
nosy_names = ['tim.peters', 'rhettinger', 'terry.reedy', 'vstinner', 'fabioz', 'benjamin.peterson', 'ezio.melotti', 'r.david.murray', 'michael.foord', 'matthewlmcclure', 'asvetlov', 'meador.inge', 'xdegaye', 'tshepang', 'python-dev', 'tomwardill', 'matthewlmcclure-gmail', 'charettes']
pr_nums = ['2502', '2505', '2506']
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue11798'
versions = ['Python 3.4']
Activity
fabioz commentedon Apr 7, 2011
Right now, when doing a test case, one must clear all the variables created in the test class, and I believe this shouldn't be needed...
E.g.:
...
Ideally (in my view), right after running the test, it should be garbage-collected and the explicit tearDown wouldn't be needed (as the test would garbage-collected, that reference would automatically die), because this is currently very error prone... (and probably a source of leaks for any sufficiently big test suite).
If that's accepted, I can provide a patch.
benjaminp commentedon Apr 7, 2011
You don't have to clear them; you just have to finalize them. Anyway, this is essentially impossible to do in a backward compatible way given that TestCases are expected to stay around.
exarkun commentedon Apr 7, 2011
Trial lets test cases get garbaged collected. When we noticed this wasn't happening, we treated it as a bug and fixed it. No one ever complained about the change. I don't see any obvious way in which an application would even be able to tell the difference (a user can tell the difference by looking at top). In what case do you think this change would result in broken application code?
fabioz commentedon Apr 7, 2011
I do get the idea of the backward incompatibility, although I think it's really minor in this case.
Just for some data, the pydev test runner has had a fix to clear those test cases for quite a while already and no one has complained about it (it actually makes each of the tests None after run, so, if someone tries to access it after that, it would be pretty clear that it's not there anymore).
benjaminp commentedon Apr 7, 2011
2011/4/7 Jean-Paul Calderone <report@bugs.python.org>:
I thought unittest was just handed a bunch of TestCase instances and
couldn't do much about insuring they were garbage collected.
exarkun commentedon Apr 7, 2011
True. But unittest could ensure that it doesn't keep a reference to each TestCase instance after it finishes running it. Then, if no one else has a reference either, it can be garbage collected.
voidspace commentedon Apr 7, 2011
A TestSuite (which is how tests are collected to run) holds all the tests and therefore keeps them all alive for the duration of the test run. (I presume this is the issue anyway.)
How would you solve this - by having calling a TestSuite (which is how a test run is executed) remove members from themselves after each test execution? (Any failure tracebacks etc stored by the TestResult would also have to not keep the test alive.)
My only concern would be backwards compatibility due to the change in behaviour.
fabioz commentedon Apr 7, 2011
The current code I use in PyDev is below -- another option could be not adding the None to the list of tests, but removing it, but I find that a bit worse because in the current way if someone tries to access it after it's ran, it'll become clear it was removed.
exarkun commentedon Apr 7, 2011
Here's Trial's implementation: http://twistedmatrix.com/trac/browser/trunk/twisted/trial/runner.py#L138
voidspace commentedon Apr 7, 2011
Not keeping tests alive for the whole run seems like a good thing and either implementation seems fine to me. I'd be interested to hear if anyone else had any backwards compatibility concerns though.
rhettinger commentedon Apr 7, 2011
+1
I slightly prefer Fabio;s assignment to None approach (for subtle reasons that I can't articulate at the moment).
fabioz commentedon May 26, 2011
So Michal, it seems no objections were raised?
47 remaining items
voidspace commentedon Sep 19, 2013
Ah right, my mistake. Before setUp there shouldn't be test state. (Although tests are free to do whatever they want in __init__ too and I've seen plenty of TestCase subclasses using __init__ when they should be using setUp.)
Essentially though _cleanup is a backwards compatibility feature - and suites that need _cleanup as a public api are already living without testcase dict cleanup.
pitrou commentedon Sep 19, 2013
That said, I agree that the __dict__ proposal is a hack, but as is the current _removetestAtIndex mechanism.
The only clean solution I can think of would be to have two separate classes:
Maybe it's possible to do this without any backwards compat problem by making TestSuite.__iter__ always return TestCases (but freshly-created ones, from the inner test specs). The main point of adaptation would be TestLoader.loadTestsFromTestCase().
voidspace commentedon Sep 19, 2013
Having TestLoader.loadTestsFromTestCase() return a "lazy suite" that defers testcase instantiation until iteration is a nice idea.
Unfortunately the TestSuite.addTests api iterates over a suite to add new tests. i.e. the code that builds a TestSuite for module probably already iterates over the suites returned by TestLoader.loadTestsFromTestCase - so the change would need to be more pervasive.
pitrou commentedon Sep 19, 2013
addTests() could easily be tweaked to recognize that it gets passed a
TestSuite, and special-case that.
Also, TestCase objects could probably get an optional "spec" attribute
pointing to their TestSpec.
xdegaye commentedon Dec 14, 2013
This seems to break BaseTestSuite.countTestCases when invoked after the TestSuite has been run:
...
File "Lib/unittest/suite.py", line 42, in countTestCases
cases += test.countTestCases()
AttributeError: 'NoneType' object has no attribute 'countTestCases'
Attached patch attempts to fix it.
pitrou commentedon Dec 28, 2013
No answer to Xavier's regression? The way this issue is being treated is a bit worrying.
python-dev commentedon Dec 28, 2013
New changeset b668c409c10a by Antoine Pitrou in branch 'default':
Fix breakage in TestSuite.countTestCases() introduced by issue bpo-11798.
http://hg.python.org/cpython/rev/b668c409c10a
voidspace commentedon Dec 28, 2013
What's the purpose of _removed_tests in your fix, it doesn't appear to be used?
pitrou commentedon Dec 29, 2013
It is used, see countTestCases().
voidspace commentedon Dec 29, 2013
Ah yes, I see - sorry.
vstinner commentedon Jun 30, 2017
New changeset e4f9a2d by Victor Stinner in branch 'master':
bpo-30813: Fix unittest when hunting refleaks (bpo-2502)
e4f9a2d
vstinner commentedon Jun 30, 2017
New changeset 714afcc by Victor Stinner in branch '3.5':
bpo-30813: Fix unittest when hunting refleaks (bpo-2502) (bpo-2506)
714afcc
vstinner commentedon Jun 30, 2017
New changeset 22d4e8f by Victor Stinner in branch '3.6':
bpo-30813: Fix unittest when hunting refleaks (bpo-2502) (bpo-2505)
22d4e8f