【pytest】 pytest自定义标记 PytestUnknownMarkWarning处理方式

发布时间 2023-04-03 13:56:30作者: Phoenixy

未注册标记会出现 warnings summary -- PytestUnknownMarkWarning

PytestUnknownMarkWarning: Unknown pytest.mark.demo - is this a typo? You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
@pytest.mark.demo

pytest.mark 内置标记

usefixture-在测试函数或类上使用fixture

filterwarnings-筛选测试函数的某些警告

skip-始终跳过测试函数

skipif-如果满足特定条件,则跳过测试函数

xfail-如果满足特定条件,则产生“预期失败”结果

parameterize-对同一测试函数执行多次调用。

 

   TIps: 创建自定义标记或将标记应用于整个测试类或模块都很容易。这些标记可以被插件使用,也通常用于通过-m选项在命令行上选择测试。

注册标记

 

在pytest.ini文件中注册自定义标记

[pytest]
markers = mark1 mark2 ...

 

pyproject.toml文件中

[tool.pytest.ini_options]
markers = [
    "slow: marks tests as slow (deselect with '-m \"not slow\"')",
    "serial",
]

pytest_configure钩子中以编程方式注册新标记

def pytest_configure(config):
    config.addinivalue_line(
        "markers", "env(name): mark test to run only on named environment"
    )

 

在未知标记上引发错误

[pytest]
addopts = --strict-markers
markers = mark1 mark2 ...

 

 

 

参考: https://docs.pytest.org/en/stable/how-to/mark.html