我想从命令行将以下列表传递到我的pytest代码中(类似于Python中的argparser),有人可以帮我如何做到这一点吗?
pytest -vs test_sample.py --html=results.html --A_list=['A1', 'A2'] --B_list=['B1'] --C_list=['C1', 'C2']
这是我的pytest代码。
这是我的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]
我需要在fixture get_A_list中使用A_list元素执行某些操作...同样,我需要在fixture get_B_list中使用A_list和B_list元素执行某些操作......这样,我有4个fixtures,最后我需要在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