SyntaxStudy
Sign Up
Python Intermediate 4 min read

Parametrized Tests

@pytest.mark.parametrize

Run the same test with multiple input sets using parametrize. Each combination becomes a separate test case.

Example
import pytest

@pytest.mark.parametrize("a,b,expected", [
    (2, 3, 5),
    (0, 0, 0),
    (-1, 1, 0),
    (100, -50, 50),
])
def test_add(a, b, expected):
    assert a + b == expected

@pytest.mark.parametrize("email", ["notanemail", "@bad.com", ""])
def test_invalid_emails(email):
    with pytest.raises(ValueError):
        validate_email(email)
Pro Tip

Add an id to each tuple for readable test names: (2, 3, 5, id="positive").