Skip to content

Allow list of dictionaries for @pytest.mark.parametrize #7568

Description

@dbalabka

Right now it is only allowed list of tuples to pass into @pytest.mark.parametrize decorator:

import pytest

from datetime import datetime, timedelta

testdata = [
    (datetime(2001, 12, 12), datetime(2001, 12, 11), timedelta(1)),
    (datetime(2001, 12, 11), datetime(2001, 12, 12), timedelta(-1)),
]


@pytest.mark.parametrize("a,b,expected", testdata)
def test_timedistance_v0(a, b, expected):
    diff = a - b
    assert diff == expected

The large test data is complicated to manage testdata and it became less readable. To overcome this issue I proposing pass testdata as a list of dictionaries and keep arguments names only in testdata:

import pytest

from datetime import datetime, timedelta

testdata = [
    {
      'a': datetime(2001, 12, 12),
      'b': datetime(2001, 12, 11), 
      'expected': timedelta(1),
    },
    {
      'a': datetime(2001, 12, 11),
      'b': datetime(2001, 12, 12), 
      'expected': timedelta(-1),
    },
]


@pytest.mark.parametrize(testdata)
def test_timedistance_v0(a, b, expected):
    diff = a - b
    assert diff == 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