ely*_*ely 8 python unit-testing pytest python-decorators
我继承了一些实现pytest.mark.skipif一些测试的代码。通读 pytest 文档,我知道我可以添加条件,可能检查环境变量,或者使用更高级的功能pytest.mark来控制测试组。不幸的是,到目前为止文档中的任何内容似乎都无法解决我的问题。
我希望简单地关闭任何测试跳过,但不修改测试的任何源代码。我只想在不支持任何测试跳过指标的模式下运行 pytest。pytest 是否存在这样的解决方案?
小智 11
创建一个conftest.py,内容如下:
import pytest
import _pytest.skipping
def pytest_addoption(parser):
parser.addoption(
"--no-skips",
action="store_true",
default=False, help="disable skip marks")
@pytest.hookimpl(tryfirst=True)
def pytest_cmdline_preparse(config, args):
if "--no-skips" not in args:
return
def no_skip(*args, **kwargs):
return
_pytest.skipping.skip = no_skip
Run Code Online (Sandbox Code Playgroud)
在命令行中使用--no-skip来运行所有测试用例,即使某些测试用例带有pytest.mark.skip装饰器
这是一个基于hoefling 答案的简短工作解决方案:
添加您的conftest.py:
from typing import Any, List
from typing_extensions import Final
NO_SKIP_OPTION: Final[str] = "--no-skip"
def pytest_addoption(parser):
parser.addoption(NO_SKIP_OPTION, action="store_true", default=False, help="also run skipped tests")
def pytest_collection_modifyitems(config,
items: List[Any]):
if config.getoption(NO_SKIP_OPTION):
for test in items:
test.own_markers = [marker for marker in test.own_markers if marker.name not in ('skip', 'skipif')]
Run Code Online (Sandbox Code Playgroud)