From 589dd2312fecf8678c7dbe4e279518100ebfe57c Mon Sep 17 00:00:00 2001 From: Casey Brooks Date: Thu, 25 Dec 2025 21:00:40 +0000 Subject: [PATCH] fix(unittest): skip tearDown for skipped tests [skip ci] --- src/_pytest/unittest.py | 3 ++- testing/test_unittest.py | 56 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/_pytest/unittest.py b/src/_pytest/unittest.py index 773f545af2e..465869c4322 100644 --- a/src/_pytest/unittest.py +++ b/src/_pytest/unittest.py @@ -122,7 +122,8 @@ def setup(self): def teardown(self): if self._explicit_tearDown is not None: - self._explicit_tearDown() + if not self._store.get(skipped_by_mark_key, False): + self._explicit_tearDown() self._explicit_tearDown = None self._testcase = None self._obj = None diff --git a/testing/test_unittest.py b/testing/test_unittest.py index 83f1b6b2a85..197ce6f10e6 100644 --- a/testing/test_unittest.py +++ b/testing/test_unittest.py @@ -1193,6 +1193,62 @@ def test_2(self): ] +def test_unittest_skip_method_does_not_call_teardown_with_pdb(testdir): + testdir.makepyfile( + """ + import unittest + + class MyTestCase(unittest.TestCase): + @unittest.skip("skip method") + def test_skipped(self): + pass + + def tearDown(self): + raise AssertionError("tearDown should not be called") + """ + ) + result = testdir.runpytest_inprocess("--pdb") + result.assert_outcomes(skipped=1) + + +def test_unittest_setUp_raises_SkipTest_does_not_call_teardown_with_pdb(testdir): + testdir.makepyfile( + """ + import unittest + + class MyTestCase(unittest.TestCase): + def setUp(self): + raise unittest.SkipTest("skip in setup") + + def tearDown(self): + raise AssertionError("tearDown should not be called") + + def test_something(self): + pass + """ + ) + result = testdir.runpytest_inprocess("--pdb") + result.assert_outcomes(skipped=1) + + +def test_unittest_class_skip_does_not_call_teardown_with_pdb(testdir): + testdir.makepyfile( + """ + import unittest + + @unittest.skip("skip class") + class MyTestCase(unittest.TestCase): + def tearDown(self): + raise AssertionError("tearDown should not be called") + + def test_something(self): + pass + """ + ) + result = testdir.runpytest_inprocess("--pdb") + result.assert_outcomes(skipped=1) + + def test_async_support(testdir): pytest.importorskip("unittest.async_case")