Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pytest

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?

like image 314
Chris Withers Avatar asked Feb 16 '17 08:02

Chris Withers


People also ask

Is pytest compatible with Unittest?

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.

How do I stop Unittest in Python?

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.

Should pytest tests be in class?

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_*”.

How do I skip pytest?

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.


3 Answers

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.

like image 132
David Röthlisberger Avatar answered Oct 23 '22 07:10

David Röthlisberger


You can also just run pytest with the following flag:

-W ignore::pytest.PytestCollectionWarning
like image 30
Suzana Avatar answered Oct 23 '22 09:10

Suzana


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.

like image 28
wim Avatar answered Oct 23 '22 07:10

wim