How to tell py.test to skip certain directories?
Asked Answered
A

9

177

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!

Advocate answered 20/6, 2012 at 9:57 Comment(6)
It seems that I have py.test and pytest both of them runnings tests and being different beasts. Strange but pytest is the one failing because it does not load the exclusions from [pytest].Advocate
pytest is from logilab. You want py.test.Stranglehold
also try doing nosecuredirs=lib/third/*Endres
is there a way to ignore some folders in the script itself with code ?Punt
ended up here because I was curious why the hell my pytest of web app in local development are so extremly slow... The reason was some uploaded resources directory with nested yy/dd/mm structure.... causing it reallly sucks! thankfully [pytest] norecursedirs = resources in pytest.ini does a trick!💪Don
@Stranglehold I guess your comment is outdated? I think today, one should use pytest instead of py.testZillah
A
31

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.

Advocate answered 20/6, 2012 at 13:19 Comment(3)
Why setup.cfg? The current documentation appears to recommend the other twoSympathize
i think it's important to mention that if you want to use a .ini file you need to specify it with pytest -c pytest.ini. for some reason setup.cfg contents were picked up automatically.Complicated
@Complicated For me pytest.ini is used without such tricks.Flambeau
L
190

py.test --ignore=somedir worked for me

In pytest.ini:

[pytest]
addopts = --ignore=somedir --ignore=someotherdir
Longdrawnout answered 30/1, 2017 at 16:0 Comment(4)
And --ignore can be specified multiple times to ignore multiple directoriesDanika
How to skip certain tests instead of directoryVisibility
You can use the -k option as another answer below shows. See "Run tests by keyword expressions" on docs.pytest.org/en/latest/…. Marks may be helpful here as well. Mark your tests as "slow", etc and then use -k "not slow"Longdrawnout
It is also possible to use --ignore-glob to ignore test file paths based on Unix shell-style wildcards. E.g. --ignore-glob=**/sandbox/* will ignore all files in any sandbox folder.Species
E
64

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.
Elfin answered 7/2, 2019 at 10:43 Comment(0)
A
31

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.

Advocate answered 20/6, 2012 at 13:19 Comment(3)
Why setup.cfg? The current documentation appears to recommend the other twoSympathize
i think it's important to mention that if you want to use a .ini file you need to specify it with pytest -c pytest.ini. for some reason setup.cfg contents were picked up automatically.Complicated
@Complicated For me pytest.ini is used without such tricks.Flambeau
T
26

You can use

py.test -k 'not third'

that excludes all 'third' directory contents.

Trap answered 8/7, 2014 at 6:50 Comment(0)
V
19

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

[tool:pytest]
norecursedirs = subpath/*

whereas just norecursedirs = subpath didn't.

Valkyrie answered 9/10, 2019 at 14:20 Comment(7)
Same here. Very strange behavior though.Calling
Looks like those entries are just globs, not foldernames, then. Note you can add more entries with a space after each.Maccarthy
does it support pattern such as a/**/test*.py?Wilmoth
for me addition of the asterisk is unnecessary, it works without itVercingetorix
is the folder path absolute or relative to pytest entry point ?Grounds
@matanster Perhaps pytest's behavior has changed in the meantime?Valkyrie
@Pierrick Rambaud: I'd always assume relative, not absolute. If one would commit an absolute path to a version control system, it would only work in this particular absolute checkout path.Valkyrie
S
12

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

Stranglehold answered 20/6, 2012 at 12:17 Comment(1)
I do have setup.cfg with the proper [pytest] and norecorsedirs inside but it seems to be ignored, and instead it will look for all files.Advocate
B
5

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"

Brazell answered 27/2, 2017 at 14:37 Comment(0)
C
5

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

Complicated answered 3/11, 2020 at 19:40 Comment(0)
D
-1

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.*
Disenfranchise answered 20/2 at 9:38 Comment(1)
it's counter-productive to run tests and then simply ignore them. The OP isn't talking about ignoring warnings, but skipping tests.Rafaellle

© 2022 - 2024 — McMap. All rights reserved.