Skip to content

Support keywords or annotations in parametrize. #2855

Description

@coady

Proposal to enhance parametrize with support for keyword params. This seems more pythonic than the zipped format, for simple params. Reference implementation (under a different name):

def params(**kwargs):
    if len(kwargs) == 1:
        return pytest.mark.parametrize(*kwargs.popitem())
    return pytest.mark.parametrize(','.join(kwargs), zip(*kwargs.values()))

An example from the docs:

@pytest.mark.parametrize("test_input,expected", [
    ("3+5", 8),
    ("2+4", 6),
])
def test_eval(test_input, expected):
    assert eval(test_input) == expected

becomes:

@params(test_input=["3+5", "2+4"], expected=[8, 6])
def test_eval(test_input, expected):
    assert eval(test_input) == expected

Relatedly, it also seems a perfect use case for Python 3 annotations:

def parametrized(func):
    return params(**func.__annotations__)(func)

@parametrized
def test_eval(test_input: ["3+5", "2+4"], expected: [8, 6]):
    assert eval(test_input) == expected

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

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions