Skip to content

Commit 26af494

Browse files
Add note on using mixin classes for abstract test classes in documentation (#13346) (#13359)
(cherry picked from commit 46c94ee) Co-authored-by: Bahram Farahmand <[email protected]>
1 parent bb4d149 commit 26af494

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

changelog/8612.doc.rst

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Add a recipe for handling abstract test classes in the documentation.
2+
3+
A new example has been added to the documentation to demonstrate how to use a mixin class to handle abstract
4+
test classes without manually setting the ``__test__`` attribute for subclasses.
5+
This ensures that subclasses of abstract test classes are automatically collected by pytest.

doc/en/example/pythoncollection.rst

+27
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,30 @@ with ``Test`` by setting a boolean ``__test__`` attribute to ``False``.
325325
# Will not be discovered as a test
326326
class TestClass:
327327
__test__ = False
328+
329+
.. note::
330+
331+
If you are working with abstract test classes and want to avoid manually setting
332+
the ``__test__`` attribute for subclasses, you can use a mixin class to handle
333+
this automatically. For example:
334+
335+
.. code-block:: python
336+
337+
# Mixin to handle abstract test classes
338+
class NotATest:
339+
def __init_subclass__(cls):
340+
cls.__test__ = NotATest not in cls.__bases__
341+
342+
343+
# Abstract test class
344+
class AbstractTest(NotATest):
345+
pass
346+
347+
348+
# Subclass that will be collected as a test
349+
class RealTest(AbstractTest):
350+
def test_example(self):
351+
assert 1 + 1 == 2
352+
353+
This approach ensures that subclasses of abstract test classes are automatically
354+
collected without needing to explicitly set the ``__test__`` attribute.

0 commit comments

Comments
 (0)