gpt4 book ai didi

python - 如何通过命令行将多个列表参数传递给多个pytest装置

转载 作者:行者123 更新时间:2023-12-04 09:59:15 25 4
gpt4 key购买 nike

我想将以下列表从我的命令行传递到我的 pytest 代码(如 python 中的 argparser),任何人都可以帮助我如何做到这一点吗?

pytest -vs test_sample.py --html=results.html --A_list=['A1', 'A2'] --B_list=['B1'] --C_list=['C1', 'C2']

这是我的 pytest 代码。

import pytest

@pytest.fixture(scope="session", params=A_list) # here A_list is a list and it has to be passed from command line
def get_A_list(request):
print("inside fixture get_A_list - element in A_list: ".format(request.param))
yield request.param


@pytest.fixture(scope="module", params=B_list) # here B_list is a list and it has to be passed from command line
def get_B_list(request, get_A_list):
print("inside fixture get_B_list - element in A_list: {}, element in B_list: {}: ".format(get_A_list, request.param))
yield request.param


@pytest.mark.parametrize("c", C_list) # here C_list is a list and it has to be passed from command line
def test_functionality(get_A_list, get_B_list, c):
print("inside function test_functionality - element in A_list: {}, element in B_list: {}, element in C_list: {}: ".format(get_A_list, get_B_list, c))

test_functionality 是我的测试用例函数。测试用例的数量应基于命令行中传递的列表的长度

对于上述列表,它必须按以下顺序执行测试用例。

test_functionality[A1-B1-C1]
test_functionality[A1-B1-C2]
test_functionality[A2-B1-C1]
test_functionality[A2-B1-C2]

我需要使用 A_list 中的元素在 **fixture get_A_list 中执行一些操作....同样,我需要使用 A_list 和 B_list 中的元素在 fixture get_B_list 中执行一些操作 .........像这样我有 4 个装置,最后我需要在 test_functionality 中执行测试用例,它将使用 A_list 、 B_list 、 C_list 等中的元素。

最后的输出应该如下所示..

    inside fixture get_A_list - element in A_list: A1
inside fixture get_B_list - element in A_list: A1, element in B_list: B1
inside function test_functionality - element in A_list: A1, element in B_list: B1, element in C_list: C1
inside function test_functionality - element in A_list: A1, element in B_list: B1, element in C_list: C2
inside function test_functionality - element in A_list: A1, element in B_list: B1, element in C_list: C3
inside fixture get_A_list - element in A_list: A2
inside fixture get_B_list - element in A_list: A2, element in B_list: B1
inside function test_functionality - element in A_list: A2, element in B_list: B1, element in C_list: C1
inside function test_functionality - element in A_list: A2, element in B_list: B1, element in C_list: C2
inside function test_functionality - element in A_list: A2, element in B_list: B1, element in C_list: C3

最佳答案

这里的问题是 params 参数是在加载时读取的,此时有关命令行参数的信息尚不可用,因此您不能像示例中那样设置它们。

最好的方法可能是重构您的代码,这样就不需要了。如果您能够做到这一点,那么您就不需要额外的固定装置,

无论如何,您首先必须注册您的命令行参数:

conftest.py

def pytest_addoption(parser):
parser.addoption("--A_list", action="store")
parser.addoption("--B_list", action="store")
parser.addoption("--C_list", action="store")

现在,如果您可以重构您的代码,使固定装置不依赖于参数,您就可以根据传递的命令行参数对您的测试进行参数化。如果您将参数作为逗号分隔列表传递,您可以执行如下操作:

测试.py

import pytest

@pytest.hookimpl
def pytest_generate_tests(metafunc):
# collect fixture names with values
option_values = {
"a": metafunc.config.option.A_list,
"b": metafunc.config.option.B_list,
"c": metafunc.config.option.C_list
}

# add parametrization for each fixture name
for name, value in option_values.items():
if name in metafunc.fixturenames and value is not None:
metafunc.parametrize(name, value.split(","))

def test_functionality1(a, b, c):
print(a, b, c)

这将按所需顺序创建参数化测试。

如果那不可能,您可以尝试在运行时创建您的装置(更多信息请参见 this post):

conftest.py

# define the functions used as fixtures
def a_list_fixture(request):
print(f"get_A_list: {request.param}")
yield request.param

def b_list_fixture(request, get_A_list):
print(f"get_B_list: {get_A_list}, {request.param}")
yield request.param

# at session start, create the fixtures with the correct params
def pytest_sessionstart(session):
a_list = session.config.option.A_list
if a_list:
a_list = a_list.split(",")
name = "get_A_list"
fn = pytest.fixture(scope='session', name=name,
params=a_list)(a_list_fixture)
setattr(sys.modules[__name__], "{}_func".format(name), fn)
b_list = session.config.option.B_list
if b_list:
b_list = b_list.split(",")
name = "get_B_list"
fn = pytest.fixture(scope='session', name=name,
params=b_list)(b_list_fixture)
setattr(sys.modules[__name__], "{}_func".format(name), fn)

请注意,您仍然需要像上面那样为 c 参数化函数:

测试.py

import pytest

@pytest.hookimpl
def pytest_generate_tests(metafunc):
c_value = metafunc.config.option.C_list
if "c" in metafunc.fixturenames and value is not None:
metafunc.parametrize("c", value.split(","))

def test_functionality(get_A_list, get_B_list, c):
print(f"test_functionality {get_A_list}, {get_B_list}, {c}")

这将创建所需的参数化测试:

pytest -vv --A_list="A1,A2"--B_list="B1"--C_list="C1,C2":

================================================= test session starts =================================================
...
collected 4 items

so/cmd_line_args/test.py::test_functionality[C1-A1-B1] PASSED [ 25%]
so/cmd_line_args/test.py::test_functionality[C2-A1-B1] PASSED [ 50%]
so/cmd_line_args/test.py::test_functionality[C1-A2-B1] PASSED [ 75%]
so/cmd_line_args/test.py::test_functionality[C2-A2-B1] PASSED [100%]

================================================== 4 passed in 0.28s ==================================================

请注意,参数顺序不符合要求,但这只是一个表面问题。以下是 print 函数的输出:

pytest -s --A_list="A1,A2"--B_list="B1"--C_list="C1,C2"

================================================= test session starts =================================================
...
collected 4 items

test.py get_A_list: A1
get_B_list: A1, B1
test_functionality A1, B1, C1
.test_functionality A1, B1, C2
.get_A_list: A2
get_B_list: A2, B1
test_functionality A2, B1, C1
.test_functionality A2, B1, C2
.

================================================== 4 passed in 0.27s ==================================================

关于python - 如何通过命令行将多个列表参数传递给多个pytest装置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61864930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com