Skip to content

Add note on using mixin classes for abstract test classes in documentation #13346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions changelog/8612.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Add a recipe for handling abstract test classes in the documentation.

A new example has been added to the documentation to demonstrate how to use a mixin class to handle abstract
test classes without manually setting the ``__test__`` attribute for subclasses.
This ensures that subclasses of abstract test classes are automatically collected by pytest.
27 changes: 27 additions & 0 deletions doc/en/example/pythoncollection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,30 @@ with ``Test`` by setting a boolean ``__test__`` attribute to ``False``.
# Will not be discovered as a test
class TestClass:
__test__ = False

.. note::

If you are working with abstract test classes and want to avoid manually setting
the ``__test__`` attribute for subclasses, you can use a mixin class to handle
this automatically. For example:

.. code-block:: python

# Mixin to handle abstract test classes
class NotATest:
def __init_subclass__(cls):
cls.__test__ = NotATest not in cls.__bases__


# Abstract test class
class AbstractTest(NotATest):
pass


# Subclass that will be collected as a test
class RealTest(AbstractTest):
def test_example(self):
assert 1 + 1 == 2

This approach ensures that subclasses of abstract test classes are automatically
collected without needing to explicitly set the ``__test__`` attribute.