这可能是重复的,但我总是想知道是否有一种“简单”的方法来减少多个设置的测试,其中一些设置与特定的参数组合完全相同。到目前为止,我还没有找到一个简单的方法。如何将以下测试用例与pytest结合起来?
@pytest.mark.parametrize("credentials", ["A", "B"])
@pytest.mark.parametrize("path", ["/path_1"])
def test_is_authorized_1(client, path, headers):
response = client.get(path, headers=headers)
assert response.status_code == 200
@pytest.mark.parametrize("credentials", ["A"])
@pytest.mark.parametrize("path", ["/path_2"])
def test_is_authorized_2(client, path, headers):
response = client.get(path, headers=headers)
assert response.status_code == 200
@pytest.mark.parametrize("credentials", [None])
@pytest.mark.parametrize("path", ["/path_1"])
def test_not_authorized_1(client, path, headers):
response = client.get(path, headers=headers)
assert response.status_code == 401
@pytest.mark.parametrize("credentials", ["B", None])
@pytest.mark.parametrize("path", ["/path_2"])
def test_not_authorized_2(client, path, headers):
response = client.get(path, headers=headers)
assert response.status_code == 401我觉得我在重复自己的话。是否有办法减少合并每个测试用例的重复代码?
发布于 2022-04-12 13:27:19
您可以将前两个测试用例和最后两个测试用例组合起来,只需将它们添加到装饰器中的相同列表中即可。
@pytest.mark.parametrize("credentials", ["A", "B"])
@pytest.mark.parametrize("path", ["/path_1", "/path_2"])
def test_is_authorized_1(client, path, headers):
response = client.get(path, headers=headers)
assert response.status_code == 200
@pytest.mark.parametrize("credentials", [None, "B"])
@pytest.mark.parametrize("path", ["/path_1", "/path_2"])
def test_not_authorized_1(client, path, headers):
response = client.get(path, headers=headers)
assert response.status_code == 401医生:
发布于 2022-04-13 04:43:45
可以将credentials、path和status作为参数发送到单个测试。
def data_source():
credentials = [["A", "B"], ["A"], [None], ["B", None]]
paths = [["/path_1"], ["/path_2"], ["/path_1"], ["/path_2"]]
statuses = [200, 200, 401, 401]
for data in zip(credentials, paths, statuses):
for creds in data[0]:
yield creds, *data[1:]
@pytest.mark.parametrize(['credentials', 'path', 'status'], data_source())
def test_is_authorized(client, credentials, path, status, headers):
response = client.get(path, headers=headers)
assert response.status_code == status参数将是
A ['/path_1'] 200
B ['/path_1'] 200
A ['/path_2'] 200
None ['/path_1'] 401
B ['/path_2'] 401
None ['/path_2'] 401发布于 2022-04-13 08:16:26
基于脓毒症病例,我发现了一个有趣的替代方案:
from pytest_cases import parametrize, parametrize_with_cases
class AuthorizedCases:
@parametrize("headers", ["heads_A", "heads_B"])
@parametrize("path", ["/path_1"])
def case_path1(self, path, headers):
return path, headers
@parametrize("headers", ["heads_A"])
@parametrize("path", ["/path_2"])
def case_path2(self, path, headers):
return path, headers
class UauthorizedCases:
@parametrize("headers", [None])
@parametrize("path", ["/path_1"])
def case_path1(self, path, headers):
return path, headers
@parametrize("headers", ["heads_B", None])
@parametrize("path", ["/path_2"])
def case_path2(self, path, headers):
return path, headers
@parametrize_with_cases("path, headers", cases=AuthorizedCases)
def test_is_authorized(client, path, headers):
response = client.get(path, headers=headers)
assert response.status_code == 200
@parametrize_with_cases("path, headers", cases=UnauthorizedCases)
def test_not_authorized(client, path, headers):
response = client.get(path, headers=headers)
assert response.status_code == 401它比问题代码长,但我认为它对于复杂的情况有更大的灵活性。这似乎与在pytest中称为“协变参数化”有关。参见github上的基于另一个参数#4050的参数化。
https://stackoverflow.com/questions/71843427
复制相似问题