pytest: Duplicated parameters in @pytest.mark.parametrize

What’s the problem this feature will solve?

Make unambigious what the values of the parameters used from parametrize are.

Describe the solution you’d like

When duplicated values are used in parametrize, pytest adds a number after each duplicated value, starting from 0 and this confuses what the actual value of parameter is.

Example:

@pytest.mark.parametrize("a", [1, 2, 10, 11, 2, 1, 12, 11]
def test_params(a):
    print('a:', a)
    assert a > 0

This result in following output from pytest:

test_a_1.py::test_params[10] PASSED                    [ 12%]
test_a_1.py::test_params[20] PASSED                    [ 25%]
test_a_1.py::test_params[10] PASSED                    [ 25%]
test_a_1.py::test_params[110] PASSED                   [ 37%]
test_a_1.py::test_params[21] PASSED                    [ 50%]
test_a_1.py::test_params[11] PASSED                    [ 62%]
test_a_1.py::test_params[12] PASSED                    [ 75%]
test_a_1.py::test_params[111] PASSED                   [ 87%]

It is clear that a has the value 1 the very first time and not 10, so confusing with the real 10 later. Pytest adds a sequential nbr immediately after each duplicated value and that is very confusing

Alternative Solutions

Do not do this and leave the values as they are, so I would like following output:

1, 2, 10, 11, 2, 1, 12, 11 as in parametrize list

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Comments: 19 (18 by maintainers)

Most upvoted comments

My suggestion:

  • Continue to append numbers rather than letters, because some data-driven parametrizations are very large
  • If the id ends in a digit, make then new id f"{id}_{suffix}". If the new id is already in the set of seen ids, increment the suffix number and try again. This is sure to work, and even be fast in practice, because our maximum suffix is the number of duplicate ids we started with.

Please go ahead, it’s a nice size for a contribution and having an Implementation helps to find concrete issues that hide in plain sight while just discussing the idea