Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

pytest plugin: avoid repeating arguments in parametrize

Project description

This is an alternative to the rejected pull request #780 of pytest.

Installation

Just get it from PyPI:

python3 -m pip install pytest-auto-parametrize --user

Usage

This is an example for the usage of a parametrized test without using this plugin:

import pytest

testparams = [
    (1, 2, 3, 4, 5, 6, 7),
    (7, 6, 5, 4, 3, 2, 1),
]

@pytest.mark.parametrize('a, b, c, d, e, f, g', testparams)
def test_many_args(a, b, c, d, e, f, g):
    assert d == 4

The argument list has to be repeated, which is annoying.

By using this plugin, the repetition can be avoided:

import pytest

testparams = [
    (1, 2, 3, 4, 5, 6, 7),
    (7, 6, 5, 4, 3, 2, 1),
]

@pytest.auto_parametrize(testparams)
def test_many_args(a, b, c, d, e, f, g):
    assert d == 4

The auto-deduced parameters must be in the beginning of the parameter list, but any other parameters can be used afterwards, e.g. fixtures:

import pytest

testparams = [
    (1, 2, 3, 4, 5, 6, 7),
    (7, 6, 5, 4, 3, 2, 1),
]

@pytest.fixture
def myfixture():
    return 4

@pytest.auto_parametrize(testparams)
def test_many_args_and_fixture(a, b, c, d, e, f, g, myfixture):
    assert d - myfixture == 0

Limitations

Unlike @pytest.mark.parametrize(...) the decorator @pytest.auto_parametrize(...) cannot be used multiple times for the same test function. It can be used together with one or multiple instances of @pytest.mark.parametrize(...), though, as long as the “auto” arguments are in the beginning of the argument list.

Project details


Download files

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

Files for pytest-auto-parametrize, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size pytest_auto_parametrize-0.1.0-py2.py3-none-any.whl (4.9 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size pytest-auto-parametrize-0.1.0.tar.gz (2.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page