python - 在 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
收集项目的 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
推荐阅读
- javascript - 如果语句不适用于 classList 添加
- java - 在多个容器中存储数据时,Java 的参考系统是否有助于节省内存?
- javascript - 如何编辑一个导航/菜单文档并更改所有博客页面中的元素?
- c# - 如何在 .NET 框架 4.6 中注册 JS 引擎 - reactjs.net - 服务器错误 - Microsoft.ClearScript.ScriptEngineException?
- cuda - 在 Jetson TX2 上构建和运行的 Cuda 应用程序可以在 Jetson Xavier 上运行吗?
- mysql - 自动删除 SQL 行中的重复行或 IP
- dart - Flutter - 从另一个类中调用持久性底页
- php - Yii2:添加本地 nginx 代理以请求可信主机
- c# - 无法在c#.net中使用文件读写
- java - rxJava 有序(按键)任务执行