How to tell py.test to skip certain directories?

PythonUnit TestingPytest

Python Problem Overview


I tried to use the norecursedirs option inside setup.cfg to tell py.test not to collect tests from certain directories but it seems it does ignore it.

[tool:pytest]
norecursedirs=lib/third

When I run py.test I do see how it does get tests from inside lib/third!

Python Solutions


Solution 1 - Python

py.test --ignore=somedir worked for me

Solution 2 - Python

If you have several directories with different parents you can specify different --ignore parameters:

py.test --ignore=somedir --ignore=otherdir --ignore=etcdir

> - new option: --ignore will prevent specified path from collection.
Can be specified multiple times.

Solution 3 - Python

I solved the mystery: If a pytest section is found in one of the possible config files (pytest.ini, tox.ini and setup.cfg), pytest will not look for any others so be sure you define the py.test options in a single file.

I would suggest using setup.cfg.

Solution 4 - Python

You can use

py.test -k 'not third'

that excludes all 'third' directory contents.

Solution 5 - Python

In my case, the issue was the missing wildcard. The following works for me:

[tool:pytest]
norecursedirs = subpath/*

whereas just norecursedirs = subpath didn't.

Solution 6 - Python

norecursedirs should work. Check whether you have a pytest.ini or other setup.cfg files. How are you invoking py.test?

Solution 7 - Python

If you are using setup.cfg, you have to use [tool:pytest] as per http://doc.pytest.org/en/latest/example/pythoncollection.html

> # content of pytest.ini
> # can also be defined in tox.ini or setup.cfg file, although the section
> # name in setup.cfg files should be "tool:pytest"

Solution 8 - Python

To use pytest.ini (which is recommended, unless you're using tox in which case tox.ini would make sense), call pytest with pytest -c pytest.ini.

In order to prevent my sample not_me_1.py and not_me_2.py from being tested or linted, my pytest.ini is as follows:

[pytest]
addopts = --ignore-glob=not_me_*

[pycodestyle]
ignore = not_me_* ALL

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsorinView Question on Stackoverflow
Solution 1 - PythonshadfcView Answer on Stackoverflow
Solution 2 - PythonDavid BatistaView Answer on Stackoverflow
Solution 3 - PythonsorinView Answer on Stackoverflow
Solution 4 - PythonSalvatore AvanzoView Answer on Stackoverflow
Solution 5 - Pythonbluenote10View Answer on Stackoverflow
Solution 6 - PythonecatmurView Answer on Stackoverflow
Solution 7 - PythonRChatView Answer on Stackoverflow
Solution 8 - PythontherightstuffView Answer on Stackoverflow