pytest-kwparametrize
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!
Metadata
Release files for pytest-kwparametrize 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pytest_kwparametrize-0.0.3.tar.gz | 8.9 kB | Without provenance | Details for pytest_kwparametrize-0.0.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytest_kwparametrize-0.0.3-py3-none-any.whl | Python 3 | none | any | Details for pytest_kwparametrize-0.0.3-py3-none-any.whl |
Total release size: 16.0 kB
Release files / pytest_kwparametrize-0.0.3.tar.gz
| Download URL | pytest_kwparametrize-0.0.3.tar.gz |
|---|---|
| Size | 8.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
728731e418b526cedbd63c58b9c40848f089ef3fac32abfc45a512c0d70557b6
|
|
BLAKE2b-256 checksum How to use checksums |
45f3d46ba14711016f9d8a16360e6afe06a10e3193e2bed6c003c834b60967d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / pytest_kwparametrize-0.0.3-py3-none-any.whl
| Download URL | pytest_kwparametrize-0.0.3-py3-none-any.whl |
|---|---|
| Size | 7.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0a31377320cd23ff9399956940d6733df4eec8912153ac65f842f95a029d23cc
|
|
BLAKE2b-256 checksum How to use checksums |
d305c40f85d629ade14bbad9b00db0c081591ad92fd850f69435846662427326
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for pytest-kwparametrize 0.0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.