如何在pytest.parametrize中访问测试的索引

时间:2018-10-19 15:22:19

标签: python pytest

这些装饰器进行6个测试:

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

如何在测试体内检查pytest以获得当前索引或测试的某些ID?

1 个答案:

答案 0 :(得分:0)

好的,我找到了:

@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