Skip to content

Commit f3c7b60

Browse files
committed
Do not call tearDown for skipped unittest.TestCases with --pdb
Fix #10060
1 parent 11fb5cd commit f3c7b60

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

changelog/10060.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
When running with ``--pdb``, ``TestCase.tearDown`` is no longer called for tests when the *class* has been skipped via ``unittest.skip`` or ``pytest.mark.skip``.

src/_pytest/unittest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,10 @@ def runtest(self) -> None:
316316
# Arguably we could always postpone tearDown(), but this changes the moment where the
317317
# TestCase instance interacts with the results object, so better to only do it
318318
# when absolutely needed.
319-
if self.config.getoption("usepdb") and not _is_skipped(self.obj):
319+
# We need to consider if the test itself is skipped, or the whole class.
320+
assert isinstance(self.parent, UnitTestCase)
321+
skipped = not _is_skipped(self.obj) and not _is_skipped(self.parent.obj)
322+
if self.config.getoption("usepdb") and skipped:
320323
self._explicit_tearDown = self._testcase.tearDown
321324
setattr(self._testcase, "tearDown", lambda *args: None)
322325

testing/test_unittest.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,12 +1241,15 @@ def test_2(self):
12411241

12421242

12431243
@pytest.mark.parametrize("mark", ["@unittest.skip", "@pytest.mark.skip"])
1244-
def test_pdb_teardown_skipped(
1244+
def test_pdb_teardown_skipped_for_functions(
12451245
pytester: Pytester, monkeypatch: MonkeyPatch, mark: str
12461246
) -> None:
1247-
"""With --pdb, setUp and tearDown should not be called for skipped tests."""
1247+
"""
1248+
With --pdb, setUp and tearDown should not be called for tests skipped
1249+
via a decorator (#7215).
1250+
"""
12481251
tracked: List[str] = []
1249-
monkeypatch.setattr(pytest, "test_pdb_teardown_skipped", tracked, raising=False)
1252+
monkeypatch.setattr(pytest, "track_pdb_teardown_skipped", tracked, raising=False)
12501253

12511254
pytester.makepyfile(
12521255
"""
@@ -1256,10 +1259,10 @@ def test_pdb_teardown_skipped(
12561259
class MyTestCase(unittest.TestCase):
12571260
12581261
def setUp(self):
1259-
pytest.test_pdb_teardown_skipped.append("setUp:" + self.id())
1262+
pytest.track_pdb_teardown_skipped.append("setUp:" + self.id())
12601263
12611264
def tearDown(self):
1262-
pytest.test_pdb_teardown_skipped.append("tearDown:" + self.id())
1265+
pytest.track_pdb_teardown_skipped.append("tearDown:" + self.id())
12631266
12641267
{mark}("skipped for reasons")
12651268
def test_1(self):
@@ -1274,6 +1277,43 @@ def test_1(self):
12741277
assert tracked == []
12751278

12761279

1280+
@pytest.mark.parametrize("mark", ["@unittest.skip", "@pytest.mark.skip"])
1281+
def test_pdb_teardown_skipped_for_classes(
1282+
pytester: Pytester, monkeypatch: MonkeyPatch, mark: str
1283+
) -> None:
1284+
"""
1285+
With --pdb, setUp and tearDown should not be called for tests skipped
1286+
via a decorator on the class (#10060).
1287+
"""
1288+
tracked: List[str] = []
1289+
monkeypatch.setattr(pytest, "track_pdb_teardown_skipped", tracked, raising=False)
1290+
1291+
pytester.makepyfile(
1292+
"""
1293+
import unittest
1294+
import pytest
1295+
1296+
{mark}("skipped for reasons")
1297+
class MyTestCase(unittest.TestCase):
1298+
1299+
def setUp(self):
1300+
pytest.track_pdb_teardown_skipped.append("setUp:" + self.id())
1301+
1302+
def tearDown(self):
1303+
pytest.track_pdb_teardown_skipped.append("tearDown:" + self.id())
1304+
1305+
def test_1(self):
1306+
pass
1307+
1308+
""".format(
1309+
mark=mark
1310+
)
1311+
)
1312+
result = pytester.runpytest_inprocess("--pdb")
1313+
result.stdout.fnmatch_lines("* 1 skipped in *")
1314+
assert tracked == []
1315+
1316+
12771317
def test_async_support(pytester: Pytester) -> None:
12781318
pytest.importorskip("unittest.async_case")
12791319

0 commit comments

Comments
 (0)