简体   繁体   English

将自己的测试方法名称注入pytest输出

[英]Inject own name for test method into pytest output

Normally pytest output for parametrized test method - test_bar of test class TestFoo looks like 对于参数化测试方法通常pytest输出- test_bar测试类的TestFoo样子

path/to/test_file.py:67: TestFoo.test_bar[param1] FAILED

is it possible to inject own name based on parameter of TestFoo instance? 是否可以根据TestFoo实例的参数注入自己的名称?

path/to/test_file.py:67: TestFoo.test_bar[own-generic-name] FAILED

Where own-generic-name is str(self.baz) of TestFoo instance. 其中own-generic-nameTestFoo实例的str(self.baz)

You can use the ids keyword to pass a list of strings to parametrize in order to customize your test ids: 您可以使用ids关键字传递要parametrize的字符串列表,以自定义测试ID:

import pytest

@pytest.mark.parametrize('i', [1, 2], ids=['param1', 'param2'])
def test_foo(i):
    pass  

Generates this output: 生成此输出:

test_foo.py:3: test_foo[param1] PASSED
test_foo.py:3: test_foo[param2] PASSED

Customizing the generated ids by using a callable is being worked on here and looks like it will be merged soon. 通过使用可调用自定义生成的ID被制作在这里 ,看起来像它即将合并。

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

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