4

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

0

2 Answers 2

3

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
4
  • So beautifully powerful. Thanks for sharing and solving this issue. A minor improvement: adding a $ in the replacement pattern: item._nodeid = re.sub(r"\[.*?\]$", "_{:02d}".format(i + 1), item.nodeid) and lazy pattern (.*?). Does it actually improve speed?? Sep 27, 2019 at 2:29
  • Glad I could help! You can play with and benchmark the different regular expressions, but TBH I don't think it will be any significant difference in performance here, most of the time will be spent in collecting the test functions from files anyways.
    – hoefling
    Sep 27, 2019 at 7:52
  • Making the greedy quantifier more specific will surely bring some minor speed, and avoiding the regex compilation in the loop by placing a pattern = re.compile(...) outside the loop will also be an improvement, but I wouldn't expect great performance gains from those.
    – hoefling
    Sep 27, 2019 at 7:54
  • yes indeed, not great performance gain... and anyway, it's not what will slow down the whole process. Your solution works like a charm by the way. Sep 27, 2019 at 7:56
0

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/

1
  • 1
    Hi, thanks for the input but this is not what I am looking for. (I have also came across this nice page when doing my research for solution). I have currently a format like test_ids.py::test_foobar[1-b0] but I would like something like test_ids.py::test_foobar_01. Sep 26, 2019 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.