name: test-coverage-boost description: > Systematically improves test coverage for x0tta6bl4 modules. Use when user says "increase coverage", "add tests", "improve testing", "write unit tests", "boost coverage", "untested code", or "coverage report". Identifies uncovered code and generates focused tests. metadata: author: x0tta6bl4 version: 1.0.0 category: testing tags: [testing, coverage, pytest, quality]
Test Coverage Boost
Instructions
Step 1: Measure Current Coverage
Run coverage report to identify gaps:
python3 -m pytest tests/ --cov=src --cov-report=term-missing --tb=short -q
Parse the output to find modules with lowest coverage. Focus on:
- Modules below 75% coverage (project minimum threshold)
- Critical modules (security, core, network) regardless of percentage
- Recently changed files (check
git diff --name-only HEAD~5)
Step 2: Prioritize by Impact
Rank uncovered modules by criticality:
P0 - Must cover (security/correctness impact):
src/security/- All security modulessrc/core/- Core application logicsrc/api/- API endpoints (attack surface)
P1 - Should cover (reliability impact):
src/network/- Mesh networkingsrc/self_healing/- MAPE-K loopsrc/consensus/- RAFT consensus
P2 - Nice to cover (quality improvement):
src/ml/- ML modelssrc/dao/- Governancesrc/monitoring/- Metrics
Step 3: Generate Tests (Iterative)
For each uncovered module, follow this pattern:
Read the source file to understand its public API
Identify test categories:
- Initialization / construction
- Happy path (normal operation)
- Edge cases (empty input, None, boundary values)
- Error handling (invalid input, exceptions)
- Integration points (mocked external dependencies)
Write tests following project conventions:
- File:
tests/unit/{module}/test_{filename}_unit.py - Use
pytestwithunittest.mockfor mocking - Mark async tests with
@pytest.mark.asyncio - Set environment variables at module level:
os.environ.setdefault("X0TTA6BL4_PRODUCTION", "false") os.environ.setdefault("X0TTA6BL4_SPIFFE", "false") os.environ.setdefault("X0TTA6BL4_FORCE_MOCK_SPIFFE", "true") - Use try/except ImportError with
pytest.skipfor optional dependencies - Group tests in classes by feature:
TestClassName,TestFeatureName
- File:
Run tests to verify they pass:
python3 -m pytest tests/unit/{module}/test_{filename}_unit.py -v --tb=shortRe-measure coverage for the module:
python3 -m pytest tests/ --cov=src/{module} --cov-report=term-missing -qRepeat until module reaches 75%+ coverage
Step 4: Validate All Tests Pass
python3 -m pytest tests/ -v --tb=short -q
All existing tests must continue to pass. New tests must not break old ones.
Test Patterns for x0tta6bl4
Mocking external services
from unittest.mock import patch, MagicMock, AsyncMock
# Mock database
@patch("src.api.users.users_db", new_callable=MagicMock)
def test_something(mock_db):
mock_db.find_one.return_value = {"id": "1", "email": "a@b.com"}
...
# Mock async operations
@pytest.mark.asyncio
async def test_async_op():
with patch("src.module.async_func", new_callable=AsyncMock) as mock:
mock.return_value = {"status": "ok"}
result = await some_function()
assert result["status"] == "ok"
Testing torch-dependent code
def _make_detector(**kwargs):
"""Handle environments where torch_geometric fails."""
try:
return GraphSAGEAnomalyDetector(**kwargs)
except Exception:
import src.ml.graphsage_anomaly_detector as _mod
orig = _mod._TORCH_AVAILABLE
_mod._TORCH_AVAILABLE = False
try:
return GraphSAGEAnomalyDetector(**kwargs)
finally:
_mod._TORCH_AVAILABLE = orig
Testing FastAPI endpoints
from fastapi.testclient import TestClient
from src.core.app import app
client = TestClient(app)
def test_health():
resp = client.get("/health")
assert resp.status_code == 200
assert resp.json()["status"] == "healthy"
Quality Checklist
Before marking coverage work as done:
- All new tests pass independently (
pytest test_file.py -v) - All existing tests still pass (
pytest tests/ -q) - No tests depend on execution order
- External services are mocked (DB, network, filesystem)
- Async tests use
@pytest.mark.asyncio - Coverage for target module is 75%+