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 == expectedSingle 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 > 0All lessons in this course
- Writing Your First pytest Tests
- Fixtures and Setup/Teardown
- Parametrize and Test Coverage
- Mocking with unittest.mock