Parametrize and Test Coverage
Run tests with multiple inputs using @pytest.mark.parametrize.
Parametrize and Test Coverage is a free Python Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
@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 > 0Named Test IDs
Provide an ids list to label each parameter set for clearer output.
import pytest
@pytest.mark.parametrize("value,result", [
(2, True),
(3, False),
], ids=["even", "odd"])
def test_is_even(value, result):
assert (value % 2 == 0) == resultStacking parametrize Decorators
Stack two parametrize decorators to get the Cartesian product of inputs.
import pytest
@pytest.mark.parametrize("x", [1, 2])
@pytest.mark.parametrize("y", [10, 20])
def test_product(x, y):
assert x * y > 0 # 4 test cases: (1,10),(1,20),(2,10),(2,20)pytest-cov: Install
Install pytest-cov to measure which lines of your code are exercised by tests.
# pip install pytest-cov
# Basic usage:
# pytest --cov=mypackage
# HTML report:
# pytest --cov=mypackage --cov-report=htmlReading Coverage Output
Coverage shows which lines, branches, or statements were not reached during testing.
# Name Stmts Miss Cover
# ------- ----- ---- -----
# mypackage/utils.py 20 3 85%
#
# Missing: lines 45, 67, 88Branch Coverage
Use --cov-branch to track whether both branches of conditionals are tested.
# pytest --cov=mypackage --cov-branch
def is_positive(n):
if n > 0: # branch 1: True
return True
return False # branch 2: FalseFailing on Low Coverage
Use --cov-fail-under=N to fail the build if coverage drops below N percent.
# pytest --cov=mypackage --cov-fail-under=80
# FAIL Required test coverage of 80% not reached. Total: 72%.coveragerc Configuration
Configure coverage exclusions in .coveragerc to skip boilerplate code.
# .coveragerc
[run]
omit =
*/migrations/*
*/tests/*
setup.py
[report]
exclude_lines =
pragma: no cover
if TYPE_CHECKING:What Good Coverage Means
High coverage does not guarantee correct tests. Aim for meaningful assertions, not just line hits. 80-90% is a practical target for most projects.
# A covered but useless test:
def test_nothing():
is_positive(5) # no assert — 100% coverage, zero confidence
# A meaningful test:
def test_positive():
assert is_positive(5) is True
assert is_positive(-1) is FalseCombining parametrize and Coverage
Parametrized tests improve coverage naturally by exercising multiple code paths with one test definition.
import pytest
@pytest.mark.parametrize("n,expected", [
(5, True), (-1, False), (0, False)
])
def test_is_positive(n, expected):
assert is_positive(n) == expectedQuick Check
What does --cov-fail-under=90 do in a pytest run?
Recap
Use @pytest.mark.parametrize to test multiple inputs cleanly. Measure code coverage with pytest-cov, track branches with --cov-branch, and enforce minimums with --cov-fail-under.
Frequently asked questions
Is the “Parametrize and Test Coverage” lesson free?
Yes — the full text of “Parametrize and Test Coverage” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “Parametrize and Test Coverage”?
Run tests with multiple inputs using @pytest.mark.parametrize. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Parametrize and Test Coverage” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Writing Your First pytest Tests
- Fixtures and Setup/Teardown
- Parametrize and Test Coverage
- Mocking with unittest.mock