coverage-analyzer
npx machina-cli add skill kanopi/cms-cultivator/coverage-analyzer --openclawCoverage Analyzer
Automatically analyze test coverage and identify untested code.
Philosophy
Knowing what's tested gives confidence to refactor and prevents regressions.
Core Beliefs
- Visibility Drives Action: Can't improve what you can't measure
- Not All Code Needs 100% Coverage: Prioritize critical paths over getters/setters
- Coverage ≠ Quality: 100% coverage doesn't guarantee bug-free code
- Gap Analysis Guides Testing: Knowing what's untested helps prioritize test writing
Why Coverage Analysis Matters
- Identify Risks: Find critical code without test protection
- Prioritize Effort: Focus testing where it matters most
- Prevent Regressions: Tests catch bugs before they reach users
- Enable Refactoring: Good coverage allows confident code changes
When to Use This Skill
Activate this skill when the user:
- Asks "what code isn't tested?"
- Mentions "test coverage" or "coverage report"
- Says "which tests are missing?"
- Shows code and asks "is this tested?"
- References "untested code paths"
- Asks "what's my coverage percentage?"
Decision Framework
Before analyzing coverage, consider:
What's the Goal?
- Find coverage gaps → Identify untested code
- Measure current coverage → Run coverage tools and report percentages
- Prioritize testing → Focus on critical paths first
- Improve coverage → Recommend specific tests to write
What's the Scope?
- Specific file - User shows code → Analyze that file's coverage
- Component/module - User mentions feature → Check component tests
- Recent changes - User says "my code" → Check coverage of git diff
- Entire project - User says "overall coverage" → Run project-wide analysis
What Test Types Exist?
Check for:
- PHPUnit tests (PHP) → Run
phpunit --coverage-text - Jest tests (JavaScript) → Run
jest --coverage - Cypress tests (E2E) → Integration coverage only
- Manual test documentation → Note gaps
What Coverage Metrics Matter?
Primary metrics:
- Line coverage - Percentage of lines executed
- Branch coverage - Percentage of decision branches taken
- Function coverage - Percentage of functions called
Priority order:
- Critical paths (auth, payments, data writes)
- Public APIs
- Security-sensitive code
- Business logic
- Getters/setters (lowest priority)
What's a Good Target?
- Critical code - Aim for 90%+ coverage
- Business logic - Aim for 80%+ coverage
- Overall project - Aim for 70%+ coverage
- Getters/setters - Can skip, focus on behavior
Decision Tree
User asks about coverage
↓
Determine scope (file/component/project)
↓
Check for existing test files
↓
Run coverage tool (PHPUnit/Jest/Cypress)
↓
Analyze gaps (prioritize critical paths)
↓
Report coverage with recommendations
↓
Suggest specific tests for gaps
Quick Coverage Analysis
1. Check if Tests Exist
PHP (PHPUnit):
# Check for test files
find tests/ -name "*Test.php"
# Check specific class
test -f "tests/src/Unit/DataProcessorTest.php" && echo "Tests exist"
JavaScript (Jest):
# Check for test files
find . -name "*.test.js" -o -name "*.spec.js"
2. Run Coverage Analysis
PHP:
# Generate coverage report
vendor/bin/phpunit --coverage-text
# For Kanopi projects
ddev exec vendor/bin/phpunit --coverage-text
JavaScript:
# Jest coverage
npm run test -- --coverage
# Or directly
npx jest --coverage
3. Identify Gaps
Common untested areas:
- Error handling (catch blocks)
- Edge cases (empty input, null values)
- Private methods (test via public interface)
- Complex conditionals
- New features without tests
Response Format
## Test Coverage Analysis
### Summary
- **Overall Coverage**: 72%
- **Files Analyzed**: 45
- **Tested**: 32 files
- **Untested**: 13 files
### Coverage by Type
- Classes: 85% (34/40)
- Methods: 68% (156/230)
- Lines: 72% (2,340/3,250)
- Branches: 58% (89/153)
### 🔴 Critical Gaps (No Tests)
**1. PaymentProcessor.php** - 0% coverage
- **Risk**: High - handles money
- **Methods untested**: processPayment(), refund(), validate()
- **Recommendation**: Add unit tests immediately
**2. AuthenticationService.php** - 0% coverage
- **Risk**: Critical - security component
- **Methods untested**: authenticate(), validateToken()
- **Recommendation**: Add security tests ASAP
### 🟡 Partial Coverage
**3. UserManager.php** - 45% coverage
- ✅ Tested: getUser(), createUser()
- ❌ Untested: deleteUser(), updatePermissions()
- **Recommendation**: Add tests for delete and permission methods
### ✅ Well Tested
- DataProcessor.php - 95%
- EmailService.php - 88%
- ValidationHelper.php - 100%
### Suggested Actions
1. **Immediate** (Critical):
- Add tests for PaymentProcessor
- Add tests for AuthenticationService
2. **This Sprint**:
- Complete UserManager tests
- Add integration tests for checkout flow
3. **Next Sprint**:
- Increase overall coverage to 80%
- Add E2E tests for critical paths
Detailed Analysis
Method-Level Coverage
## DataProcessor.php Coverage
| Method | Tested? | Coverage | Priority |
|--------|---------|----------|----------|
| processData() | ✅ Yes | 100% | - |
| validateInput() | ✅ Yes | 90% | Low |
| handleError() | ❌ No | 0% | High |
| formatOutput() | ⚠️ Partial | 60% | Medium |
### Untested Code Paths
**handleError() method:**
```php
public function handleError($error) {
// Line 45: No test coverage
if ($error instanceof ValidationException) {
return $this->formatValidationError($error);
}
// Line 49: No test coverage
if ($error instanceof DatabaseException) {
return $this->formatDatabaseError($error);
}
// Line 53: Tested
return $this->formatGenericError($error);
}
Missing test cases:
- ValidationException handling
- DatabaseException handling
- Edge case: null error
Suggested test:
public function testHandleValidationException(): void {
$exception = new ValidationException('Invalid input');
$result = $this->processor->handleError($exception);
$this->assertStringContains('validation error', $result);
}
## Integration with /test-coverage Command
- **This Skill**: Quick coverage checks
- "Is this function tested?"
- "What's missing tests?"
- Single file/class analysis
- **`/test-coverage` Command**: Comprehensive coverage analysis
- Full project coverage report
- Trend analysis over time
- CI/CD integration
- Detailed HTML reports
## Coverage Goals
### Industry Standards
- **Minimum**: 70% coverage
- **Good**: 80% coverage
- **Excellent**: 90%+ coverage
**But remember**: 100% coverage ≠ bug-free code
### What to Focus On
**High Priority:**
- Authentication/authorization
- Payment processing
- Data validation
- Security-sensitive code
- Critical business logic
**Medium Priority:**
- API endpoints
- Form handlers
- Data transformations
- Email notifications
**Low Priority:**
- Simple getters/setters
- Configuration classes
- View rendering
- Logging statements
## Quick Commands
### PHP (PHPUnit)
```bash
# Text coverage report
vendor/bin/phpunit --coverage-text
# HTML coverage report
vendor/bin/phpunit --coverage-html coverage/
# Coverage for specific test
vendor/bin/phpunit --coverage-text tests/Unit/DataProcessorTest.php
# Kanopi projects
ddev exec vendor/bin/phpunit --coverage-html coverage/
JavaScript (Jest)
# Terminal coverage
npm test -- --coverage
# HTML report
npm test -- --coverage --coverageReporters=html
# Watch mode with coverage
npm test -- --coverage --watch
# Coverage for specific file
npm test -- --coverage DataProcessor.test.js
Common Gaps & Solutions
Gap 1: Error Handling
Untested:
try {
$this->processData($data);
} catch (Exception $e) {
// Untested catch block
$this->logger->error($e->getMessage());
}
Solution:
public function testProcessDataWithException(): void {
$this->expectException(ProcessingException::class);
$this->processor->processData([]);
}
Gap 2: Edge Cases
Untested:
- Empty arrays
- Null values
- Maximum values
- Boundary conditions
Solution: Add tests for each edge case
Gap 3: Integration Points
Untested:
- Database interactions
- API calls
- File system operations
Solution: Add integration tests or use mocks
Coverage Best Practices
- Test behavior, not coverage - Don't chase 100% blindly
- Focus on critical paths - Test important code thoroughly
- Test edge cases - Empty, null, min, max values
- Test error paths - Exceptions and error handling
- Keep tests fast - Slow tests won't run
- Update tests with code - Keep tests current
Resources
Source
git clone https://github.com/kanopi/cms-cultivator/blob/main/skills/coverage-analyzer/SKILL.mdView on GitHub Overview
Coverage Analyzer automatically assesses which code is tested and where gaps exist. It helps teams focus testing on critical paths, prevent regressions, and guides test creation by surfacing untested code paths and suggesting targeted tests.
How This Skill Works
The skill determines the analysis scope (file, component, or project), runs the appropriate coverage tool (PHPUnit for PHP, Jest for JavaScript, or Cypress for E2E), and analyzes results to surface untested paths. It then prioritizes gaps based on criticality and provides concrete test recommendations.
When to Use It
- what code isn't tested?
- coverage report
- which tests are missing?
- is this code tested?
- untested code paths
Quick Start
- Step 1: Determine scope (file, component, or project) and locate related tests.
- Step 2: Run the correct coverage command (phpunit --coverage-text, npm test -- --coverage, or npx jest --coverage).
- Step 3: Review the report, identify gaps, and write targeted tests to close them.
Best Practices
- Define the analysis scope before running coverage (file, component, or project).
- Run the project-appropriate coverage tool (PHPUnit, Jest, or Cypress).
- Prioritize gaps in critical paths (auth, payments, data writes) and public APIs.
- Review uncovered areas and write targeted tests to close gaps.
- Re-run coverage and compare against target benchmarks to verify improvements.
Example Use Cases
- PHP e-commerce checkout flow with missing tests on payment validation logic.
- React components with high-risk business logic showing only 60% coverage.
- Git diff analysis to check coverage impact after recent changes.
- Cypress E2E suite that misses integration coverage for edge cases.
- Analytics service lacking tests for error handling in the service layer.