繁体   English   中英

重命名 pytest 中的参数化测试

[英]Renaming parametrized tests in pytest

Pytest 中的参数化测试具有以下 id 格式: <function name>[<param identifier>]

当这些参数化时,我希望能够完全控制测试用例的名称。

例如,我目前有以下代码:

import pytest

list_args = ["a", "b", "c"]


@pytest.fixture(params=list_args)
def prog_arg(request):
    yield request.param


def test_001():
    # This should not be changed
    pass

def test_002(prog_arg):
    # This should be test_002_01, test_002_02, ...
    print(prog_arg)


ids = [f"test_003_{i+1:02d}" for i in range(len(list_args))]


@pytest.mark.parametrize("arg", list_args, ids=ids)
def test_003(arg):
    # This should be test_003_01, test_003_02, ...
    print(prog_arg)

当我运行( pytest 5.1.3 )时,我有:

test_rename_id.py::test_TC_001 PASSED
test_rename_id.py::test_TC_002[a] PASSED
test_rename_id.py::test_TC_002[b] PASSED
test_rename_id.py::test_TC_002[c] PASSED
test_rename_id.py::test_TC_003[test_003_01] PASSED                                                                                                                                                   
test_rename_id.py::test_TC_003[test_003_02] PASSED                                                                                                                                                  
test_rename_id.py::test_TC_003[test_003_03] PASSED

我想要的是:

test_rename_id.py::test_TC_001 PASSED
test_rename_id.py::test_TC_002_01 PASSED
test_rename_id.py::test_TC_002_02 PASSED
test_rename_id.py::test_TC_002_03 PASSED
test_rename_id.py::test_TC_003_01 PASSED                                                                                                                                                   
test_rename_id.py::test_TC_003_02 PASSED                                                                                                                                                  
test_rename_id.py::test_TC_003_03 PASSED

是否有可能没有太多的请求object (或其他可能在pytest的未来更新中被破坏的修改?

谢谢

这肯定可以通过重写收集项目的nodeid来实现。 在下面的示例中,我在pytest_collection_modifyitems挂钩的自定义 impl 中重写了nodeid 将以下代码放入您的conftest.py

# conftest.py

import itertools as it
import re


def grouper(item):
    return item.nodeid[:item.nodeid.rfind('[')]


def pytest_collection_modifyitems(items):
    for _, group in it.groupby(items, grouper):
        for i, item in enumerate(group):
            item._nodeid = re.sub(r'\[.*\]', '_{:02d}'.format(i + 1), item.nodeid)

现在从问题中运行您的测试模块会产生:

test_spam.py::test_001 PASSED
test_spam.py::test_002_01 PASSED
test_spam.py::test_002_02 PASSED
test_spam.py::test_002_03 PASSED
test_spam.py::test_003_01 PASSED
test_spam.py::test_003_02 PASSED
test_spam.py::test_003_03 PASSED

根据 pytest 中提供的文档,我想告诉您, ID在 pytest.mark.paramterize 中的工作方式就像您在 DB38 中提到的 Z78E6221F6393D1356681FZCE39 一样。

格式为:- filename-testname-idsvalue

参考:- https://hackebrot.github.io/pytest-tricks/param_id_func/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM