Skip to content

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

Closed
@pepoluan

Description

@pepoluan

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.)

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