Python 在Pytest中是否可以根据命令行参数使用参数?
我有一些像这样的老测试:Python 在Pytest中是否可以根据命令行参数使用参数?,python,pytest,Python,Pytest,我有一些像这样的老测试: test1(some_param): ... test2(some_param): ... call_tests(param): for test in all_test: test(param) if arg_a: call_tests("foo") if arg_b: call_tests("bar") 我想将它们转换为使用Pytest,并以正确的方式进行转换。我想在conftest.py @pytes
test1(some_param):
...
test2(some_param):
...
call_tests(param):
for test in all_test:
test(param)
if arg_a:
call_tests("foo")
if arg_b:
call_tests("bar")
我想将它们转换为使用Pytest,并以正确的方式进行转换。我想在conftest.py
@pytest.fixture(scope="session",
params=["foo", "bar"])
def some_param(request):
return request.param
但我还没有发现任何可以从命令行参数控制参数列表的示例。我相信我能解决这个问题,但我希望Pytest已经有了一些很好的解决方法。是这样,还是我应该自己想办法呢?是的,您可以传递命令行参数。 在conftest.py中,可以使用parser.addoption。例如:
def pytest_addoption(parser):
parser.addoption("--browser", action="store", default="chrome")
def pytest_generate_tests(metafunc):
option_value = meta.config.option.browser
if 'browser' in metafunc.fixturenames and option_value is not None:
metafunc.parametrize("browser", [option_value])
还有更多信息是的,您可以传递命令行参数。 在conftest.py中,可以使用parser.addoption。例如:
def pytest_addoption(parser):
parser.addoption("--browser", action="store", default="chrome")
def pytest_generate_tests(metafunc):
option_value = meta.config.option.browser
if 'browser' in metafunc.fixturenames and option_value is not None:
metafunc.parametrize("browser", [option_value])
还有更多信息