Skip to content

Commit 9609029

Browse files
committed
Restore skipping tests via raise unittest.SkipTest
Revert "Remove unused code related to `nose` (#13528)" This reverts commit a620d24 and modifies it adding tests and docs. Fixes #13895
1 parent 6eb7609 commit 9609029

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

changelog/13895.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Restore support for skipping tests via ``raise unittest.SkipTest``.

src/_pytest/unittest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,14 @@ def pytest_runtest_makereport(item: Item, call: CallInfo[None]) -> None:
476476
except AttributeError:
477477
pass
478478

479+
# Convert unittest.SkipTest to pytest.skip.
480+
# This covers explicit `raise unittest.SkipTest`.
481+
unittest = sys.modules.get("unittest")
482+
if unittest and call.excinfo and isinstance(call.excinfo.value, unittest.SkipTest):
483+
excinfo = call.excinfo
484+
call2 = CallInfo[None].from_call(lambda: skip(str(excinfo.value)), call.when)
485+
call.excinfo = call2.excinfo
486+
479487

480488
def _is_skipped(obj) -> bool:
481489
"""Return True if the given object has been marked with @unittest.skip."""

testing/test_unittest.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,49 @@ def test_two(self):
10941094
result.assert_outcomes(passed=2)
10951095

10961096

1097+
def test_skip_setup_class(pytester: Pytester) -> None:
1098+
"""Skipping tests in a class by raising unittest.SkipTest in `setUpClass` (#13985)."""
1099+
pytester.makepyfile(
1100+
"""
1101+
import unittest
1102+
1103+
class Test(unittest.TestCase):
1104+
1105+
@classmethod
1106+
def setUpClass(cls):
1107+
raise unittest.SkipTest('Skipping setupclass')
1108+
1109+
def test_foo(self):
1110+
assert False
1111+
1112+
def test_bar(self):
1113+
assert False
1114+
"""
1115+
)
1116+
result = pytester.runpytest()
1117+
result.assert_outcomes(skipped=2)
1118+
1119+
1120+
def test_unittest_skip_function(pytester: Pytester) -> None:
1121+
"""
1122+
Ensure raising an explicit unittest.SkipTest skips standard pytest functions.
1123+
1124+
Support for this is debatable -- technically we only support unittest.SkipTest in TestCase subclasses,
1125+
but stating this support here in this test because users currently expect this to work,
1126+
so if we ever break it we at least know we are breaking this use case (#13985).
1127+
"""
1128+
pytester.makepyfile(
1129+
"""
1130+
import unittest
1131+
1132+
def test_foo():
1133+
raise unittest.SkipTest('Skipping test_foo')
1134+
"""
1135+
)
1136+
result = pytester.runpytest()
1137+
result.assert_outcomes(skipped=1)
1138+
1139+
10971140
def test_testcase_handles_init_exceptions(pytester: Pytester) -> None:
10981141
"""
10991142
Regression test to make sure exceptions in the __init__ method are bubbled up correctly.

0 commit comments

Comments
 (0)