pythonpytest

how to access index of the test in pytest.parametrize


These decorators make 6 tests:

@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3])
@pytest.mark.parametrize("z", [4, 5])

How inspect pytest to get current index or some ID of the test, inside the test body?


Solution

  • ok, I found it:

    @pytest.mark.parametrize("x", [pytest.param(0, id="first_test"), 1])
    @pytest.mark.parametrize("y", [2, 3])
    @pytest.mark.parametrize("z", [4, 5])
    def test_it(request, x, y, z):
        if request.node.nodename == "test_it[first_test]":
            # something special