Skip to content

Commit

Permalink
clean up test state before the garbage collection to properly report …
Browse files Browse the repository at this point in the history
…cyclic garbage referenced by the test
  • Loading branch information
d-maurer committed May 11, 2022
1 parent 132c98a commit 52b9754
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/zope/testrunner/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ def _restoreStdStreams(self):
return None, None

def startTest(self, test):
self._test_state = test.__dict__.copy()
self.testSetUp()
unittest.TestResult.startTest(self, test)
testsRun = self.testsRun - 1 # subtract the one the base class added
Expand Down Expand Up @@ -1027,6 +1028,11 @@ def addUnexpectedSuccess(self, test):

def stopTest(self, test):
self.testTearDown()
# Without clearing, cyclic garbage referenced by the test
# would be reported in the following test.
test.__dict__.clear()
test.__dict__.update(self._test_state)
del self._test_state
gccount = gc.collect() \
if self.options.gc_after_test and not is_jython \
else 0
Expand Down Expand Up @@ -1100,7 +1106,10 @@ def _gather(layer):
key.append(layer)

_gather(layer)
return tuple(name_from_layer(ly) for ly in key if ly != UnitTests)
try:
return tuple(name_from_layer(ly) for ly in key if ly != UnitTests)
finally:
del _gather # break reference cycle


def order_by_bases(layers):
Expand Down
3 changes: 3 additions & 0 deletions src/zope/testrunner/tests/testrunner-ex/gc-after-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def test_cycle_without_resource(self):
def test_cycle_with_resource(self):
self.cycle = _Cycle(resource=_Resource())

def test_test_holds_cycle(self):
self.hold_cycle = _Cycle(resource=_Resource())

def test_failure(self):
raise AssertionError("failure")

Expand Down
9 changes: 5 additions & 4 deletions src/zope/testrunner/tests/testrunner-gc-after-test.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ therefore does not show the warnings (even though two are issued).
...
AssertionError: failure
<BLANKLINE>
.
Ran 5 tests with 1 failures, 1 errors and 0 skipped in N.NNN seconds.
..!
Ran 6 tests with 1 failures, 1 errors and 0 skipped in N.NNN seconds.
Tearing down left over layers:
Tear down zope.testrunner.layer.UnitTests in N.NNN seconds.
<BLANKLINE>
Expand All @@ -57,7 +57,7 @@ therefore does not show the warnings (even though two are issued).
Running zope.testrunner.layer.UnitTests tests:
Set up zope.testrunner.layer.UnitTests in N.NNN seconds.
Running:
test_cycle_with_resource (gc-after-test.GcAfterTestTests) [10]
test_cycle_with_resource (gc-after-test.GcAfterTestTests) [3]
<BLANKLINE>
test_cycle_without_resource (gc-after-test.GcAfterTestTests) [2]
<BLANKLINE>
Expand All @@ -78,7 +78,8 @@ therefore does not show the warnings (even though two are issued).
<BLANKLINE>
<BLANKLINE>
test_okay (gc-after-test.GcAfterTestTests)
Ran 5 tests with 1 failures, 1 errors and 0 skipped in N.NNN seconds.
test_test_holds_cycle (gc-after-test.GcAfterTestTests) [3]
Ran 6 tests with 1 failures, 1 errors and 0 skipped in N.NNN seconds.
Tearing down left over layers:
Tear down zope.testrunner.layer.UnitTests in N.NNN seconds.
<BLANKLINE>
Expand Down

0 comments on commit 52b9754

Please sign in to comment.