-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
unittest: fix class instances no longer released on test teardown since pytest 8.2.0 #12368
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix a regression in pytest 8.2.0 where unittest class instances (a fresh one is created for each test) were not released promptly on test teardown but only on session teardown. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| # mypy: allow-untyped-defs | ||
| import gc | ||
| import sys | ||
| from typing import List | ||
|
|
||
|
|
@@ -192,30 +191,35 @@ def test_check(self): | |
| def test_teardown_issue1649(pytester: Pytester) -> None: | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way this test was written previously wasn't good; it checked if the obj is still alive after the pytester run, at which the entire session was torn down, too late. This fixes it up. I made sure it now fails before and passes after. |
||
| """ | ||
| Are TestCase objects cleaned up? Often unittest TestCase objects set | ||
| attributes that are large and expensive during setUp. | ||
| attributes that are large and expensive during test run or setUp. | ||
|
|
||
| The TestCase will not be cleaned up if the test fails, because it | ||
| would then exist in the stackframe. | ||
|
|
||
| Regression test for #1649 (see also #12367). | ||
| """ | ||
| testpath = pytester.makepyfile( | ||
| pytester.makepyfile( | ||
| """ | ||
| import unittest | ||
| class TestCaseObjectsShouldBeCleanedUp(unittest.TestCase): | ||
| def setUp(self): | ||
| self.an_expensive_object = 1 | ||
| def test_demo(self): | ||
| pass | ||
| import gc | ||
|
|
||
| """ | ||
| class TestCaseObjectsShouldBeCleanedUp(unittest.TestCase): | ||
| def test_expensive(self): | ||
| self.an_expensive_obj = object() | ||
|
|
||
| def test_is_it_still_alive(self): | ||
| gc.collect() | ||
| for obj in gc.get_objects(): | ||
| if type(obj).__name__ == "TestCaseObjectsShouldBeCleanedUp": | ||
| assert not hasattr(obj, "an_expensive_obj") | ||
|
bluetech marked this conversation as resolved.
|
||
| break | ||
| else: | ||
| assert False, "Could not find TestCaseObjectsShouldBeCleanedUp instance" | ||
| """ | ||
| ) | ||
|
|
||
| pytester.inline_run("-s", testpath) | ||
| gc.collect() | ||
|
|
||
| # Either already destroyed, or didn't run setUp. | ||
| for obj in gc.get_objects(): | ||
| if type(obj).__name__ == "TestCaseObjectsShouldBeCleanedUp": | ||
| assert not hasattr(obj, "an_expensive_obj") | ||
| result = pytester.runpytest() | ||
| assert result.ret == ExitCode.OK | ||
|
|
||
|
|
||
| def test_unittest_skip_issue148(pytester: Pytester) -> None: | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change isn't really related and doesn't have any effect because the super call doesn't do anything, but nonetheless teardown must be done in reverse order, so I fixed it just in case the super ever starts doing something.