我目前正在使用
@pytest.mark.skip
我的函数 def 上方的注释告诉 pytest 始终跳过此测试,但我只希望它在运行所有测试时跳过测试。如果我使用 -k 参数运行 pytest,告诉它运行特定测试,当它与带注释的函数匹配时,它会跳过它。但我不希望它在这种情况下跳过测试。仅当运行所有测试时,如不使用 -k 选项时。有办法做到吗?
文档似乎在这方面有一些想法。特别是:
或者,也可以通过调用 pytest.skip(reason) 函数在测试执行或设置期间强制跳过:
def test_function(): if not valid_config(): pytest.skip("unsupported configuration")
当无法在导入期间评估跳过条件时,命令式方法非常有用。
您可以将其与对
request
装置的一些古怪的内省结合起来,如下所示:
import pytest
def test_function(request):
if 'test_function' not in request.config.known_args_namespace.keyword:
pytest.skip("This test is disabled")
这让我们产生以下行为:
$ pytest -v
[...]
test_conditions.py::test_function SKIPPED (This test is disabled) [100%]
还有
-k
:
$ pytest -v -k test_function
[...]
test_conditions.py::test_function PASSED [100%]