Skip to content

Commit 6c86232

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

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,16 @@ 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(
485+
lambda: skip(str(excinfo.value)), call.when
486+
)
487+
call.excinfo = call2.excinfo
488+
479489

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

testing/test_unittest.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,50 @@ def test_two(self):
10931093
result = pytester.runpytest("-s")
10941094
result.assert_outcomes(passed=2)
10951095

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

10971141
def test_testcase_handles_init_exceptions(pytester: Pytester) -> None:
10981142
"""

0 commit comments

Comments
 (0)