Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pytest中重命名参数化测试_Python_Pytest - Fatal编程技术网

Python 在pytest中重命名参数化测试

Python 在pytest中重命名参数化测试,python,pytest,Python,Pytest,Pytest中的参数化测试具有以下id格式: [] 当这些参数化时,我希望能够完全控制测试用例的名称 例如,我目前有以下代码: 导入pytest 列表参数=[“a”、“b”、“c”] @pytest.fixture(参数=列表参数) def程序参数(请求): 屈服请求参数 def测试_001(): #这一点不应改变 通过 def测试_002(程序参数): #这应该是测试_002_01,测试_002_02。。。 打印(程序参数) ids=[f“test_003_{i+1:02d}”表示范围内的i(

Pytest中的参数化测试具有以下id格式:
[]

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

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

导入pytest
列表参数=[“a”、“b”、“c”]
@pytest.fixture(参数=列表参数)
def程序参数(请求):
屈服请求参数
def测试_001():
#这一点不应改变
通过
def测试_002(程序参数):
#这应该是测试_002_01,测试_002_02。。。
打印(程序参数)
ids=[f“test_003_{i+1:02d}”表示范围内的i(len(list_args))]
@pytest.mark.parametrize(“arg”,list_args,ids=ids)
def测试_003(arg):
#这应该是测试_003_01,测试_003_02。。。
打印(程序参数)
当我运行(pytest 5.1.3)时,我有:

我想要的是:

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
是否可以不对请求
对象进行太多的黑客攻击(或者在以后的
pytest
更新中可能被破坏的其他修改)


谢谢

根据pytest中提供的文档,我想通知您,ids在pytest.mark.paramatize中的工作方式与您在问题中提到的输出方式相同

格式为:- 文件名testname idsvalue


参考:-

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

您好,谢谢您的输入,但这不是我要找的。(我在研究解决方案时也看到了这个不错的页面)。我目前有一个类似
test\u ids.py::test\u foobar[1-b0]的格式
但是我想要像
test\u id.py::test\u foobar\u 01这样功能强大的东西。感谢分享和解决这个问题。一个小的改进:在替换模式中添加一个
$
item.\u nodeid=re.sub(r“\[.\\]$”、“{:02d}.format(I+1)、item.nodeid)
和惰性模式(
)。它真的提高了速度吗?很高兴我能帮上忙!你可以使用不同的正则表达式并对其进行基准测试,但我不认为这在性能上会有任何显著的差异,不管怎样,大部分时间都会花在从文件中收集测试函数上。使贪婪的量词更加具体肯定会带来一些好处较小的速度,并通过放置
pattern=re.compile(…)避免循环中的正则表达式编译
外循环也将是一种改进,但我不希望从中获得巨大的性能提升。是的,的确,性能提升不太大……而且无论如何,这不会减慢整个过程。顺便说一句,您的解决方案很有魅力。
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