pytest文档76

前言

pytest 命令行中 -o 参数的作用是覆盖pytest.ini配置文件中的参数,那就意味着在ini中的参数,也可以在命令行中使用了。

-o 参数

pytest -h 可以查看到-o参数的使用

-o OVERRIDE_INI, --override-ini=OVERRIDE_INI
   override ini option with "option=value" style, e.g. `-o xfail_strict=True -o cache_dir=cache`.

其作用是覆盖ini配置中的"option=value",如:-o xfail_strict=True -o cache_dir=cache

使用示例

之前有小伙伴问到生成JUnit报告,在 pytest.ini 配置文件添加 junit_suite_name 参数可以实现

[pytest]

junit_suite_name=yoyo

但是小伙伴想在命令行中实现,却没有这个参数,当时给的解决办法是在conftest.py中通过钩子函数把命令行参数注册到pytest.ini中

# conftest.py
def pytest_addoption(parser):
    parser.addoption(
        "--suite-name",
        action="store",
        default="yoyo",
        help="'Default yoyo"
    )


def pytest_configure(config):
    name = config.getoption("--suite-name")
    if name:
        config._inicache['junit_suite_name']=name

后来翻阅各种文档发现命令行带上-o参数就能实现,原来pytest早就设计好了

> pytest demo --junit-xml=./report.xml -o junit_suite_name
原文地址:https://www.cnblogs.com/yoyoketang/p/15042462.html