| name | coverage-analyzer |
| description | Analyze test coverage with detailed metrics, identify gaps, and generate comprehensive reports. Use when measuring code coverage, finding untested code, or validating coverage thresholds. |
| allowed-tools | Read, Bash, Grep, Glob |
Coverage Analyzer Skill
Purpose
This skill provides comprehensive test coverage analysis, gap identification, threshold validation, and detailed reporting to ensure code quality and adequate test coverage across the codebase.
When to Use
- Measuring test coverage for a module or project
- Identifying untested code and coverage gaps
- Validating coverage meets threshold (≥ 80%)
- Generating coverage reports for review
- Analyzing coverage trends over time
- Finding missing branch coverage
Coverage Analysis Workflow
1. Basic Coverage Measurement
Run Tests with Coverage:
pip install pytest-cov
pytest --cov=src tests/
pytest --cov=src --cov-report=term tests/
pytest --cov=src --cov-report=term-missing tests/
pytest --cov=src.tools.feature tests/
Quick Coverage Check:
pytest --cov=src --cov-report=term tests/ | grep TOTAL
pytest --cov=src --cov-fail-under=80 tests/
pytest --cov=src --cov-report=term -q tests/
Deliverable: Basic coverage metrics
2. Detailed Coverage Analysis
Generate Detailed Reports:
pytest --cov=src --cov-report=html tests/
pytest --cov=src --cov-report=xml tests/
pytest --cov=src --cov-report=json tests/
pytest --cov=src \
--cov-report=html \
--cov-report=xml \
--cov-report=term-missing \
tests/
Analyze Coverage by Module:
pytest --cov=src --cov-report=term tests/
pytest --cov=src --cov-report=term-missing tests/
pytest --cov=src --cov-report=annotate tests/
Deliverable: Detailed coverage reports
3. Coverage Gap Identification
Find Untested Code:
pytest --cov=src --cov-report=term-missing tests/
pytest --cov=src --cov-branch --cov-report=term-missing tests/
pytest --cov=src --cov-report=html tests/
Identify Low Coverage Modules:
pytest --cov=src --cov-report=term tests/ | grep -v "100%"
pytest --cov=src --cov-report=term tests/ | awk '$NF ~ /%/ && $NF+0 < 80'
pytest --cov=src --cov-report=term tests/ | sort -k4 -n
Analyze Coverage by Test Type:
pytest --cov=src --cov-report=term tests/unit/
pytest --cov=src --cov-report=term tests/integration/
pytest --cov=src --cov-report=term tests/unit/ tests/integration/
pytest --cov=src --cov-append --cov-report=term tests/unit/
pytest --cov=src --cov-append --cov-report=term tests/integration/
Deliverable: Coverage gap analysis
4. Branch Coverage Analysis
Measure Branch Coverage:
pytest --cov=src --cov-branch --cov-report=term-missing tests/
pytest --cov=src --cov-branch --cov-report=html tests/
pytest --cov=src --cov-branch --cov-report=term-missing tests/ | grep "!->"
Analyze Conditional Coverage:
pytest --cov=src --cov-branch --cov-report=term-missing tests/ | grep "if"
pytest --cov=src --cov-branch --cov-report=term-missing tests/ | grep "case"
Deliverable: Branch coverage metrics
5. Coverage Threshold Validation
Validate Minimum Coverage:
pytest --cov=src --cov-fail-under=80 tests/
pytest --cov=src --cov-fail-under=90 tests/
pytest --cov=src --cov-fail-under=80 --cov-report=term-missing tests/
Per-Module Thresholds:
pytest --cov=src.core --cov-fail-under=90 tests/
pytest --cov=src.utils --cov-fail-under=85 tests/
pytest --cov=src.interfaces --cov-fail-under=80 tests/
Deliverable: Threshold validation report
6. Coverage Configuration
Configure .coveragerc:
[run]
source = src
omit =
tests/*
*/__init__.py
*/venv/*
*/.venv/*
*/migrations/*
branch = True
[report]
exclude_lines =
pragma: no cover
def __repr__
def __str__
raise AssertionError
raise NotImplementedError
if __name__ == .__main__.:
if TYPE_CHECKING:
@abstractmethod
precision = 2
show_missing = True
[html]
directory = htmlcov
[xml]
output = coverage.xml
Configure pyproject.toml:
[tool.coverage.run]
source = ["src"]
omit = [
"tests/*",
"*/__init__.py",
"*/venv/*",
"*/.venv/*",
]
branch = true
[tool.coverage.report]
exclude_lines = [
"pragma: no cover",
"def __repr__",
"raise AssertionError",
"raise NotImplementedError",
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
"@abstractmethod",
]
precision = 2
show_missing = true
fail_under = 80
[tool.coverage.html]
directory = "htmlcov"
[tool.coverage.xml]
output = "coverage.xml"
Deliverable: Coverage configuration
7. Coverage Reporting
Generate Coverage Reports:
pytest --cov=src --cov-report=term tests/
pytest --cov=src --cov-report=term-missing tests/
pytest --cov=src --cov-report=html tests/
pytest --cov=src --cov-report=xml tests/
pytest --cov=src --cov-report=json tests/
pytest --cov=src \
--cov-report=term-missing \
--cov-report=html \
--cov-report=xml \
--cov-report=json \
tests/
Coverage Summary Report:
# Coverage Report
## Overall Coverage: 87%
### By Module
| Module | Coverage | Missing Lines |
|--------|----------|---------------|
| src.core | 95% | 45-47, 89 |
| src.utils | 88% | 12-15, 67-70 |
| src.interfaces | 82% | 23, 56-58 |
| src.models | 100% | - |
### Critical Gaps
1. Error handling in `src.core.process()` (lines 45-47)
2. Edge case validation in `src.utils.validate()` (lines 67-70)
3. Exception paths in `src.interfaces.execute()` (lines 56-58)
### Recommendations
- Add tests for error conditions in core module
- Improve edge case coverage in utilities
- Test exception handling in interfaces
### Status
✅ Overall coverage: 87% (target: 80%)
✅ Core business logic: 95% (target: 90%)
✅ Utilities: 88% (target: 85%)
✅ All thresholds met
Deliverable: Comprehensive coverage reports
8. Coverage Trend Analysis
Track Coverage Over Time:
pip install coverage-badge
coverage-badge -o coverage.svg
pytest --cov=src --cov-report=json tests/
mv coverage.json coverage-$(date +%Y%m%d).json
coverage json
python -c "import json; print(json.load(open('coverage.json'))['totals']['percent_covered'])"
Coverage Diff:
pytest --cov=src --cov-report=json tests/
cp coverage.json coverage-current.json
git checkout main
pytest --cov=src --cov-report=json tests/
cp coverage.json coverage-main.json
diff <(jq '.totals.percent_covered' coverage-main.json) \
<(jq '.totals.percent_covered' coverage-current.json)
Deliverable: Coverage trends and comparisons
Coverage Analysis Patterns
Quick Coverage Check
pytest --cov=src --cov-report=term-missing -q tests/unit/
Comprehensive Coverage Analysis
pytest --cov=src \
--cov-branch \
--cov-report=html \
--cov-report=xml \
--cov-report=term-missing \
--cov-fail-under=80 \
tests/
CI/CD Coverage Check
pytest --cov=src \
--cov-report=xml \
--cov-report=term \
--cov-fail-under=80 \
tests/
Coverage Gap Investigation
pytest --cov=src --cov-report=html tests/
Coverage Metrics and Thresholds
Coverage Targets
By Module Type:
- Core Business Logic: ≥ 90%
- Services/APIs: ≥ 85%
- Utilities/Helpers: ≥ 85%
- Models/DTOs: ≥ 90%
- Interfaces/Contracts: ≥ 80%
- CLI/Main: ≥ 70%
By Coverage Type:
- Line Coverage: ≥ 80%
- Branch Coverage: ≥ 75%
- Function Coverage: ≥ 85%
Overall Project:
- Minimum: 80%
- Target: 85%
- Excellent: 90%+
Acceptable Exclusions
def __repr__(self):
return f"Object({self.id})"
if __name__ == "__main__":
main()
if TYPE_CHECKING:
from typing import Optional
@abstractmethod
def interface_method(self):
pass
def debug_helper():
pass
Coverage Analysis Checklist
Pre-Analysis
Analysis
Threshold Validation
Reporting
Common Coverage Commands
Quick Reference
pytest --cov=src tests/
pytest --cov=src --cov-report=term-missing tests/
pytest --cov=src --cov-fail-under=80 tests/
pytest --cov=src --cov-branch --cov-report=term-missing tests/
pytest --cov=src --cov-report=html tests/
pytest --cov=src \
--cov-branch \
--cov-report=html \
--cov-report=term-missing \
--cov-fail-under=80 \
tests/
Coverage Analysis Troubleshooting
Coverage Tool Not Found
pip install pytest-cov
pytest --version
pip show pytest-cov
Incorrect Coverage (Too Low/High)
cat .coveragerc
cat pyproject.toml
pytest --cov=src tests/
pytest --cov tests/
rm -rf .coverage htmlcov/
pytest --cov=src tests/
Missing Files in Coverage
grep -A5 "\[run\]" .coveragerc
pytest --cov=src --cov-report=term tests/ | grep "filename"
grep -r "^import\|^from" tests/
Branch Coverage Not Working
pytest --cov=src --cov-branch tests/
grep "branch" .coveragerc
grep -r "if\|else\|elif" src/
Integration with Test Runner Specialist
Input: Completed test execution with coverage enabled
Process: Analyze coverage, identify gaps, validate thresholds
Output: Coverage report with gap analysis and recommendations
Next Step: Report to test-runner-specialist for decision
Coverage Analysis Best Practices
Development
- Check coverage frequently during TDD
- Aim for >90% coverage on new code
- Review coverage reports locally
- Fix coverage gaps before committing
Pre-Commit
- Verify coverage meets threshold
- Review coverage diff vs main branch
- Ensure new code is well tested
- Check branch coverage for complex logic
CI/CD
- Generate coverage reports for every build
- Fail build if coverage below threshold
- Track coverage trends over time
- Archive coverage reports
Review
- Focus on critical paths first
- Prioritize business logic coverage
- Don't chase 100% coverage unnecessarily
- Exclude unreachable code appropriately
Coverage Gap Prioritization
High Priority (Fix Immediately)
- Core business logic uncovered
- Error handling paths untested
- Security-critical code uncovered
- Data validation missing tests
- API endpoints without tests
Medium Priority (Fix Soon)
- Utilities with <80% coverage
- Branch coverage gaps in logic
- Edge cases not tested
- Configuration parsing untested
Low Priority (Fix When Possible)
- Representation methods (repr, str)
- Debug/logging code
- CLI help text
- Non-critical utilities
Acceptable to Exclude
- Abstract methods
- TYPE_CHECKING blocks
- if name == "main"
- Platform-specific code not running in CI
- Deprecated code scheduled for removal
Supporting Resources
Success Metrics