Skip to content

Using dict to provide ids for pytest.mark.parametrize #7977

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

Closed
pepoluan opened this issue Oct 31, 2020 · 1 comment
Closed

Using dict to provide ids for pytest.mark.parametrize #7977

pepoluan opened this issue Oct 31, 2020 · 1 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

Comments

@pepoluan
Copy link

As it is right now, to provide non-default test IDs when using pytest.mark.parametrize, one must use the ids keyword arg.

However, a thought occurred to me: What if we supply a dict to parametrize's argvalues, and from that dict, use the keys as the don-default test IDs?

For example, rather than:

    @pytest.mark.parametrize(
        "command", [
            "RCPT",
            "RCPT <[email protected]>",
            "RCPT TO:",
            "RCPT TO: <[email protected]> SIZE=1000",
        ],
        ids=["noarg", "noto", "noaddr", "params"]
    )

we can supply it more succinctly like so:

    @pytest.mark.parametrize(
        "command", {
            "noarg": "RCPT",
            "noto": "RCPT <[email protected]>",
            "noaddr": "RCPT TO:",
            "params": "RCPT TO: <[email protected]> SIZE=1000",
        }
    )

Users who absolutely need ordering can still use the first one, or use OrderedDict (or standard dict in Python>=3.7, where dict ordering became a language feature.)

@Zac-HD Zac-HD added 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 labels Oct 31, 2020
@The-Compiler
Copy link
Member

The-Compiler commented Oct 31, 2020

There isn't really an agreement on what dicts in parametrize should mean - see #7568 and related discussions.

Note that you can already use pytest.param:

    @pytest.mark.parametrize(
        "command", [
            pytest.param("RCPT", id="noarg"),
            pytest.param("RCPT <[email protected]>", id="noto"),
            pytest.param("RCPT TO:", id="noattr"),
            pytest.param("RCPT TO: <[email protected]> SIZE=1000", id="params"),
        ],
    )

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

3 participants