Skip to main content

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


Download files

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

Source Distribution

pytest-funparam-0.1.0.tar.gz (5.8 kB view hashes)

Uploaded Source

Built Distribution

pytest_funparam-0.1.0-py3-none-any.whl (5.7 kB view hashes)

Uploaded Python 3

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