Skip to content

Parameterized Testing - Running Tests with Multiple Data Sets

diagram parametrize makes N tests, not one test with a loop mermaid
Each set of arguments is collected as its own test, with its own id and its own result. That is the whole advantage over looping inside a single test: one bad case is reported as one failure, the others still report as passes, and you can re-run just the failing id.

Instead of repeating similar tests, parameterize inputs.

test_parametrize.py
import pytest
 
 
def add(a, b):
    return a + b
 
 
@pytest.mark.parametrize(
    "a,b,expected",
    [
        (1, 2, 3),
        (0, 0, 0),
        (-1, 1, 0),
    ],
)
def test_add(a, b, expected):
    assert add(a, b) == expected
test_parametrize_ids.py
import pytest
 
 
@pytest.mark.parametrize(
    "s,expected",
    [("10", 10), ("0", 0)],
    ids=["ten", "zero"],
)
def test_int_parse(s, expected):
    assert int(s) == expected

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading