Test Coverage Tracking and Guidelines¶
📊 Current Status¶
CI Threshold: 85% (pytest enforces it through pyproject's addopts)
The codecov badge shows live coverage.
🎯 Coverage Milestones¶
The threshold was raised in stages to its current 85%.
🔍 Finding Low-Coverage Files¶
Run uv run pytest --cov-report=term-missing and sort by the Miss column, or open the Codecov dashboard.
🛠️ How to Improve Coverage¶
Running Coverage Locally¶
pytest fails when coverage drops below 85%.
# Basic coverage report
uv run pytest --cov=src/pyeye --cov-report=term
# Detailed with missing lines
uv run pytest --cov=src/pyeye --cov-report=term-missing
# HTML report for exploration
uv run pytest --cov=src/pyeye --cov-report=html
# Open htmlcov/index.html
# Check specific module
uv run pytest --cov=pyeye.plugins.flask --cov-fail-under=0 tests/unit/plugins/test_flask_plugin.py
# Fails below the threshold; pyproject's addopts enforces it
uv run pytest
Writing Effective Tests¶
1. Test Structure Pattern¶
def test_feature_happy_path():
"""Test normal operation."""
# Given: Setup
# When: Action
# Then: Assert
def test_feature_edge_case():
"""Test boundary conditions."""
def test_feature_error_handling():
"""Test error scenarios."""
2. Common Patterns to Test¶
Plugin Detection¶
def test_plugin_detects_framework():
"""Test that plugin correctly identifies framework."""
with tempfile.TemporaryDirectory() as tmpdir:
# Create marker files
# Assert detection works
Error Handling¶
def test_handles_invalid_input():
"""Test graceful error handling."""
with pytest.raises(ValidationError):
function_under_test(invalid_input)
Async Code¶
@pytest.mark.asyncio
async def test_async_operation():
"""Test async functionality."""
result = await async_function()
assert result.expected_value
Coverage Best Practices¶
- Focus on Critical Paths First
- Core functionality
- User-facing APIs
-
Error handling
-
Don't Chase Full Coverage
- Some code is hard to test (OS-specific, network)
-
Use
# pragma: no coversparingly with justification -
Test Behavior, Not Implementation
- Tests should survive refactoring
-
Focus on inputs/outputs, not internals
-
Mock External Dependencies
from unittest.mock import patch, MagicMock
@patch('module.external_service')
def test_with_mock(mock_service):
mock_service.return_value = expected_data
📋 PR Checklist for Coverage¶
Before submitting a PR:
- [ ] Run
uv run pytest --cov=src/pyeye --cov-report=term - [ ] Coverage is at or above 85% (pytest fails below it)
- [ ] Modified files' coverage didn't drop
- [ ] No untested error paths in new code
⬆️ Raising the Threshold¶
Change --cov-fail-under in pyproject's addopts and in every workflow that sets it (ci.yml, release.yml) in one PR. docs_facts.py check fails while any of them disagree, and docs_facts.py generate updates every page that binds ci.coverage_threshold.
🤝 Contributing to Coverage¶
- Pick a Low-Coverage File: See Finding Low-Coverage Files
- Write Comprehensive Tests: Follow patterns in this guide
- Submit PR: Say in its description which file's coverage it raises