简体   繁体   中英

how to set the pytest.param id automatically?

I have a parametrized test:

import pytest
@pytest.mark.parametrize('args', [
    [1,2,3],
    ['a'],
    [[],[1],['a']],
])
def test_bla(args):
    assert all(args)

When running it, I get the following output:

platform linux -- Python 3.6.12, pytest-6.0.2, py-1.9.0, pluggy-0.13.1 -- /home/bla/venv/bin/python
cachedir: .pytest_cache
metadata: {'Python': '3.6.12', 'Platform': 'Linux-4.15.0-132-generic-x86_64-with-Ubuntu-16.04-xenial', 'Packages': {'pytest': '6.0.2', 'py': '1.9.0', 'pluggy': '0.13.1'}, 'Plugins': {'metadata': '1.10.0'}}
rootdir: ...
plugins: metadata-1.10.0
collected 3 items                                                                                                                                                                                              

my_test.py::test_bla[args0] PASSED
my_test.py::test_bla[args1] PASSED
my_test.py::test_bla[args2] FAILED    <---------- Note that this is not very descriptive :(

=================================================================================================== FAILURES ===================================================================================================
_______________________________________________________________________________________________ test_bla[args2] ________________________________________________________________________________________________

args = [[], [1], ['a']]

    @pytest.mark.parametrize('args', [
        [1,2,3],
        ['a'],
        [[],[1],['a']],
    ])
    def test_bla(args):
>       assert all(args)
E       AssertionError: assert False
E        +  where False = all([[], [1], ['a']])

my_test.py:8: AssertionError
=========================================================================================== short test summary info ============================================================================================
FAILED my_test.py::test_bla[args2] - AssertionError: assert False
========================================================================================= 1 failed, 2 passed in 0.11s ==========================================================================================

What I want, is to get in the output the actual args used, eg:

my_test.py::test_bla[args0] PASSED
my_test.py::test_bla[args1] PASSED
my_test.py::test_bla[[], [1], ["a"]] FAILED    <---------- Note the change here

This I got by editing the parameterization :

pytest.param([[],[1],['a']], id='[], [1], ["a"]'),

Is there a way for pytest to just take the parameter and make it's id = str(param) ? (basically automating what I just edited myself in the test)

You can pass an ID function to the ids parameter of the @pytest.mark.parametrize decorator:

import pytest

@pytest.mark.parametrize('args', [
    [1,2,3],
    ['a'],
    [[],[1],['a']],
], ids=str)
def test_bla(args):
    assert all(args)

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