简体   繁体   中英

How can I get pytest to ignore Test* classes that don't subclass unittest?

I'm trying to get tests for testfixtures passing with pytest, but it keeps trying to collect things that aren't tests:

======================================================= pytest-warning summary ========================================================
WC1 /Users/chris/vcs/git/testfixtures/testfixtures/tests/test_comparison.py cannot collect test class 'TestClassA' because it has a __init__ constructor
WC1 /Users/chris/vcs/git/testfixtures/testfixtures/tests/test_components.py cannot collect test class 'TestComponents' because it has a __init__ constructor
WC1 /Users/chris/vcs/git/testfixtures/testfixtures/tests/test_datetime.py cannot collect test class 'TestTZInfo' because it has a __new__ constructor
WC1 /Users/chris/vcs/git/testfixtures/testfixtures/tests/test_datetime.py cannot collect test class 'TestTZ2Info' because it has a __new__ constructor
cannot collect test class 'TestContainer' because it has a __init__ constructor
cannot collect test class 'TestTZInfo' because it has a __new__ constructor

How can I get pytest to only collect Test* classes that subclass unittest.TestCase?

I found this in the Pytest 2.6 release notes :

support nose-style __test__ attribute on modules, classes and functions, including unittest-style Classes. If set to False, the test will not be collected.

This can help if your class is being collected when you don't want it to be, but it doesn't help with the "cannot collect test class because it has a __init__ constructor" warning.

您也可以使用以下标志运行 pytest:

-W ignore::pytest.PytestCollectionWarning

Pytest uses glob-style name patterns to collect tests, and the discovery can be customized by the options python_files , python_classes , and python_functions in the configuration file.

I think the defaults are something like this:

[pytest]
python_files=test_*.py
python_classes=Test*
python_functions=test_*

Perhaps you could just customize it not to collect classes by overriding that:

# pytest.ini (or in tox.ini, or in setup.cfg)
[pytest]  # if using setup.cfg, this section name should instead be [tool:pytest]
python_classes=NoThanks

Note: it's not limited to one pattern here, you may specify a list of them.

Classes which inherit unittest.TestCase should still be collected regardless of this option . This is because the unittest framework itself is used to collect those tests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM