Skip to content

Skip teardown with --pdb for skipped class #10061

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Harald Armin Massa
Harshna
Henk-Jaap Wagenaar
Holger Kohr
Hugo Buddelmeijer
Hugo van Kemenade
Hui Wang (coldnight)
Ian Bicking
Expand Down
2 changes: 2 additions & 0 deletions changelog/10060.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix regression where running with ``--pdb`` would call the ``tearDown`` methods of ``unittest.TestCase``
subclasses tests that are skipped on the class level.
10 changes: 8 additions & 2 deletions src/_pytest/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,5 +410,11 @@ def check_testcase_implements_trial_reporter(done: List[int] = []) -> None:


def _is_skipped(obj) -> bool:
"""Return True if the given object has been marked with @unittest.skip."""
return bool(getattr(obj, "__unittest_skip__", False))
"""Return True if the given object has been marked with @unittest.skip.

Also return True if obj is a method of a skipped object.
"""
skipped = bool(getattr(obj, "__unittest_skip__", False))
if isinstance(obj, types.MethodType):
skipped |= _is_skipped(obj.__self__)
return skipped
14 changes: 13 additions & 1 deletion testing/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,12 +1265,24 @@ def tearDown(self):
def test_1(self):
pass

{mark}("skipped for other reasons")
class MyTestCase2(unittest.TestCase):

def setUp(self):
pytest.test_pdb_teardown_skipped.append("setUp:" + self.id())

def tearDown(self):
pytest.test_pdb_teardown_skipped.append("tearDown:" + self.id())

def test_2(self):
pass

""".format(
mark=mark
)
)
result = pytester.runpytest_inprocess("--pdb")
result.stdout.fnmatch_lines("* 1 skipped in *")
result.stdout.fnmatch_lines("* 2 skipped in *")
assert tracked == []


Expand Down