pyproject.toml 配置忽略特定路径?

Yau*_*dan 19 python pytest pyproject.toml

有没有什么方法可以设置忽略的路径,pyproject.toml例如:

#pyproject.toml
[tool.pytest.ini_options]
ignore = ["path/to/test"]
Run Code Online (Sandbox Code Playgroud)

而不是使用addopts

#pyproject.toml
[tool.pytest.ini_options]
addopts = "--ignore=path/to/test"   
Run Code Online (Sandbox Code Playgroud)

Jon*_*hoi 10

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

但是,您有一些选择:

使用testpaths

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

维护一个白名单(包含列表)来运行测试是一个好主意,例如包或测试套件。这设置了应该搜索测试发现的目录列表。

[tool.pytest.ini_options]
testpaths = your_package testing
Run Code Online (Sandbox Code Playgroud)

用于collect_ignoretestconf.py

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 -变量

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

# conftest.py
collect_ignore = ["path/to/test/excluded"]
collect_ignore_glob = ["*_ignore.py"]
Run Code Online (Sandbox Code Playgroud)

评论

作为旁注,用于排除某些子模块或子目录,但这不是一个好主意,因为这应该包含忽略某些构建工件或忽略目录的模式,例如,,,等。norecursedirs _build.venvdist