Last modified: Jun 01, 2025 By Alexander Williams

Install Pytest-cov for Coverage Reports

Testing is crucial in Python development. Coverage reports help track tested code. Pytest-cov integrates with pytest to generate these reports.

What is Pytest-cov?

Pytest-cov is a plugin for pytest. It measures code coverage during tests. This helps identify untested parts of your codebase.

Prerequisites

Before installing pytest-cov, ensure you have:

  • Python 3.6+ installed
  • Basic knowledge of pytest
  • A project with tests

If you need pytest basics, check our guide on Install Bottle Web Framework for Python.

Install Pytest-cov

Install pytest-cov using pip:


pip install pytest-cov

This installs pytest-cov and its dependencies. Verify installation:


pytest --version

Basic Usage

Run tests with coverage using:


pytest --cov=your_package tests/

Replace your_package with your module name. This generates a coverage report.

Understanding the Output

A sample output looks like:


----------- coverage: platform linux, python 3.8.5 -----------
Name               Stmts   Miss  Cover
-------------------------------------
your_module.py       15      2    87%

The report shows statements, missed lines, and coverage percentage.

Generating HTML Reports

For detailed reports, generate HTML:


pytest --cov=your_package --cov-report=html tests/

This creates an htmlcov directory. Open index.html in a browser.

Configuration

Add a .coveragerc file to customize coverage:

 
[run]
source = your_package
omit = */tests/*

This configures coverage source and omitted files.

Advanced Features

Pytest-cov offers advanced features:

  • Branch coverage
  • Coverage thresholds
  • Multiple report formats

For similar tools, see our guide on Install Pexpect in Python for Automation.

Common Issues

Some common problems include:

  • Missing source files
  • Incorrect paths
  • Version conflicts

Ensure paths are correct and versions compatible.

Conclusion

Pytest-cov is essential for Python testing. It provides valuable coverage insights. Install it today to improve your testing workflow.

For more Python tools, check our guide on Install PsySH for Python IPython Shell.