繁体   English   中英

如何使用将在 pytest 测试用例中用作参数化的夹具函数和数据的返回

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM