如何告诉py.test跳过某些目录?

sor*_*rin 65 python unit-testing pytest

我试图使用norecursedirssetup.cfg中的选项告诉py.test不要从某些目录中收集测试,但它似乎确实忽略了它.

[tool:pytest]
norecursedirs=lib/third
Run Code Online (Sandbox Code Playgroud)

当我跑步时,py.test我确实看到它是如何从内部进行测试的lib/third!

sha*_*dfc 57

py.test --ignore=somedir 为我工作

  • 并且可以多次指定`--ignore` 来忽略多个目录 (12认同)
  • 也可以[使用 `--ignore-glob`](https://docs.pytest.org/en/stable/example/pythoncollection.html#ignore-paths-during-test-collection) 忽略测试文件基于 Unix shell 风格通配符的路径。例如 `--ignore-glob=**/sandbox/*` 将忽略任何 `sandbox` 文件夹中的所有文件。 (5认同)
  • 您可以使用“-k”选项,如下面的另一个答案所示。请参阅 https://docs.pytest.org/en/latest/usage.html#specifying-tests-selecting-tests 上的“按关键字表达式运行测试”。标记在这里也可能有帮助。将您的测试标记为“慢”等,然后使用“-k“不慢”” (2认同)

sor*_*rin 21

我解开了这个谜团:如果在其中一个可能的配置文件(pytest.ini,tox.inisetup.cfg)中找到pytest部分,pytest将不会查找任何其他部分,因此请确保在单个文件中定义py.test选项.

我建议使用setup.cfg.

  • 为什么是“setup.cfg”?当前文档似乎推荐[其他两个](https://docs.pytest.org/en/latest/reference.html#ini-options-ref) (11认同)

Sal*_*nzo 13

您可以使用

py.test -k 'not third'
Run Code Online (Sandbox Code Playgroud)

排除所有"第三"目录内容.


blu*_*e10 13

就我而言,问题是缺少通配符。以下对我有用:

[tool:pytest]
norecursedirs = subpath/*
Run Code Online (Sandbox Code Playgroud)

而只是norecursedirs = subpath没有。


eca*_*mur 11

norecursedirs应该管用.检查您是否有pytest.ini或其他setup.cfg文件.你是怎么调用的py.test


Dav*_*sta 10

如果您有多个具有不同父级的目录,则可以指定不同的--ignore参数:

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

  • 新选项:--ignore将阻止收集指定的路径。
    可以多次指定。


RCh*_*hat 5

如果您使用 setup.cfg,则必须[tool:pytest]按照http://doc.pytest.org/en/latest/example/pythoncollection.html使用

# pytest.ini 的内容
也可以在 tox.ini 或 setup.cfg 文件中定义,尽管
setup.cfg 文件中的部分名称应为“tool:pytest”


the*_*uff 5

要使用pytest.ini(推荐使用,除非您tox在这种情况下使用tox.ini才有意义),请pytest使用pytest -c pytest.ini.

为了防止我的样品not_me_1.pynot_me_2.py测试或被检查,我pytest.ini的如下:

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

[pycodestyle]
ignore = not_me_* ALL

Run Code Online (Sandbox Code Playgroud)