Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.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会在测试报告中打印参数值。例如: @fixture(params=['hello', 'world'] def data(request): return request.param def test_something(data): pass 使用py.test--verbose运行此命令将打印如下内容: test_example.py:7: test_something[hello] PASSED test_example.py:

当使用内置类型作为夹具参数时,pytest会在测试报告中打印参数值。例如:

@fixture(params=['hello', 'world']
def data(request):
    return request.param

def test_something(data):
    pass
使用
py.test--verbose
运行此命令将打印如下内容:

test_example.py:7: test_something[hello]
PASSED
test_example.py:7: test_something[world]
PASSED
请注意,参数值打印在测试名称后的方括号中

现在,当使用用户定义类的对象作为参数时,如下所示:

class Param(object):
    def __init__(self, text):
        self.text = text

@fixture(params=[Param('hello'), Param('world')]
def data(request):
    return request.param

def test_something(data):
    pass
pytest将简单地枚举值的数量(
p0
p1
,等等):

即使用户定义的类提供自定义的
\uuuu str\uuu
\uuu repr\uuu
实现,此行为也不会改变。有没有办法让pytest显示比这里的
p0
更有用的东西


我在Windows 7上的Python 2.7.6上使用pytest 2.5.2。

fixture decorator采用
ids
参数,该参数可用于覆盖自动参数名称:

@fixture(params=[Param('hello'), Param('world')], ids=['hello', 'world'])
def data(request):
    return request.param

如图所示,参数列表中的相应项需要一个名称列表。

是否有一种方法可以根据参数以受控方式生成ID?例如,我希望将3个“person”对象作为参数传递,但我希望根据person.name甚至person['name']命名ID?可以将函数传递给
ids
参数,该参数将使用fixture值调用,并可以为自动id返回字符串或
None
。这记录在repo中的doc/en/fixture.txt中,但由于某些原因,目前未出现在网站的呈现版本中,这很奇怪。我也能看到,也许是即将推出的功能?我尝试了pytest 2.6.4中的示例,得到了:———pytest\python.py:824:in参数化--if-id和len(ids)!=len(argvalues):TypeError:类型为“function”的对象没有len()
@fixture(params=[Param('hello'), Param('world')], ids=['hello', 'world'])
def data(request):
    return request.param