从命令行中使用 pytest 排除多个标记的测试

Uba*_*eri 9 python pytest

我的中有以下内容pyproject.toml

[tool.pytest.ini_options]

markers = [
    "plot: marks SLOW plot tests (deselect with '-m \"not plot\"')",
    "open_tutorial: marks the open_tutorial (which opens VSCode all the times)"
]
Run Code Online (Sandbox Code Playgroud)

我有一堆相应标记的测试方法。

如果我跑

coverage run --branch -m pytest -m "not open_tutorial"
Run Code Online (Sandbox Code Playgroud)

或者

coverage run --branch -m pytest -m "not plot"
Run Code Online (Sandbox Code Playgroud)

我得到了想要的结果,即跳过了标记的测试,但我不知道如何pytest跳过这两个测试。我尝试了以下方法

coverage run --branch -m pytest -m "not open_tutorial" -m "not plot"
coverage run --branch -m pytest -m "not open_tutorial" "not plot"
coverage run --branch -m pytest -m ["not open_tutorial","not plot"]
Run Code Online (Sandbox Code Playgroud)

但它们都不起作用。

小智 14

根据 pytest 帮助:

-m MARKEXPR 仅运行与给定标记表达式匹配的测试。例如:-m 'mark1 而不是 mark2'。

如果您想使用多个标记,则应使用and运算符。

pytest -m "not open_tutorial and not plot"
Run Code Online (Sandbox Code Playgroud)

将运行所有没有标记的测试:open_tutorial并且plot

但:

pytest -m "not open_tutorial and not plot and othermark"
Run Code Online (Sandbox Code Playgroud)

othermark如果他们没有plot或标记,将运行测试open_tutorial