Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Bastian-Krause opened this issue Dec 12, 2024 · 0 comments
Open

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

Bastian-Krause opened this issue Dec 12, 2024 · 0 comments
Labels
topic: parametrize related to @pytest.mark.parametrize type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature

Comments

@Bastian-Krause
Copy link

Bastian-Krause commented Dec 12, 2024

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

@Zac-HD Zac-HD added type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature topic: parametrize related to @pytest.mark.parametrize labels Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: parametrize related to @pytest.mark.parametrize type: proposal proposal for a new feature, often to gather opinions or design the API around the new feature
Projects
None yet
Development

No branches or pull requests

2 participants