Add Test Coverage
Analyze the most recent commit (HEAD) and implement all tests required to cover new and modified code.
When to Use This Skill
- After completing a feature or fix, to ensure adequate test coverage before merge
- When a commit introduces new functions, methods, or API endpoints
- When modifying existing behavior that may not have sufficient tests
- During code review when test coverage gaps are identified
Workflow
Phase 1: Change Analysis
- Identify changes in the latest commit:
- Diff HEAD against its parent (HEAD~1), or against the appropriate base branch if more accurate
- Classify each changed file by type based on file extensions and project structure:
- Backend code (e.g.,
.go, .py, .java, .rb)
- Frontend code (e.g.,
.js, .ts, .jsx, .tsx, .vue, .svelte)
- Integration/API surfaces (e.g., handlers, controllers, routes)
- Config, infrastructure, or test-only changes
- For each changed area, determine:
- What behavior was added or modified
- Which existing tests (if any) already cover this behavior
- Where new or expanded tests are needed
Phase 2: Test Plan Design
Draft a concrete test plan for this commit:
Scenarios to cover:
- List specific behaviors that need tests
- Identify edge cases and error conditions
Test layers to use:
- Unit tests for individual functions/methods
- Integration tests for cross-component behavior
- E2E tests for user-visible flows (if applicable)
Test dependencies:
- Required fixtures, mocks, or test data
- Any setup/teardown requirements
Prioritization criteria:
- Safety-critical paths
- Public/externally visible behavior
- Complex logic and branching
- Previously under-tested areas
Phase 3: Test Implementation
Implement the tests specified in your plan:
Follow existing conventions:
- Use existing test directories and naming patterns
- Reuse shared test utilities and helpers
- Match the style of existing tests in the repository
Test quality standards:
- Each test should be focused on a single behavior
- Tests must be deterministic (no flaky tests)
- Tests should be readable and self-documenting
- Include clear assertion messages
Phase 4: Execution and Refinement
Incremental testing:
- Run only the relevant tests while developing
- Use focused test commands (specific packages, files, or test names)
Validation:
- Once confident, run a broader test subset to check for regressions
- Ensure all new tests pass consistently
Failure diagnosis:
- If tests fail, prefer fixing implementation bugs exposed by the tests
- Only adjust tests when they don't match the correct intended behavior
Phase 5: Summary
Produce a concise summary including:
- Which files changed in the commit
- What tests were added or modified (by file and purpose)
- What behaviors are now covered that were not before
- The exact commands to run the relevant test suites
Constraints
- Do not remove or disable existing tests unless clearly invalid; explain any such changes
- Do not introduce new frameworks or major structural changes; work within the existing test stack
- Keep changes tightly scoped to covering the latest commit's behavior, not the entire repository
Examples
Example: New HTTP Handler in Go
Commit changes: Added internal/api/users.go with a CreateUser handler
Phase 1 output:
Changed files:
- internal/api/users.go (new file)
- Added CreateUser handler
- Added userRequest/userResponse types
- Added input validation logic
Existing test coverage: None (new file)
Phase 2 output:
Test Plan:
1. Unit tests for input validation
- Valid request succeeds
- Missing required fields return 400
- Invalid email format returns 400
2. Integration tests for CreateUser handler
- Successful user creation returns 201
- Duplicate email returns 409
- Database error returns 500
3. Mocks needed: UserRepository interface
Phase 3 output:
Created: internal/api/users_test.go
- TestCreateUser_Success
- TestCreateUser_MissingFields
- TestCreateUser_InvalidEmail
- TestCreateUser_DuplicateEmail
- TestCreateUser_DatabaseError
Phase 5 summary:
Files changed in commit: internal/api/users.go
Tests added:
- internal/api/users_test.go (5 tests covering CreateUser handler)
New coverage:
- Input validation for user creation requests
- Success and error paths for CreateUser handler
- Database error handling
Run tests: go test ./internal/api/... -v
Begin by performing the diff-based analysis for HEAD and drafting the test plan before writing or modifying any tests.
1---2name: add-test-coverage3description: Analyze git HEAD commit changes and implement comprehensive test coverage. Supports unit tests, integration tests, and E2E tests. Automatically detects language and testing frameworks from the repository.4license: MIT5---67# Add Test Coverage89Analyze the most recent commit (HEAD) and implement all tests required to cover new and modified code.1011## When to Use This Skill1213- After completing a feature or fix, to ensure adequate test coverage before merge14- When a commit introduces new functions, methods, or API endpoints15- When modifying existing behavior that may not have sufficient tests16- During code review when test coverage gaps are identified1718## Workflow1920### Phase 1: Change Analysis21221. Identify changes in the latest commit:23 - Diff HEAD against its parent (HEAD~1), or against the appropriate base branch if more accurate242. Classify each changed file by type based on file extensions and project structure:25 - Backend code (e.g., `.go`, `.py`, `.java`, `.rb`)26 - Frontend code (e.g., `.js`, `.ts`, `.jsx`, `.tsx`, `.vue`, `.svelte`)27 - Integration/API surfaces (e.g., handlers, controllers, routes)28 - Config, infrastructure, or test-only changes293. For each changed area, determine:30 - What behavior was added or modified31 - Which existing tests (if any) already cover this behavior32 - Where new or expanded tests are needed3334### Phase 2: Test Plan Design3536Draft a concrete test plan for this commit:37381. **Scenarios to cover:**39 - List specific behaviors that need tests40 - Identify edge cases and error conditions41422. **Test layers to use:**43 - Unit tests for individual functions/methods44 - Integration tests for cross-component behavior45 - E2E tests for user-visible flows (if applicable)46473. **Test dependencies:**48 - Required fixtures, mocks, or test data49 - Any setup/teardown requirements50514. **Prioritization criteria:**52 - Safety-critical paths53 - Public/externally visible behavior54 - Complex logic and branching55 - Previously under-tested areas5657### Phase 3: Test Implementation5859Implement the tests specified in your plan:60611. **Follow existing conventions:**62 - Use existing test directories and naming patterns63 - Reuse shared test utilities and helpers64 - Match the style of existing tests in the repository65662. **Test quality standards:**67 - Each test should be focused on a single behavior68 - Tests must be deterministic (no flaky tests)69 - Tests should be readable and self-documenting70 - Include clear assertion messages7172### Phase 4: Execution and Refinement73741. **Incremental testing:**75 - Run only the relevant tests while developing76 - Use focused test commands (specific packages, files, or test names)77782. **Validation:**79 - Once confident, run a broader test subset to check for regressions80 - Ensure all new tests pass consistently81823. **Failure diagnosis:**83 - If tests fail, prefer fixing implementation bugs exposed by the tests84 - Only adjust tests when they don't match the correct intended behavior8586### Phase 5: Summary8788Produce a concise summary including:8990- Which files changed in the commit91- What tests were added or modified (by file and purpose)92- What behaviors are now covered that were not before93- The exact commands to run the relevant test suites9495## Constraints9697- **Do not remove or disable existing tests** unless clearly invalid; explain any such changes98- **Do not introduce new frameworks** or major structural changes; work within the existing test stack99- **Keep changes tightly scoped** to covering the latest commit's behavior, not the entire repository100101## Examples102103### Example: New HTTP Handler in Go104105**Commit changes:** Added `internal/api/users.go` with a `CreateUser` handler106107**Phase 1 output:**108```109Changed files:110- internal/api/users.go (new file)111 - Added CreateUser handler112 - Added userRequest/userResponse types113 - Added input validation logic114115Existing test coverage: None (new file)116```117118**Phase 2 output:**119```120Test Plan:1211. Unit tests for input validation122 - Valid request succeeds123 - Missing required fields return 400124 - Invalid email format returns 4001252. Integration tests for CreateUser handler126 - Successful user creation returns 201127 - Duplicate email returns 409128 - Database error returns 5001293. Mocks needed: UserRepository interface130```131132**Phase 3 output:**133```134Created: internal/api/users_test.go135- TestCreateUser_Success136- TestCreateUser_MissingFields137- TestCreateUser_InvalidEmail138- TestCreateUser_DuplicateEmail139- TestCreateUser_DatabaseError140```141142**Phase 5 summary:**143```144Files changed in commit: internal/api/users.go145146Tests added:147- internal/api/users_test.go (5 tests covering CreateUser handler)148149New coverage:150- Input validation for user creation requests151- Success and error paths for CreateUser handler152- Database error handling153154Run tests: go test ./internal/api/... -v155```156157---158159Begin by performing the diff-based analysis for HEAD and drafting the test plan before writing or modifying any tests.