[Python] Pytest text annotation for test with tuple of parameters

Discussão em 'Python' iniciado por Stack, Abril 5, 2025.

  1. Stack

    Stack Membro Participativo

    I'm looking for more elegant solution for this kind of problem:

    def ids(x):
    if isinstance(x, int):
    return str(x)
    elif isinstance(x, str):
    return x[0]


    @pytest.mark.parametrize("number, string",
    [
    (1, "One"),
    (2, "Two"),
    (3, "Three")
    ],
    ids=ids)
    def test_stupid(number, string):
    assert 0 == 1


    This code will produce test names: '1-O', '2-T', '3-T'.

    The problem is that pytest is using same function for all arguments in tuple and I have to deal with ugly isinstance calls.

    Is there a better way to solve this problem?

    Continue reading...

Compartilhe esta Página