Skip to content

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

Merged
Pierre-Sassoulas merged 2 commits into
pytest-dev:mainfrom
worksbyfriday:fix-unittest-skipif-autouse
May 23, 2026
Merged

Fix autouse fixtures running in @unittest.skip classes#14210
Pierre-Sassoulas merged 2 commits into
pytest-dev:mainfrom
worksbyfriday:fix-unittest-skipif-autouse

Conversation

@worksbyfriday

Copy link
Copy Markdown
Contributor

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>
@Pierre-Sassoulas Pierre-Sassoulas merged commit 62ca642 into pytest-dev:main May 23, 2026
33 checks passed
RonnyPfannschmidt pushed a commit that referenced this pull request May 23, 2026
PR #14210 registered the new skip fixture with `nodeid=self.nodeid`, the
legacy string-baseid path that 121d7a4 deprecated in favor of `node=`.
The legacy matching mis-scopes the autouse skip fixture so subclasses of
a `@unittest.skip` class still execute their inherited `setUp` /
`setUpClass`, breaking `test_setup_inheritance_skipping` and
`test_pdb_teardown_skipped_for_classes[@unittest.skip]` on main.

Align with the two existing `_register_unittest_setUp*` fixtures in the
same file.
@Pierre-Sassoulas Pierre-Sassoulas added this to the 9.1 milestone May 23, 2026
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

3 participants