diff --git a/src/_pytest/unittest.py b/src/_pytest/unittest.py index 851e4943b23..1b158cb37be 100644 --- a/src/_pytest/unittest.py +++ b/src/_pytest/unittest.py @@ -316,7 +316,9 @@ def runtest(self) -> None: # Arguably we could always postpone tearDown(), but this changes the moment where the # TestCase instance interacts with the results object, so better to only do it # when absolutely needed. - if self.config.getoption("usepdb") and not _is_skipped(self.obj): + if self.config.getoption("usepdb") and not ( + _is_skipped(self.obj) or _is_skipped(self._testcase) + ): self._explicit_tearDown = self._testcase.tearDown setattr(self._testcase, "tearDown", lambda *args: None) diff --git a/testing/test_unittest.py b/testing/test_unittest.py index fb312814542..22448c8d4eb 100644 --- a/testing/test_unittest.py +++ b/testing/test_unittest.py @@ -1274,6 +1274,45 @@ def test_1(self): assert tracked == [] +@pytest.mark.parametrize( + "mark", + [ + '@unittest.skip("skipped for reasons")', + '@pytest.mark.skip(reason="skipped for reasons")', + ], +) +def test_pdb_teardown_skipped_class( + pytester: Pytester, monkeypatch: MonkeyPatch, mark: str +) -> None: + tracked: List[str] = [] + monkeypatch.setattr( + pytest, "test_pdb_teardown_skipped_class", tracked, raising=False + ) + + pytester.makepyfile( + """ + import unittest + import pytest + + {mark} + class MyTestCase(unittest.TestCase): + + def tearDown(self): + pytest.test_pdb_teardown_skipped_class.append(self.id()) + + def test_1(self): + pass + + """.format( + mark=mark + ) + ) + + result = pytester.runpytest_inprocess("--pdb") + result.stdout.fnmatch_lines("* 1 skipped in *") + assert tracked == [] + + def test_async_support(pytester: Pytester) -> None: pytest.importorskip("unittest.async_case")