File tree 2 files changed +32
-0
lines changed
2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change
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.
Original file line number Diff line number Diff line change @@ -325,3 +325,30 @@ with ``Test`` by setting a boolean ``__test__`` attribute to ``False``.
325
325
# Will not be discovered as a test
326
326
class TestClass :
327
327
__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.
You can’t perform that action at this time.
0 commit comments