[英]How can I use return of fixture function and data which will be used as parameterized in pytest test case
我从命令行获取一些参数。 然后我想在测试用例中使用这些参数作为变量。 我想在同一个测试用例中使用参数化测试。 如下运行它是否正确?
conftest.py
import pytest
def pytest_adoption(parser):
parser("--arg1",action="store",help="first argument")
parser("--arg2),action="store",help="second argument")
example_test.py
import pytest
test_data = [("name1","surname1",40),("name2","surname2",30)]
@pytest.fixture(autouse=True)
def getArg(pytestconfig):
arg1 = pytestconfig.getoption("arg1")
arg2 = pytestconfig.getoption("arg2")
return arg1,arg2
@pytest.mark.parametrize("name,surname,age",test_data)
def test_example(name,surname,age):
assert type(getArg) == tuple
arg1,arg2 = getArg
if arg1 == "first" && arg2 == "second" :
print('My name is {} {} and I am {} years old'.format(name,surname,age))
执行:
pytest example_test.py --arg1 "first" --arg2 "second"
我找到了答案,因为fixture函数将作为测试参数给出:
@pytest.mark.parametrize("name,surname,age",test_data)
def test_example(getArg,name,surname,age):
assert type(getArg) == tuple
arg1,arg2 = getArg
if arg1 == "first" && arg2 == "second" :
print('My name is {} {} and I am {} years old'.format(name,surname,age))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.