Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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/13885.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed autouse fixtures defined inside a :class:`unittest.TestCase` class running even when the class is decorated with :func:`unittest.skip` or :func:`unittest.skipIf` -- regression since pytest 8.1.0.
18 changes: 18 additions & 0 deletions src/_pytest/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def collect(self) -> Iterable[Item | Collector]:
self._register_unittest_setup_method_fixture(cls)
self._register_unittest_setup_class_fixture(cls)
self._register_setup_class_fixture()
else:
self._register_unittest_skip_fixture(cls)

self.session._fixturemanager.parsefactories(self.newinstance(), self.nodeid)

Expand Down Expand Up @@ -175,6 +177,22 @@ def unittest_setup_class_fixture(
autouse=True,
)

def _register_unittest_skip_fixture(self, cls: type) -> None:
"""Register an auto-use fixture to skip tests for a class decorated
with @unittest.skip or @unittest.skipIf (#13885)."""

def unittest_skip_fixture(request: FixtureRequest) -> None:
reason = cls.__unittest_skip_why__
raise skip.Exception(reason, _use_item_location=True)

self.session._fixturemanager._register_fixture(
name=f"_unittest_skip_fixture_{cls.__qualname__}",
func=unittest_skip_fixture,
nodeid=self.nodeid,
scope="class",
autouse=True,
)

def _register_unittest_setup_method_fixture(self, cls: type) -> None:
"""Register an auto-use fixture to invoke setup_method and
teardown_method (#517)."""
Expand Down
21 changes: 21 additions & 0 deletions testing/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,27 @@ def tearDownClass(self):
reprec.assertoutcome(skipped=1)


def test_unittest_skip_with_autouse_fixture(pytester: Pytester) -> None:
"""Autouse fixtures inside a @unittest.skipIf class should not run (#13885)."""
pytester.makepyfile(
"""
import unittest
import pytest

@unittest.skipIf(True, "skip reason")
class TestSkipped(unittest.TestCase):
@pytest.fixture(autouse=True)
def my_fixture(self):
raise RuntimeError("fixture should not run")

def test_one(self):
pass
"""
)
reprec = pytester.inline_run()
reprec.assertoutcome(skipped=1)


def test_method_and_teardown_failing_reporting(pytester: Pytester) -> None:
pytester.makepyfile(
"""
Expand Down
Loading