natter1
natter1

Reputation: 394

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?

Upvotes: 22

Views: 7055

Answers (3)

Bruno Oliveira
Bruno Oliveira

Reputation: 15285

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

class Testament:
    __test__ = False

Upvotes: 58

Suzana
Suzana

Reputation: 4420

You can run pytest with the following flag:

-W ignore::pytest.PytestCollectionWarning

This is just the "normal" warning filter for python, according to the documentation:

Both -W command-line option and filterwarnings ini option are based on Python’s own -W option and warnings.simplefilter

Upvotes: 4

merwok
merwok

Reputation: 6907

You can define the python_classes option in your config file to change from default Test* to e.g. *Tests.

Upvotes: 1

Related Questions