Python tutorials > Testing > Test Coverage > How to measure test coverage?
How to measure test coverage?
Introduction to `coverage.py`
coverage.py
is a popular Python library that provides line, branch, and path coverage measurement. It works by analyzing the execution of your Python code during test runs and reporting which lines were executed and which were not. This information allows you to identify gaps in your testing strategy and write more effective tests.
Installation
coverage.py
library. The simplest way to do this is using pip, the Python package installer. Run the above command in your terminal.
pip install coverage
Basic Usage: Measuring Line Coverage
my_module.py
with two functions, add
and subtract
, and a corresponding test suite test_my_module.py
using the unittest
framework. To measure the coverage of this code, follow the steps in the next content part.
# my_module.py
def add(x, y):
return x + y
def subtract(x, y):
return x - y
# test_my_module.py
import unittest
from my_module import add, subtract
class TestMyModule(unittest.TestCase):
def test_add(self):
self.assertEqual(add(2, 3), 5)
def test_subtract(self):
self.assertEqual(subtract(5, 2), 3)
if __name__ == '__main__':
unittest.main()
Running Tests with Coverage
The report will show the percentage of lines covered by your tests for each file.coverage
tool. The -m unittest
option tells coverage to run the tests using the unittest module. Replace test_my_module.py
with the name of your test file.
coverage run -m unittest test_my_module.py
coverage report -m
Generating HTML Reports
coverage html
. This will create a directory named htmlcov
in your project directory. Open the index.html
file in your web browser to view the report. The HTML report highlights which lines of code were executed and which were missed by the tests. This makes it easier to identify gaps in your test coverage.
coverage html
Concepts Behind the Snippet
Real-Life Use Case
Best Practices
Interview Tip
When to use them
Memory Footprint
Alternatives
Pros
Cons
FAQ
-
How can I exclude certain files or directories from coverage measurement?
You can exclude files or directories by creating a `.coveragerc` file in your project's root directory. This file allows you to configure various aspects of coverage measurement. For example, to exclude files matching the pattern*_test.py
, add the following to your `.coveragerc` file:[run]
omit = *_test.py -
What does 'branch coverage' mean?
Branch coverage measures whether all possible execution paths through conditional statements (e.g.,if
,else
,elif
) have been tested. For example, if anif
statement has both aTrue
andFalse
branch, branch coverage ensures that both branches are executed by your tests. -
How do I integrate coverage reporting into my CI/CD pipeline?
Most CI/CD platforms (e.g., Jenkins, GitHub Actions, GitLab CI) allow you to execute shell commands as part of your build process. You can add steps to installcoverage.py
, run your tests withcoverage run
, generate a report, and then upload the report to a reporting service (e.g., SonarQube, Codecov) or simply store the report as an artifact.