Parametrized tests run the same test with different inputs.
1import pytest23def is_prime(n):4 if n < 2:5 return False6 for i in range(2, int(n ** 0.5) + 1):7 if n % i == 0:8 return False9 return True1011# Basic parametrize12@pytest.mark.parametrize("input,expected", [13 (2, True),14 (3, True),15 (4, False),16 (17, True),17 (20, False),18 (-1, False),19 (0, False),20 (1, False),21])22def test_is_prime(input, expected):23 assert is_prime(input) == expected2425# Multiple parameters26@pytest.mark.parametrize("a,b,expected", [27 (1, 2, 3),28 (-1, 1, 0),29 (0, 0, 0),30 (100, 200, 300),31])32def test_add(a, b, expected):33 assert a + b == expected3435# IDs for better output36@pytest.mark.parametrize("input", [37 pytest.param(2, id="prime-2"),38 pytest.param(4, id="not-prime-4"),39 pytest.param(17, id="prime-17"),40])41def test_is_prime_with_ids(input):42 assert is_prime(input) == (input in [2, 17])4344# Cross product45@pytest.mark.parametrize("x", [1, 2])46@pytest.mark.parametrize("y", [10, 20])47def test_multiply(x, y):48 assert x * y > 0
Benefits: