sorin
sorin

Reputation: 170638

How to tell py.test to skip certain directories?

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!

Upvotes: 191

Views: 190893

Answers (11)

the-citto
the-citto

Reputation: 137

+1 to @kartiks77

quite likely that this wasn't available yet at the time of the question (added in version 6.0)

I opted for specifying testpaths, but addopts works too, and it's very good to know for setting defaults

# pyproject.toml
[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-ra -q"
testpaths = [
    "tests",
    "integration",
]

from pytest's docs

Upvotes: 1

kartiks77
kartiks77

Reputation: 72

I was looking for a way to ignore the __pypackages__ directory.

When using PDM, the following ignore flag placed in pyproject.toml worked for me:

[tool.pytest.ini_options]
addopts = [
    "--ignore=packages/some_pkg/__pypackages__",
]

Upvotes: 3

Abdullah
Abdullah

Reputation: 249

This (regex pattern) worked for me in pytest.ini:

filterwarnings =
    ignore::DeprecationWarning:.*(venv)*

you can also do this:

filterwarnings =
    ignore:::.*(venv)*

or to be more specific with libraries, you can do:

filterwarnings =
    ignore::DeprecationWarning:pkg_resources.*
    ignore::DeprecationWarning:django.*
    ignore::DeprecationWarning:rest_framework_simplejwt.*

Upvotes: -1

shadfc
shadfc

Reputation: 6594

py.test --ignore=somedir worked for me

In pytest.ini:

[pytest]
addopts = --ignore=somedir --ignore=someotherdir

Upvotes: 210

therightstuff
therightstuff

Reputation: 1027

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

Upvotes: 6

RChat
RChat

Reputation: 617

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"

Upvotes: 5

bluenote10
bluenote10

Reputation: 26710

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

[tool:pytest]
norecursedirs = subpath/*

whereas just norecursedirs = subpath didn't.

Upvotes: 20

David Batista
David Batista

Reputation: 3134

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.

Upvotes: 65

sorin
sorin

Reputation: 170638

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.

Upvotes: 31

Salvatore Avanzo
Salvatore Avanzo

Reputation: 2786

You can use

py.test -k 'not third'

that excludes all 'third' directory contents.

Upvotes: 29

ecatmur
ecatmur

Reputation: 157434

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

Upvotes: 12

Related Questions