How to Measure Code Coverage in Python Easily
You can measure code coverage in Python using the
coverage tool, which tracks how much of your code is tested by running your tests with coverage run and then generating a report with coverage report or coverage html. This helps you see which parts of your code are covered by tests and which are not.Syntax
To measure code coverage, you use the coverage command-line tool with these main steps:
coverage run -m unittestruns your tests while tracking coverage.coverage reportshows a summary of coverage in the terminal.coverage htmlcreates a detailed HTML report you can open in a browser.
Each part means:
- coverage run: Runs your test code and collects coverage data.
- -m unittest: Runs Python's built-in test framework (you can replace this with your test command).
- coverage report: Prints coverage stats in the terminal.
- coverage html: Generates a visual report in HTML format.
bash
coverage run -m unittest coverage report coverage html
Example
This example shows how to measure coverage for a simple Python function and its test using coverage and unittest.
python
def add(a, b): return a + b import unittest class TestAdd(unittest.TestCase): def test_add(self): self.assertEqual(add(2, 3), 5) if __name__ == '__main__': import coverage cov = coverage.Coverage() cov.start() unittest.main(exit=False) cov.stop() cov.save() cov.report()
Output
Name Stmts Miss Cover
--------------------------------
__main__ 7 0 100%
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Common Pitfalls
Common mistakes when measuring code coverage include:
- Not running tests through
coverage run, so no coverage data is collected. - Forgetting to save coverage data with
cov.save()when using the API. - Running tests without coverage tracking, leading to zero coverage reports.
- Ignoring generated HTML reports which provide clearer insights than terminal output.
Always ensure your tests run inside the coverage tool and check reports carefully.
python
## Wrong way (no coverage tracking): import unittest class TestAdd(unittest.TestCase): def test_add(self): self.assertEqual(1 + 1, 2) unittest.main() ## Right way (with coverage): import coverage cov = coverage.Coverage() cov.start() import unittest class TestAdd(unittest.TestCase): def test_add(self): self.assertEqual(1 + 1, 2) unittest.main(exit=False) cov.stop() cov.save() cov.report()
Quick Reference
Here is a quick cheat sheet for measuring code coverage in Python:
| Command | Description |
|---|---|
| coverage run -m unittest | Run tests with coverage tracking |
| coverage report | Show coverage summary in terminal |
| coverage html | Generate detailed HTML coverage report |
| coverage erase | Clear previous coverage data |
| coverage combine | Combine data from multiple runs |
Key Takeaways
Use the coverage.py tool to track which parts of your Python code are tested.
Run your tests with coverage using 'coverage run' to collect data.
Generate reports with 'coverage report' for terminal output or 'coverage html' for detailed browser view.
Always ensure coverage data is saved when using the API to avoid empty reports.
Check HTML reports for clear visualization of covered and missed code lines.