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?
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
If you don’t define id
explicitly then you could probably use:
current_input_id = int(request.node.name.split('[')[-1].split(']')[0][-1])
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?
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
If you don’t define id
explicitly then you could probably use:
current_input_id = int(request.node.name.split('[')[-1].split(']')[0][-1])