mardi 6 avril 2021

How to parametrize a parameter in pytest?

Sometimes with pytest we start to parametrize a test function:

import pytest

@pytest.mark.parametrize("user", [WebUser(), BusinessUser(), AdminUser()])
def test_foo(user):
    ...

And then realize that some of the parameters would themselves have possible variants. Of course for simple concepts and short lists of parameters the simplest way to do this is to duplicate:

import pytest

@pytest.mark.parametrize("user", [WebUser("bob", age=12), WebUser("alice", age=56), BusinessUser("v1"), BusinessUser("v2"), AdminUser()])
def test_foo(user):
    ...

However this breaks tests readability quite fast when the number of variants gets big and very different for each "kind" in the first parameter. Is there a way to do this in a more elegant/modular way ?

Aucun commentaire:

Enregistrer un commentaire