Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/13895.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Restore support for skipping tests via ``raise unittest.SkipTest``.
8 changes: 8 additions & 0 deletions src/_pytest/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,14 @@ def pytest_runtest_makereport(item: Item, call: CallInfo[None]) -> None:
except AttributeError:
pass

# Convert unittest.SkipTest to pytest.skip.
# This covers explicit `raise unittest.SkipTest`.
unittest = sys.modules.get("unittest")
if unittest and call.excinfo and isinstance(call.excinfo.value, unittest.SkipTest):
excinfo = call.excinfo
call2 = CallInfo[None].from_call(lambda: skip(str(excinfo.value)), call.when)
call.excinfo = call2.excinfo


def _is_skipped(obj) -> bool:
"""Return True if the given object has been marked with @unittest.skip."""
Expand Down
43 changes: 43 additions & 0 deletions testing/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,6 +1094,49 @@ def test_two(self):
result.assert_outcomes(passed=2)


def test_skip_setup_class(pytester: Pytester) -> None:
"""Skipping tests in a class by raising unittest.SkipTest in `setUpClass` (#13985)."""
pytester.makepyfile(
"""
import unittest

class Test(unittest.TestCase):

@classmethod
def setUpClass(cls):
raise unittest.SkipTest('Skipping setupclass')

def test_foo(self):
assert False

def test_bar(self):
assert False
"""
)
result = pytester.runpytest()
result.assert_outcomes(skipped=2)


def test_unittest_skip_function(pytester: Pytester) -> None:
"""
Ensure raising an explicit unittest.SkipTest skips standard pytest functions.

Support for this is debatable -- technically we only support unittest.SkipTest in TestCase subclasses,
but stating this support here in this test because users currently expect this to work,
so if we ever break it we at least know we are breaking this use case (#13985).
"""
pytester.makepyfile(
"""
import unittest

def test_foo():
raise unittest.SkipTest('Skipping test_foo')
"""
)
result = pytester.runpytest()
result.assert_outcomes(skipped=1)


def test_testcase_handles_init_exceptions(pytester: Pytester) -> None:
"""
Regression test to make sure exceptions in the __init__ method are bubbled up correctly.
Expand Down