前言
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