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

(cherry picked from commit 46c94ee82d)

Co-authored-by: Bahram Farahmand <bahram.0098.bf@gmail.com>
This commit is contained in:
patchback[bot] 2025-04-07 11:21:38 +00:00 committed by GitHub
parent bb4d149c9d
commit 26af4941fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 0 deletions

5
changelog/8612.doc.rst Normal file
View File

@ -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.

View File

@ -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.