-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fix cleanup functions not being invoked on test failures #7151
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
nicoddemus
merged 2 commits into
pytest-dev:master
from
nicoddemus:unittest-cleanup-6947
May 2, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 regression where functions registered with ``TestCase.addCleanup`` were not being called on test failures. |
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
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 |
---|---|---|
|
@@ -537,28 +537,24 @@ def f(_): | |
) | ||
result.stdout.fnmatch_lines( | ||
[ | ||
"test_trial_error.py::TC::test_four SKIPPED", | ||
"test_trial_error.py::TC::test_four FAILED", | ||
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. This reverts the test to the state before 04f27d4, which introduced the breaking change about addCleanup not being called properly for failed tests. |
||
"test_trial_error.py::TC::test_four ERROR", | ||
"test_trial_error.py::TC::test_one FAILED", | ||
"test_trial_error.py::TC::test_three FAILED", | ||
"test_trial_error.py::TC::test_two SKIPPED", | ||
"test_trial_error.py::TC::test_two ERROR", | ||
"test_trial_error.py::TC::test_two FAILED", | ||
"*ERRORS*", | ||
"*_ ERROR at teardown of TC.test_four _*", | ||
"NOTE: Incompatible Exception Representation, displaying natively:", | ||
"*DelayedCalls*", | ||
"*_ ERROR at teardown of TC.test_two _*", | ||
"NOTE: Incompatible Exception Representation, displaying natively:", | ||
"*DelayedCalls*", | ||
"*= FAILURES =*", | ||
# "*_ TC.test_four _*", | ||
# "*NameError*crash*", | ||
"*_ TC.test_four _*", | ||
"*NameError*crash*", | ||
"*_ TC.test_one _*", | ||
"*NameError*crash*", | ||
"*_ TC.test_three _*", | ||
"NOTE: Incompatible Exception Representation, displaying natively:", | ||
"*DelayedCalls*", | ||
"*= 2 failed, 2 skipped, 2 errors in *", | ||
"*_ TC.test_two _*", | ||
"*NameError*crash*", | ||
"*= 4 failed, 1 error in *", | ||
] | ||
) | ||
|
||
|
@@ -876,6 +872,37 @@ def test_notTornDown(): | |
reprec.assertoutcome(passed=1, failed=1) | ||
|
||
|
||
def test_cleanup_functions(testdir): | ||
"""Ensure functions added with addCleanup are always called after each test ends (#6947)""" | ||
testdir.makepyfile( | ||
""" | ||
import unittest | ||
|
||
cleanups = [] | ||
|
||
class Test(unittest.TestCase): | ||
|
||
def test_func_1(self): | ||
self.addCleanup(cleanups.append, "test_func_1") | ||
|
||
def test_func_2(self): | ||
self.addCleanup(cleanups.append, "test_func_2") | ||
assert 0 | ||
|
||
def test_func_3_check_cleanups(self): | ||
assert cleanups == ["test_func_1", "test_func_2"] | ||
""" | ||
) | ||
result = testdir.runpytest("-v") | ||
result.stdout.fnmatch_lines( | ||
[ | ||
"*::test_func_1 PASSED *", | ||
"*::test_func_2 FAILED *", | ||
"*::test_func_3_check_cleanups PASSED *", | ||
] | ||
) | ||
|
||
|
||
def test_issue333_result_clearing(testdir): | ||
testdir.makeconftest( | ||
""" | ||
|
@@ -1131,6 +1158,41 @@ def test(self): | |
assert result.ret == 0 | ||
|
||
|
||
def test_pdb_teardown_called(testdir, monkeypatch): | ||
"""Ensure tearDown() is always called when --pdb is given in the command-line. | ||
|
||
We delay the normal tearDown() calls when --pdb is given, so this ensures we are calling | ||
tearDown() eventually to avoid memory leaks when using --pdb. | ||
""" | ||
teardowns = [] | ||
monkeypatch.setattr( | ||
pytest, "test_pdb_teardown_called_teardowns", teardowns, raising=False | ||
) | ||
|
||
testdir.makepyfile( | ||
""" | ||
import unittest | ||
import pytest | ||
|
||
class MyTestCase(unittest.TestCase): | ||
|
||
def tearDown(self): | ||
pytest.test_pdb_teardown_called_teardowns.append(self.id()) | ||
|
||
def test_1(self): | ||
pass | ||
def test_2(self): | ||
pass | ||
""" | ||
) | ||
result = testdir.runpytest_inprocess("--pdb") | ||
result.stdout.fnmatch_lines("* 2 passed in *") | ||
assert teardowns == [ | ||
"test_pdb_teardown_called.MyTestCase.test_1", | ||
"test_pdb_teardown_called.MyTestCase.test_2", | ||
] | ||
|
||
|
||
def test_async_support(testdir): | ||
pytest.importorskip("unittest.async_case") | ||
|
||
|
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.
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.
Can you please review this bit @RonnyPfannschmidt? I noticed this was needed after one of the tests failed.
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.
i'm actually YOLO on that one, my insight to the implementation of unittest is limited