Skip to main content

Alternate syntax for @pytest.mark.parametrize with test cases as dictionaries and default value fallbacks

Project description

Alternate syntax for @pytest.mark.parametrize with test cases as dictionaries and default value fallbacks.

The problem

Tests parametrized using @pytest.mark.parametrize can easily become hard to read when the number of parameters grows large. For example:

@pytest.mark.parametrize(
    "a, b, c, d, e, f, expect",
    [
        (3, "one", 4.0, 0x01, 0o5, 9e0, 0b10,),
        (6, "five", 3.0, 0x05, 0o10, 9e0, 0b111,),
    ],
)
def test_my_func(a, b, c, d, e, f, expect):
    assert my_func(a, b, c, d, e, f) == expect

The solution

pytest-kwparametrize uses dictionaries instead of tuples for test cases. This way every parameter is always labeled and more easily identified by the reader. Also, test function parameters aren’t declared separately as with @pytest.mark.parametrize, and test cases don’t need to be enclosed in a list:

@pytest.mark.kwparametrize(
    dict(a=3, b="one", c=4.0, d=0x01, e=0o5, f=9e0, expect=0b10,),
    dict(a=6, b="five", c=3.0, d=0x05, e=0o10, f=9e0, expect=0b111,),
)
def test_my_func(a, b, c, d, e, f, expect):
    assert my_func(a, b, c, d, e, f) == expect

See examples below for additional features.

Examples

Basic syntax with no default values:

@pytest.mark.kwparametrize(
    dict(a=0, b=0, expect=0),
    dict(a=1, b=0, expect=1),
)
def test_my_func(a, b, expect):
    assert my_func(a, b) == expect

Defining a default value for a parameter so it can be omitted from test cases:

@pytest.mark.kwparametrize(
    dict(a=0, expect=0),
    dict(a=1, expect=1),
    dict(a=0, b=1, expect=0),
    dict(a=1, b=1, expect=2),
    b=0,
)
def test_my_func(a, b, expect):
    assert my_func(a, b) == expect

You can also provide the test cases as an iterable (e.g. list, tuple, generator) just as with @pytest.mark.parametrize:

@pytest.mark.kwparametrize(
    [
        dict(a=0, b=0, expect=0),
        dict(a=1, b=0, expect=1),
    ]
)
def test_my_func(a, b, expect):
    assert my_func(a, b) == expect

Default values can also be paassed as a dictionary using the defaults= keyword argument (here all parameters have a default):

@pytest.mark.kwparametrize(
    dict(),
    dict(a=1, expect=1),
    dict(b=1),
    dict(a=1, b=1, expect=2),
    defaults=dict(a=0, b=0, expect=0),
)
def test_my_func(a, b, expect):
    assert my_func(a, b) == expect

The marker works with fixtures and Pytest’s built-in keyword arguments:

@pytest.mark.kwparametrize(
    # test cases:
    dict(),
    dict(filename="special.txt", expect=1),
    dict(content="special content"),

    # default parameter values:
    filename="dummy.txt",
    content="dummy content",
    expect=42,

    # example of a Pytest built-in keyword argument:
    ids=["with defaults", "special filename", "speial content"],
)
def test_my_func(tmpdir, filename, content, expect):
    filepath = (tmpdir / filename)
    filepath.write(content)
    assert my_func(filepath) == expect

Contributors ✨

See README.rst for the list of contributors.

This project follows the all-contributors specification. Contributions of any kind are welcome!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_kwparametrize-0.0.3.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

pytest_kwparametrize-0.0.3-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest_kwparametrize-0.0.3.tar.gz.

File metadata

  • Download URL: pytest_kwparametrize-0.0.3.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.0.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.7

File hashes

Hashes for pytest_kwparametrize-0.0.3.tar.gz
Algorithm Hash digest
SHA256 728731e418b526cedbd63c58b9c40848f089ef3fac32abfc45a512c0d70557b6
MD5 74fcf3aa49e174c5a79aeefd55125359
BLAKE2b-256 45f3d46ba14711016f9d8a16360e6afe06a10e3193e2bed6c003c834b60967d2

See more details on using hashes here.

File details

Details for the file pytest_kwparametrize-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: pytest_kwparametrize-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.0.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.7

File hashes

Hashes for pytest_kwparametrize-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0a31377320cd23ff9399956940d6733df4eec8912153ac65f842f95a029d23cc
MD5 420686ff6d80a9ef0ec4e5386b3f5926
BLAKE2b-256 d305c40f85d629ade14bbad9b00db0c081591ad92fd850f69435846662427326

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page