简体   繁体   中英

Inject own name for test method into pytest output

Normally pytest output for parametrized test method - test_bar of test class TestFoo looks like

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

is it possible to inject own name based on parameter of TestFoo instance?

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

Where own-generic-name is str(self.baz) of TestFoo instance.

You can use the ids keyword to pass a list of strings to parametrize in order to customize your test ids:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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