Skip to content

Fix autouse fixtures running in @unittest.skip classes#14210

Open
Fridayai700 wants to merge 2 commits intopytest-dev:mainfrom
Fridayai700:fix-unittest-skipif-autouse
Open

Fix autouse fixtures running in @unittest.skip classes#14210
Fridayai700 wants to merge 2 commits intopytest-dev:mainfrom
Fridayai700:fix-unittest-skipif-autouse

Conversation

@Fridayai700
Copy link

Fixes #13885.

When a unittest.TestCase is decorated with @unittest.skipIf(True, ...) or @unittest.skip, autouse pytest fixtures defined inside the class were still being executed. This is a regression since pytest 8.1.0.

Root cause

Commit c8792bd changed fixture registration so that when a class is skipped (_is_skipped(cls) is True), the unittest setup fixtures are not registered. However, parsefactories still discovers user-defined autouse fixtures inside the class. With no skip fixture registered, there is nothing to preempt the user fixtures, so they run and crash.

Fix

Register a dedicated skip fixture for classes marked with @unittest.skip/@unittest.skipIf. This fixture raises skip.Exception before any other fixtures run, ensuring the class is properly skipped.

Test

Added test_unittest_skip_with_autouse_fixture that reproduces the regression: a @unittest.skipIf(True, ...) class with an autouse fixture that raises RuntimeError. Before the fix, the fixture ran and caused an error. After the fix, the test is properly skipped.

)

When a unittest.TestCase is decorated with @unittest.skipIf(True, ...)
or @unittest.skip, autouse pytest fixtures defined inside the class were
still being executed. This is a regression since pytest 8.1.0 (commit
c8792bd) which changed fixture registration to skip registering the
unittest setup fixtures when the class is skipped, but this also meant
no skip fixture was registered to preempt user-defined autouse fixtures.

Fix by registering a dedicated skip fixture for classes marked with
@unittest.skip, ensuring the skip happens before any other fixtures run.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Feb 18, 2026
Use getattr() instead of direct attribute access to satisfy mypy's
type checking, since __unittest_skip_why__ is set dynamically by
unittest.skip decorators and not visible to the type checker.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fixture executed even if all tests are skipped

2 participants