161

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!

kkurian
  • 3,844
  • 3
  • 30
  • 49
sorin
  • 161,544
  • 178
  • 535
  • 806
  • 1
    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]`. – sorin Jun 20 '12 at 13:21
  • 1
    `pytest` is from logilab. You want `py.test`. – ecatmur Jun 20 '12 at 13:24
  • 3
    also try doing `nosecuredirs=lib/third/*` – intelis Nov 16 '16 at 16:54
  • is there a way to ignore some folders in the script itself with code ? – bicepjai Mar 07 '17 at 20:56
  • 2
    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! – andilabs Dec 21 '18 at 23:45
  • 1
    @ecatmur I guess your comment is outdated? I think today, one should use `pytest` instead of `py.test` – Martin Thoma Mar 18 '20 at 13:21

8 Answers8

171

py.test --ignore=somedir worked for me

In pytest.ini:

[pytest]
addopts = --ignore=somedir --ignore=someotherdir
LightCC
  • 9,804
  • 5
  • 52
  • 92
shadfc
  • 6,104
  • 3
  • 25
  • 19
  • 29
    And `--ignore` can be specified multiple times to ignore multiple directories – phoenix Mar 27 '19 at 18:20
  • How to skip certain tests instead of directory – Rajat jain Apr 09 '19 at 05:56
  • 1
    You can use the `-k` option as another answer below shows. See "Run tests by keyword expressions" on https://docs.pytest.org/en/latest/usage.html#specifying-tests-selecting-tests. Marks may be helpful here as well. Mark your tests as "slow", etc and then use `-k "not slow"` – shadfc May 06 '19 at 14:34
  • 13
    It is also possible [to use `--ignore-glob`](https://docs.pytest.org/en/stable/example/pythoncollection.html#ignore-paths-during-test-collection) to ignore test file paths based on Unix shell-style wildcards. E.g. `--ignore-glob=**/sandbox/*` will ignore all files in any `sandbox` folder. – Jan M. Sep 24 '20 at 15:05
62

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.
David Batista
  • 3,029
  • 2
  • 23
  • 42
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.

Serp C
  • 864
  • 1
  • 10
  • 24
sorin
  • 161,544
  • 178
  • 535
  • 806
  • 17
    Why `setup.cfg`? The current documentation appears to recommend [the other two](https://docs.pytest.org/en/latest/reference.html#ini-options-ref) – jebob Feb 05 '20 at 15:03
  • 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. – therightstuff Nov 03 '20 at 19:35
  • @therightstuff For me `pytest.ini` is used without such tricks. – mirek Feb 14 '21 at 09:38
25

You can use

py.test -k 'not third'

that excludes all 'third' directory contents.

Salvatore Avanzo
  • 2,656
  • 1
  • 21
  • 30
18

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

[tool:pytest]
norecursedirs = subpath/*

whereas just norecursedirs = subpath didn't.

bluenote10
  • 23,414
  • 14
  • 122
  • 178
12

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

ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • 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. – sorin Apr 18 '13 at 15:36
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"

florisla
  • 12,668
  • 6
  • 40
  • 47
RChat
  • 607
  • 7
  • 9
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

therightstuff
  • 833
  • 1
  • 16
  • 21