To measure test coverage with pytest-cov, install the plugin and run pytest --cov=your_package --cov-report=term-missing. It runs your tests under coverage.py and prints, for each file, the percentage of lines executed and the line numbers that no test reached. That makes pytest-cov coverage the quickest way to find untested code, and you can fail the build when coverage drops below a limit.
This guide uses pytest-cov (current 6.x and 7.x releases) with pytest 8 on Python 3.13. Coverage shows what ran, not whether the assertions are good.
Install it into the same environment as pytest:
Take a small package:
Run:
The Missing column lists the lines no test executed: the error branch and the zero-percent shortcut. Writing tests for those two paths brings the file to 100 percent.
By default, coverage measures lines. Branch coverage also checks that each if was seen both true and false, which catches code where a line ran but one outcome never did.
With branches, a partial line appears in the missing column as a range such as 5->7, meaning the jump from line 5 to line 7 never happened. Turn branch coverage on for any project that has more than trivial logic.
| Measure | Question it answers |
|---|---|
| Line coverage | Did this line run? |
| Branch coverage | Did each decision go both ways? |
| Neither | Are the assertions meaningful? (only reviews and mutation testing tell you) |
Pass --cov-report several times to get more than one output:
term-missing prints the table with missing lines.html writes htmlcov/index.html, where you can open any file and see covered lines in green and missed ones in red.xml writes coverage.xml, which services such as code-quality dashboards read.json and lcov formats are also available for other tools.Use --cov-fail-under to make the run fail when total coverage is too low:
Pick a threshold your project already meets and raise it slowly. A target of 100 percent tends to push people to write meaningless tests, while a number that never moves is ignored.
Keep options in the project configuration so everyone runs the same command:
Use omit for generated files and exclude_also for lines that should not count, such as type-checking imports. Exclude sparingly. For a single line, add a # pragma: no cover comment, and give a reason in review.
If your tests start subprocesses, or you use pytest-xdist, pytest-cov combines the data files for you; in recent versions subprocess measurement works without a special setup, but check that your configuration includes parallel = true if you run coverage by hand.
When coverage is collected in separate steps, such as several CI jobs, combine the data files with coverage combine and then run coverage report.
A high number is not proof of quality. This test gives 100 percent line coverage and checks nothing:
Treat coverage as a map of untested code, not a score. Use it to find files nobody tested, review the missing lines in the HTML report, and make sure the assertions in the tests check real behaviour. For stronger confidence, add mutation testing or property-based tests.
coverage.py measures the code, while pytest-cov is the pytest plugin that starts it, merges data from xdist workers and adds the --cov options.
Usually the --cov value does not match an importable package or path. Use the package name or directory, and check that tests import the code you measure.
Add its path to omit under [tool.coverage.run]. Use exclude_also or # pragma: no cover for single lines.
There is no universal number. Many teams hold between 80 and 90 percent with branch coverage and focus review on the changed lines.
Install pytest-cov, run pytest --cov=package --cov-branch --cov-report=term-missing, and read the missing lines to decide which tests to write. Store settings in pyproject.toml, produce HTML or XML reports as needed, and use --cov-fail-under to protect the level you have reached. Treat coverage as a guide to untested code and not as proof the tests are good.