An alternative way to parametrize test cases
Project description
pytest-funparam makes it easy to write parametrized tests.
Installation
You can install “pytest-funparam” via pip from PyPI:
$ pip install pytest-funparam
Usage
Inside a test function, decorate a function with the funparam fixture:
def test_addition(funparam):
@funparam
def verify_sum(a, b, expected):
assert a + b == expected
verify_sum(1, 2, 3)
verify_sum(2, 2, 5) # OOPS!
verify_sum(4, 2, 6)
And run pytest:
$ pytest ============================= test session starts ============================== collected 3 items test_readme.py .F. [100%] =================================== FAILURES =================================== _______________________________ test_addition[1] _______________________________ def test_addition(funparam): @funparam def verify_sum(a, b, expected): assert a + b == expected verify_sum(1, 2, 3) > verify_sum(2, 2, 5) # OOPS! test_readme.py:7: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ a = 2, b = 2, expected = 5 @funparam def verify_sum(a, b, expected): > assert a + b == expected E assert (2 + 2) == 5 test_readme.py:4: AssertionError ========================= 1 failed, 2 passed in 0.03s ==========================
The test_addition test case was split into 3 tests, one for each verify_sum call.
Because funparam is parametrizing the test calls, it even works with commands like pytest --last-failed:
$ pytest --last-failed ============================= test session starts ============================== collected 1 item test_readme.py F [100%] =================================== FAILURES =================================== _______________________________ test_addition[1] _______________________________ def test_addition(funparam): @funparam def verify_sum(a, b, expected): assert a + b == expected verify_sum(1, 2, 3) > verify_sum(2, 2, 5) # OOPS! test_readme.py:7: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ a = 2, b = 2, expected = 5 @funparam def verify_sum(a, b, expected): > assert a + b == expected E assert (2 + 2) == 5 test_readme.py:4: AssertionError ============================== 1 failed in 0.01s ===============================
Mark tests by using the _marks keyword argument on calls to verify:
import pytest
def test_addition(funparam):
@funparam
def verify_sum(a, b, expected):
assert a + b == expected
verify_sum(1, 2, 3)
verify_sum(2, 2, 5, _marks=pytest.mark.skip)
verify_sum(4, 2, 6)
$ pytest ============================= test session starts ============================== collected 3 items test_readme.py .s. [100%] ========================= 2 passed, 1 skipped in 0.01s =========================
Note that the _marks keyword argument is passed through directly to the marks keyword argument of pytest.mark.param(). This means the value can be either a single mark or a collection of marks.
Similarly, add an id to a test using the _id keyword argument:
def test_addition(funparam):
@funparam
def verify_sum(a, b, expected):
assert a + b == expected
verify_sum(1, 2, 3, _id="one and two")
verify_sum(2, 2, 5, _id="two and two")
verify_sum(4, 2, 6, _id="four and two")
$ pytest --collect-only ============================= test session starts ============================== collected 3 items <Module test_readme.py> <Function test_addition[one and two]> <Function test_addition[two and two]> <Function test_addition[four and two]> ========================== 3 tests collected in 0.01s ==========================
License
Distributed under the terms of the MIT license, “pytest-funparam” is free and open source software
Issues
If you encounter any problems, please file an issue along with a detailed description.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pytest_funparam-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee3e31dadb7dca4ecc394c3a828a5e704fbbc9ab30fca6ec4e1fc6eb65528a05 |
|
MD5 | b41e47e6057996ef930a1565fd30e114 |
|
BLAKE2b-256 | 5456e03b5a77883e702937779f89a3863b853885ef4dacf9f646af697eb96be4 |