0Pricing
Python Academy · Lesson

Parametrize and Test Coverage

Run tests with multiple inputs using @pytest.mark.parametrize.

@pytest.mark.parametrize

Run the same test with multiple inputs using @pytest.mark.parametrize. Each tuple is a separate test case.

import pytest

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

Single Parameter

For a single argument, pass a list of values directly.

import pytest

@pytest.mark.parametrize("n", [1, 2, 3, 4, 5])
def test_positive(n):
    assert n > 0

All lessons in this course

  1. Writing Your First pytest Tests
  2. Fixtures and Setup/Teardown
  3. Parametrize and Test Coverage
  4. Mocking with unittest.mock
← Back to Python Academy