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

Add support for dictionary argvalues for parametrize #5487

Closed
kevinjfoley opened this issue Jun 25, 2019 · 3 comments
Closed

Add support for dictionary argvalues for parametrize #5487

kevinjfoley opened this issue Jun 25, 2019 · 3 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

@kevinjfoley
Copy link
Contributor

I'd like to be able to pass a dictionary to pytest.mark.parametrize as argvalues with the keys as ids and the values as argvalues

For example:

import pytest


@pytest.mark.parametrize(["a", "b"], {"test 1": (1, 2), "test 2": (3, 4)})
def test_foo(a, b):

    assert a + 1 == b

# instead of
@pytest.mark.parametrize(["a", "b"], [(1, 2), (3, 4)], ids=["test 1", "test 2"])
def test_foo(a, b):

    assert a + 1 == b

would generate two tests named "test 1" and "test 2".

Before trying to add this feature I wanted to make sure there's no foreseen issues with a making change like this.

@kevinjfoley kevinjfoley changed the title Support for Dictionary argvalues for parametrize Add support for dictionary argvalues for parametrize Jun 25, 2019
@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 Jun 25, 2019
@asottile
Copy link
Member

I'm -1

You can already do this which satisfies the same thing without creating more edge cases:

@pytest.mark.parametrize(["a", "b"], (pytest.param(1, 2, id='test 1'), pytest.param(3, 4, id='test 2')))

@kevinjfoley
Copy link
Contributor Author

kevinjfoley commented Jun 25, 2019

I completely missed that in the guide, using pytest.param does what I'm looking for. Thanks!

@blueyed
Copy link
Contributor

blueyed commented Jul 16, 2019

I think using pytest.param is not really straight-forward, and less readable in general:

Currently:

@pytest.mark.parametrize(
    ["a", "b"], (
        pytest.param(1, 2, id='test 1'),
        pytest.param(3, 4, id='test 2'),
    )
)

New:

@pytest.mark.parametrize(
    ["a", "b"], {
        'test 1': (1, 2),
        'test 2': (3, 4),
    }
)

A workaround is of course to use a dict in the first place, and then use that,
but it would be better to not have this redirection also:

@pytest.mark.parametrize("a,b", t.values(), ids=list(t.keys())

It feels pretty natural to use a dict here, since the ID is supposed to be unique.

For reference: I've created https://github.com/blueyed/pytest-enhancements, which includes this.

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

4 participants