Package: pytest-funparam
pytest-funparammakes it easy to write parametrized tests.Unlikepytest.mark.parametrize,pytest-funparam:includes the failing parameter in pytest tracebacks;enables static type checking of parameters; andkeeps parameters and assertions closer together.ContentsInstallationUsageMarkersTest IDsType AnnotationsLicenseIssuesInstallationYou can install “pytest-funparam” viapipfromPyPI:$ pip install pytest-funparamUsageInside a test function, decorate a function with thefunparamfixture:deftest_addition(funparam):@funparamdefverify_sum(a,b,expected):asserta+b==expectedverify_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 ==========================Thetest_additiontest case was split into 3 tests, one for eachverify_sumcall.Becausefunparamis parametrizing the test calls, it even works with
commands likepytest--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 ===============================MarkersMark tests by using the.marks()method of your funparam function.importpytestdeftest_addition(funparam):@funparamdefverify_sum(a,b,expected):asserta+b==expectedverify_sum(1,2,3)verify_sum.marks(pytest.mark.skip)(2,2,5)verify_sum(4,2,6)$ pytest
============================= test session starts ==============================
collected 3 items

test_readme.py .s.                                                       [100%]

========================= 2 passed, 1 skipped in 0.01s =========================Test IDsSimilarly, add anidto a test using the.id()method of your funparam
function:deftest_addition(funparam):@funparamdefverify_sum(a,b,expected):asserta+b==expectedverify_sum.id("one and two")(1,2,3)verify_sum.id("two and two")(2,2,5)verify_sum.id("four and two")(4,2,6)$ 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 ==========================You can also use the shorthand for assigning anid. (It does the same thing
as calling.id().)deftest_addition(funparam):@funparamdefverify_sum(a,b,expected):asserta+b==expectedverify_sum["one and two"](1,2,3)verify_sum["two and two"](2,2,5)verify_sum["four and two"](4,2,6)$ 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 ==========================Type Annotationspytest-funparamhas full type annotations. Thefunparamfixture returns
aFunparamFixtureobject. You can import it frompytest_funparam:importpytestfrompytest_funparamimportFunparamFixturedeftest_addition(funparam:FunparamFixture):@funparamdefverify_sum(a:int,b:int,expected:int):asserta+b==expected# These are validverify_sum(1,2,3)verify_sum['it accommodates ids'](2,2,4)# Marks work too!verify_sum.marks(pytest.mark.xfail)(2,2,9)# This will be marked as invalid (since it's not an int)verify_sum(1,'2',3)# Using id/marks will still preserve the function's typing.verify_sum['should be an int'](1,2,'3')$ mypy
test_readme.py:17: error: Argument 2 to "verify_sum" has incompatible type "str"; expected "int"
test_readme.py:20: error: Argument 3 to "verify_sum" has incompatible type "str"; expected "int"
Found 2 errors in 1 file (checked 1 source file)LicenseDistributed under the terms of theMITlicense, “pytest-funparam” is free and open source softwareIssuesIf you encounter any problems, pleasefile an issuealong with a detailed description.