Test Coverage in Python Packages

Test coverage is a crucial aspect of software development that measures how much of your code is executed during testing. It helps identify untested parts of your codebase, guiding the addition of valuable tests. The goal is not necessarily to achieve 100% coverage but to ensure that coverage is monotonically increasing, meaning it should not decrease over time. This approach allows for incremental improvements and prevents coverage from getting worse.

Configuring Test Coverage

To measure test coverage in Python, the Coverage.py tool is widely used. It can be integrated with pytest using the pytest-cov plugin. This integration allows you to run tests and collect coverage data simultaneously. To start measuring coverage, you can run:

$ py -m pytest --cov

This command will provide a report showing the coverage of your code, including the name, line, branch, and overall coverage for each file. However, it may also include coverage for dependencies and other installed packages. To focus on your package, specify your package name with the --cov option:

$ py -m pytest --cov=imppkg

If your package is not imported in the tests, you might see warnings indicating that no data was collected. To resolve this, ensure that your package's modules are imported in your test files.

Branch Coverage

In addition to line coverage, branch coverage can be configured to measure how well the branches in your code are tested. This is done by adding the following configuration:

Listing 5.1 Configuring coverage to measure branches

[coverage:run]
branch = True

With branch coverage enabled, possible branches are added to the statement count to determine total coverage. This can sometimes result in a decrease in the reported coverage percentage, as more potential execution paths are considered.

Example Coverage Output

After configuring and running tests with coverage, you might see output similar to the following:

$ py -m pytest --cov=imppkg
...
 
-- coverage: platform darwin, python 3.10.0-beta-2 --
Name                Stmts   Miss Branch BrPart  Cover
-----------------------------------------------------
.../__init__.py         0      0      0      0   100%
.../harmony.py          6      2      2      0    50%
-----------------------------------------------------
TOTAL                   6      2      2      0    50%

In this example, the harmony.py module has six statements, two of which are not executed by tests, resulting in 50% coverage. The branch coverage shows that there are two branches, none of which are partially covered.

Incremental Improvement

It's important to prioritize monotonically increasing coverage for sustained, incremental improvement over time. This means focusing on gradually increasing the coverage percentage without letting it decrease, ensuring that your codebase becomes more robust and well-tested over time.

Figure 9.8 Prioritize monotonically increasing coverage for sustained, incremental improvement over time. Figure 9.8 Prioritize monotonically increasing coverage for sustained, incremental improvement over time.

By following these practices, you can effectively use test coverage to improve the quality and reliability of your Python packages.