Skip to content

pytest.mark.parametrize: parameter=value pairs in test IDs #13055

Open
@Bastian-Krause

Description

@Bastian-Krause

What's the problem this feature will solve?

I have a lot of parametrized tests. By default, only the parameter's values make in into the test IDs. The parameter names don't. Since especially bool and int parameters do not speak for themselves (certain strings might not, too), the test function + test ID are not descriptive/expressive.

Describe the solution you'd like

I think it would be beneficial to allow parameter=value pairs in the test ID (optionally) to get an idea what parameters a test gets passed.

The test IDs usually look like this:

test_something[100-10-True-False-True]

While the True/False values could potentially be turned into some other data type with a proper __str__() method (enums, ..), I think having the option to show key=value instead of only the value would be useful to easily see what parameters a test gets passed, e.g.:

test_something[speed_down=100-speed_up=10-foo=True-bar=False-baz=True]

The "-" separator does not look natural with this approach, maybe it could also be configurable? But I think that part is less important here.

I imagine something like pytest.mark.parametrize(.., id_names=True) could enable such behavior.

Alternative Solutions

For the time being, I'm using a wrapper around pytest.mark.parametrize():

def parametrize(argnames, argvalues, **kwargs):
    argnames_consumable = argnames.split(",") * len(argvalues)

    def idfn(value):
        argname = argnames_consumable.pop(0)
        return f"{argname}={value}"

    if "ids" not in kwargs:
        kwargs["ids"] = idfn

    return pytest.mark.parametrize(argnames, argvalues, **kwargs)                                  

This gives me the parameter=value test IDs as described above (if no ids kwarg is given). While I don't consider this a too ugly hack, a solution for this directly in pytest.mark.parametrize() might also be useful to other users.

Additional context

https://docs.pytest.org/en/latest/reference/reference.html#pytest-mark-parametrize
https://docs.pytest.org/en/latest/reference/reference.html#pytest.Metafunc.parametrize

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: parametrizerelated to @pytest.mark.parametrizetype: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions