如何在pyproject.toml配置文件中忽略特定路径?

17

是否有办法在 pyproject.toml 中设置要忽略的路径,类似于

#pyproject.toml
[tool.pytest.ini_options]
ignore = ["path/to/test"]

不要使用addopts,而是:

#pyproject.toml
[tool.pytest.ini_options]
addopts = "--ignore=path/to/test"   
1个回答

7
您可以在[tool.pytest.ini_options](或pytest.ini)中添加的配置选项列表在https://docs.pytest.org/en/7.1.x/reference/reference.html#configuration-options中得到记录,其中包括addopts, norecursedirs等。目前为止,在pytest.inipyproject.toml中没有像测试发现那样可以列出ignore的选项。

但是,您有一些替代方案:

使用testpaths

https://docs.pytest.org/en/7.1.x/reference/reference.html#confval-testpaths

建议维护一个白名单(includelist),以便运行测试,如包或测试套件。这将设置应搜索进行测试发现的目录列表。

[tool.pytest.ini_options]
testpaths = your_package testing

testconf.py中使用collect_ignore

https://docs.pytest.org/en/7.1.x/example/pythoncollection.html#customizing-test-collection https://docs.pytest.org/en/7.1.x/reference/reference.html#global-variables

您可以在conftest.py中添加全局变量collect_ignoreconnect_ignore_glob,以自定义排除哪些文件/目录在收集测试时。这更为强大,因为它允许动态、运行时值,而不是在pyproject.toml中。

# conftest.py
collect_ignore = ["path/to/test/excluded"]
collect_ignore_glob = ["*_ignore.py"]

备注

顺便提一下,norecursedirs 可以用于排除一些子模块或子目录,但这并不是一个好主意,因为它应该包含要忽略的一些构建产物或被忽略的目录的模式,例如 _build, .venv, dist 等。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接