Fix autouse fixtures running in @unittest.skip classes#14210
Merged
Pierre-Sassoulas merged 2 commits intoMay 23, 2026
Conversation
) 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>
RonnyPfannschmidt
approved these changes
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>
2 tasks
Pierre-Sassoulas
approved these changes
May 23, 2026
This was referenced May 23, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #13885.
When a
unittest.TestCaseis 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,parsefactoriesstill 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 raisesskip.Exceptionbefore any other fixtures run, ensuring the class is properly skipped.Test
Added
test_unittest_skip_with_autouse_fixturethat 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.