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?
pytest supports running Python unittest -based tests out of the box. It's meant for leveraging existing unittest -based test suites to use pytest as a test runner and also allow to incrementally adapt the test suite to take full advantage of pytest's features.
Somewhat related to your question, if you are using python 2.7, you can use the -f/--failfast flag when calling your test with python -m unittest . This will stop the test at the first failure.
Pytests may be written either as functions or as methods in classes – unlike unittest, which forces tests to be inside classes. Test classes must be named “Test*”, and test functions/methods must be named “test_*”.
The simplest way to skip a test is to mark it with the skip decorator which may be passed an optional reason . It is also possible to skip imperatively during test execution or setup by calling the pytest. skip(reason) function. This is useful when it is not possible to evaluate the skip condition during import time.
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.
You can also just run pytest with the following flag:
-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.
The defaults are like this:
[pytest]
python_files=test_*.py *_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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With