Repository navigation
Mixin for abstract test classes #8612
Description
Activity
- addedtype: docsdocumentation improvement, missing or needing clarificationdocumentation improvement, missing or needing clarificationtype: proposalproposal for a new feature, often to gather opinions or design the API around the new featureproposal for a new feature, often to gather opinions or design the API around the new feature
on Apr 30, 2021 What I'm really asking is, would you like to add this mixin class to
pytest.__init__?IMO this would be great as a 'recipe' in the documentation for the
__test__attribute, but doesn't quite meet the bar of generality-and-difficulty to justify adding it to our public API. Would you like to open a PR to add it to the docs?Reacted by Freya Bruhin, Bruno Oliveira, anthony sottile and Rahul Kumaresanmixins with side-effects are usually a smell, and I think this is not an exception here -- what if you have two levels of "abstract" you'd have to mixin the class twice (?) -- it's also just as much if not the same amount of typing as just putting
__test__ = False-- admittedly, pytest is designed around avoiding test base classes and instead using fixtures and function-tests -- I usually consider inheritance-based testing a smell because it makes tests significantly harder to reason about and maintainReacted by Zac Hatfield-Dodds- addedgood first issueeasy issue that is friendly to new contributoreasy issue that is friendly to new contributorand removedtype: proposalproposal for a new feature, often to gather opinions or design the API around the new featureproposal for a new feature, often to gather opinions or design the API around the new feature
on Jul 6, 2024 - added a commit that references this issue
on Jan 5, 2026
What's the problem this feature will solve?
Abstract test classes that use __test__=False to avoid collection require that the subclasses set __test__=True to be collected. If a user forgets to reset the attribute, the test they thought was running will be silently ignored. Difficult to catch in large projects.
Describe the solution you'd like
Have subclasses collected by pytest regardless of superclass attribute. I use a mixin class to achieve this:
Mixin clearly indicates what's not a test. No need to unset on subclasses. Safe to mixin on subclasses if they are also abstract. What I'm really asking is, would you like to add this mixin class to pytest.__init__?
Alternative Solutions
Additional context
No