Search code examples
pythonpytest

How to prevent PytestCollectionWarning when testing class Testament via pytest


Update to more general case: How can I prevent a PytestCollectionWarning when testing a Class Testament via pytest? Simple example for testament.py:

class Testament():
    def __init__(self, name):
        self.name = name

    def check(self):
        return True

And the test_testament.py

from testament.testament import Testament

def test_send():
    testament = Testament("Paul")

    assert testament.check()

This creates a PytestCollectionWarning when run with pytest. Is there a way to suppress this warning for the imported module without turning all warnings off?


Solution

  • You can set a __test__ = False attribute in classes that pytest should ignore (doc):

    class Testament:
        __test__ = False