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

Improve @parametrize: accept a dict for argvalues so that the id can be passed as the key #261

Open
smarie opened this issue Feb 22, 2022 · 1 comment

Comments

@smarie
Copy link
Owner

smarie commented Feb 22, 2022

@pytest.mark.parametrize("a", [0, 2], ids=["Null", "Positive"])

could become

@parametrize("a", dict(Null=0, Positive=2)

@smarie
Copy link
Owner Author

smarie commented Feb 22, 2022

This pytest example

@pytest.mark.parametrize(
    "a,b,expected",
    [
        pytest.param(
            datetime(2001, 12, 12), datetime(2001, 12, 11), timedelta(1), id="forward"
        ),
        pytest.param(
            datetime(2001, 12, 11), datetime(2001, 12, 12), timedelta(-1), id="backward"
        ),
    ],
)
def test_timedistance_v3(a, b, expected):
    diff = a - b
    assert diff == expected

would write:

@parametrize(
    "a,b,expected",
    dict(
        forward=(datetime(2001, 12, 12), datetime(2001, 12, 11), timedelta(1)),
        backward=(datetime(2001, 12, 11), datetime(2001, 12, 12), timedelta(-1)),
    ),
)
def test_timedistance_v3(a, b, expected):
    diff = a - b
    assert diff == expected

It seems more readable to me as the id is presented first. Of course it is still less readable than having two case functions, but probably faster to write/maintain for some tests.

See https://docs.pytest.org/en/6.2.x/example/parametrize.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant