Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent PytestCollectionWarning when testing class Testament via pytest

Tags:

python

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?

like image 775
natter1 Avatar asked Oct 30 '19 11:10

natter1


3 Answers

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

class Testament:
    __test__ = False
like image 187
Bruno Oliveira Avatar answered Nov 18 '22 10:11

Bruno Oliveira


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

like image 32
Suzana Avatar answered Nov 18 '22 11:11

Suzana


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

like image 1
merwok Avatar answered Nov 18 '22 09:11

merwok