首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将排他性参数化与pytest合并的最佳方法

将排他性参数化与pytest合并的最佳方法
EN

Stack Overflow用户
提问于 2022-04-12 13:16:32
回答 3查看 91关注 0票数 0

这可能是重复的,但我总是想知道是否有一种“简单”的方法来减少多个设置的测试,其中一些设置与特定的参数组合完全相同。到目前为止,我还没有找到一个简单的方法。如何将以下测试用例与pytest结合起来?

代码语言:javascript
运行
复制
@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

我觉得我在重复自己的话。是否有办法减少合并每个测试用例的重复代码?

EN

回答 3

Stack Overflow用户

发布于 2022-04-12 13:27:19

您可以将前两个测试用例和最后两个测试用例组合起来,只需将它们添加到装饰器中的相同列表中即可。

代码语言:javascript
运行
复制
@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

医生:

https://docs.pytest.org/en/latest/historical-notes.html#applying-marks-to-pytest-mark-parametrize-parameters

票数 1
EN

Stack Overflow用户

发布于 2022-04-13 04:43:45

可以将credentialspathstatus作为参数发送到单个测试。

代码语言:javascript
运行
复制
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

参数将是

代码语言:javascript
运行
复制
A    ['/path_1'] 200
B    ['/path_1'] 200
A    ['/path_2'] 200
None ['/path_1'] 401
B    ['/path_2'] 401
None ['/path_2'] 401
票数 1
EN

Stack Overflow用户

发布于 2022-04-13 08:16:26

基于脓毒症病例,我发现了一个有趣的替代方案:

代码语言:javascript
运行
复制
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的参数化

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71843427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档