pytest中的pytest_addoption钩子函数-添加额外命令行,并且在用例中获取命令行传参
pytest中的pytest_addoption钩子函数-添加额外命令行,并且在用例中获取命令行传参
详细学习地址:https://blog.csdn.net/fx20211108/article/details/124719004
在conftest.py
文件中:
import pytest
# 注册自定义参数 cmdopt 到配置对象
def pytest_addoption(parser):
parser.addoption("--cmdopt", action="store",
default="None",
help="将自定义命令行参数 ’--cmdopt' 添加到 pytest 配置中")
测试用例test_1.py
中获取命令行参数
def test_1(pytestconfig):
cmd_val = pytestconfig.option.cmdopt # 获取命令行参数
assert cmd_val == 1
执行用例:
pytest test_1.py --cmdopt='123123'
输出结果:
========================================================================== test session starts ==========================================================================
platform linux -- Python 3.7.3, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /home/tarzan/Desktop/playw_test
plugins: playwright-0.3.0, anyio-3.6.2, base-url-2.0.0, rerunfailures-10.2, repeat-0.9.1, timeout-2.1.0, metadata-2.0.4, allure-pytest-2.9.45, html-3.2.0
collected 1 item
test_.py F [100%]
=============================================================================== FAILURES ================================================================================
________________________________________________________________________________ test_1 _________________________________________________________________________________
pytestconfig = <_pytest.config.Config object at 0x7f151de4bc18>
def test_1(pytestconfig):
cmd_val = pytestconfig.option.cmdopt
> assert cmd_val == 1
E AssertionError: assert '123123' == 1
test_.py:6: AssertionError
======================================================================== short test summary info ========================================================================
FAILED test_.py::test_1 - AssertionError: assert '123123' == 1
=========================================================================== 1 failed in 0.02s ===========================================================================
pytest执行顺序: