首页 > 解决方案 > 在 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

谢谢

标签: pythonpytest

解决方案


这肯定可以通过重写nodeid收集项目的 s 来实现。在下面的示例中,我在钩子nodeid的自定义 impl 中重写了 s。pytest_collection_modifyitems将以下代码放入您的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

推荐阅读