how to access index of the test in pytest.parametrize

Question:

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?

Asked By: Sławomir Lenart

||

Answers:

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 
Answered By: Sławomir Lenart

If you don’t define id explicitly then you could probably use:

current_input_id = int(request.node.name.split('[')[-1].split(']')[0][-1])
Answered By: Numan Ibn Mazid
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.