pytest命令行传参

pytest命令行传参方式1:

首先在conftest.py文件中定义:

def pytest_addoption(parser):
    parser.addoption(
        "--stringinput",
        action='append',
        default =[],
        help = "list of stringinputs to pass to test functions"

    )

def pytest_generate_tests(metafunc):
    if "stringinput" in metafunc.fixturenames:
        metafunc.parametrize("stringinput",metafunc.config.getoption("stringinput"))

然后在 test_compute.py文件中使用

def test_valid_string(stringinput):
    assert stringinput.isalpha()


命令行运行:
传两个参数:
pytest -q --stringinput="hello" --stringinput="world" test_compute.py

传一个参数:
pytest -q --stringinput="world" test_compute.py
原文地址:https://www.cnblogs.com/liulilitoday/p/13474461.html