Pytest-cov is a plugin for pytest that provides code coverage reporting, helping you identify which parts of your code are being tested and which are not.
Pytest-cov helps improve test quality by:
- Measuring how much of your code is executed during tests
- Generating detailed reports on code coverage
- Identifying untested code paths
- Supporting branch coverage analysis
- Integrating seamlessly with pytest
- Providing various output formats (terminal, HTML, XML)
Pytest-cov is included as a development dependency:
# Install with other development dependencies
uv sync --devTo install it directly:
uv pip install pytest-covIn this project, Pytest-cov is used to:
- Measure test coverage across the codebase
- Generate coverage reports as part of the CI/CD pipeline
- Identify areas of the code that need more testing
- Enforce minimum coverage thresholds
Pytest-cov is configured in the pyproject.toml file:
[tool.pytest.ini_options]
addopts = "--cov=src --cov-report=term --cov-report=html"This configuration:
- Measures coverage for code in the
srcdirectory - Outputs coverage reports to the terminal
- Generates HTML reports for detailed analysis
To run tests with coverage:
# Run tests with coverage
uv run pytest --cov=src
# Run tests with coverage and generate HTML report
uv run pytest --cov=src --cov-report=html
# Run tests with coverage for specific modules
uv run pytest --cov=src.module1 --cov=src.module2# Specify source directories to measure
uv run pytest --cov=src --cov=lib
# Generate different report formats
uv run pytest --cov=src --cov-report=term --cov-report=html --cov-report=xml
# Set minimum coverage threshold
uv run pytest --cov=src --cov-fail-under=90
# Show line numbers of missing coverage
uv run pytest --cov=src --cov-report=term-missing----------- coverage: platform linux, python 3.11.0-final-0 -----------
Name Stmts Miss Cover
-------------------------------------------
src/__init__.py 4 0 100%
src/module1.py 26 3 88%
src/module2.py 42 7 83%
src/utils.py 18 2 89%
-------------------------------------------
TOTAL 90 12 87%
The HTML report provides a detailed view of coverage, including:
- File-by-file breakdown
- Line-by-line highlighting of covered and uncovered code
- Branch coverage information
- Summary statistics
Pytest-cov supports different types of coverage measurement:
- Statement Coverage: Measures which statements in your code have been executed
- Branch Coverage: Measures which possible branches (if/else paths) have been taken
- Function Coverage: Measures which functions have been called
- Line Coverage: Measures which executable lines have been run
- Aim for high coverage: Strive for at least 80-90% code coverage.
- Focus on critical paths: Ensure business-critical code has near 100% coverage.
- Don't just chase numbers: High coverage doesn't guarantee good tests; focus on test quality too.
- Use branch coverage: Enable branch coverage to catch untested conditional paths.
- Set minimum thresholds: Use
--cov-fail-underto enforce minimum coverage requirements. - Review coverage reports regularly: Identify and address areas with low coverage.
- Include coverage in CI: Make coverage checks part of your continuous integration pipeline.