Jean-Francois T.
Jean-Francois T.

Reputation: 12920

Renaming parametrized tests in pytest

Parametrized tests in Pytest have the following id format: <function name>[<param identifier>].

I would like to be able to totally control the name of the Test Case when these are parametrized.

For example, I currently have the following code:

import pytest

list_args = ["a", "b", "c"]


@pytest.fixture(params=list_args)
def prog_arg(request):
    yield request.param


def test_001():
    # This should not be changed
    pass

def test_002(prog_arg):
    # This should be test_002_01, test_002_02, ...
    print(prog_arg)


ids = [f"test_003_{i+1:02d}" for i in range(len(list_args))]


@pytest.mark.parametrize("arg", list_args, ids=ids)
def test_003(arg):
    # This should be test_003_01, test_003_02, ...
    print(prog_arg)

When I run (pytest 5.1.3), I have:

test_rename_id.py::test_TC_001 PASSED
test_rename_id.py::test_TC_002[a] PASSED
test_rename_id.py::test_TC_002[b] PASSED
test_rename_id.py::test_TC_002[c] PASSED
test_rename_id.py::test_TC_003[test_003_01] PASSED                                                                                                                                                   
test_rename_id.py::test_TC_003[test_003_02] PASSED                                                                                                                                                  
test_rename_id.py::test_TC_003[test_003_03] PASSED

What I would like is to have:

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

Is it possible without too much hacking of the request object (or other modifications that might got broken in future updates of pytest?

Thanks

Upvotes: 4

Views: 1426

Answers (2)

hoefling
hoefling

Reputation: 66231

This is surely possible by rewriting the nodeids of the collected items. In the example below, I rewrite nodeids in a custom impl of the pytest_collection_modifyitems hook. Place the following code into your 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)

Running your test module from the question now yields:

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

Upvotes: 3

nikhilesh_koshti
nikhilesh_koshti

Reputation: 403

As per the docs available in the pytest, I would like to apprise you that the way ids work in the pytest.mark.paramterize is just like the output you mentioned in your question.

The format is:- filename-testname-idsvalue.

Reference:- https://hackebrot.github.io/pytest-tricks/param_id_func/

Upvotes: 0

Related Questions