Home > OS >  Pytest tries to collect wrong classes
Pytest tries to collect wrong classes

Time:11-10

I have a Python module:

.
├── module
│   ├── __init__.py
│   ├── __main__.py
│   └── suite.py
├── docs
├── pyproject.toml
├── pytest.ini
├── setup.cfg
├── setup.py
├── tests
│   ├── test_config.py
│   ├── test_description.py
│   ├── test_id.py
│   ├── foo.py
│   └── test_schema.py
└── tox.ini

When I run pytest with this pytest.ini:

[pytest]
addopts = --doctest-modules
testpaths = tests
python_files = test_*.py
python_classes = Test*

Pytest tries to collect tests inside the module. I have a TestSuite in my module, and I get this error:

PytestCollectionWarning: cannot collect test class 'TestSuite' 
because it has a __init__ constructor

Of course, this is not a test. How to prevent pytest to prevent from false positive?

What's wrong?

CodePudding user response:

I usually set the magic __test__ attribute to False on all classes that pytest should skip for any reason, including the ones that clash with the naming rules from python_classes. Example: create a conftest.py in your project's root directory with the contents

from module.suite import TestSuite

TestSuite.__test__ = False
  • Related