(url, header, "auth")
)即可测试函数“ test_1” (url, header, "auth"), (url, header, "bad_request")
)来测试函数“ test_2” (url, header, "auth"), (url, header, "bad_request"), (url, header, "unauth"), (url + "/url_doesnt_exist", header, "invalid_request")
)传递给测试函数“ test_3” 当前每个测试用例将运行4次,因此总共将运行12个测试用例。但是我想要的是“ test_1”应该只运行一次,“ test_2”应该运行2次,“ test_3”应该运行4次,所以总共应该运行7个测试用例。
我知道我们可以按照以下方法使用参数化,但是在这里我们需要为每个测试用例定义测试数据。
是否有更好的方法将可变数量的参数传递给每个测试用例?
test_data = [
(url, header, "auth"),
(url, header, "bad_request"),
(url, header, "unauth"),
(url + "/url_doesnt_exist", header, "invalid_request")
]
@pytest.fixture(params=test_data)
def my_fixture(request):
return request.param
def test_1(my_fixture):
print(my_fixture[0])
print(my_fixture[1])
print(my_fixture[2])
def test_2(my_fixture):
print(my_fixture[0])
print(my_fixture[1])
print(my_fixture[2])
def test_3(my_fixture):
print(my_fixture[0])
print(my_fixture[1])
print(my_fixture[2])