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: MIT5compatibility:6 - vcs:git7 - runtime:any8allowed-tools:9 - Read10 - Glob11 - Grep12 - Bash(git:diff)13 - Bash(git:log)14 - Bash(git:show)15 - Bash(git:status)16 - Bash(git:rev-parse)17metadata:18 source: https://skillmd.com/skills/thoreinstein/add-test-coverage19 author: thoreinstein20 version: 1.0.021---2223# Add Test Coverage2425Analyze the most recent commit (HEAD) and implement all tests required to cover new and modified code.2627## When to Use This Skill2829- After completing a feature or fix, to ensure adequate test coverage before merge30- When a commit introduces new functions, methods, or API endpoints31- When modifying existing behavior that may not have sufficient tests32- During code review when test coverage gaps are identified3334## Workflow3536### Phase 1: Change Analysis37381. Identify changes in the latest commit:39 - Diff HEAD against its parent (HEAD~1), or against the appropriate base branch if more accurate402. Classify each changed file by type based on file extensions and project structure:41 - Backend code (e.g., `.go`, `.py`, `.java`, `.rb`)42 - Frontend code (e.g., `.js`, `.ts`, `.jsx`, `.tsx`, `.vue`, `.svelte`)43 - Integration/API surfaces (e.g., handlers, controllers, routes)44 - Config, infrastructure, or test-only changes453. For each changed area, determine:46 - What behavior was added or modified47 - Which existing tests (if any) already cover this behavior48 - Where new or expanded tests are needed4950### Phase 2: Test Plan Design5152Draft a concrete test plan for this commit:53541. **Scenarios to cover:**55 - List specific behaviors that need tests56 - Identify edge cases and error conditions57582. **Test layers to use:**59 - Unit tests for individual functions/methods60 - Integration tests for cross-component behavior61 - E2E tests for user-visible flows (if applicable)62633. **Test dependencies:**64 - Required fixtures, mocks, or test data65 - Any setup/teardown requirements66674. **Prioritization criteria:**68 - Safety-critical paths69 - Public/externally visible behavior70 - Complex logic and branching71 - Previously under-tested areas7273### Phase 3: Test Implementation7475Implement the tests specified in your plan:76771. **Follow existing conventions:**78 - Use existing test directories and naming patterns79 - Reuse shared test utilities and helpers80 - Match the style of existing tests in the repository81822. **Test quality standards:**83 - Each test should be focused on a single behavior84 - Tests must be deterministic (no flaky tests)85 - Tests should be readable and self-documenting86 - Include clear assertion messages8788### Phase 4: Execution and Refinement89901. **Incremental testing:**91 - Run only the relevant tests while developing92 - Use focused test commands (specific packages, files, or test names)93942. **Validation:**95 - Once confident, run a broader test subset to check for regressions96 - Ensure all new tests pass consistently97983. **Failure diagnosis:**99 - If tests fail, prefer fixing implementation bugs exposed by the tests100 - Only adjust tests when they don't match the correct intended behavior101102### Phase 5: Summary103104Produce a concise summary including:105106- Which files changed in the commit107- What tests were added or modified (by file and purpose)108- What behaviors are now covered that were not before109- The exact commands to run the relevant test suites110111## Constraints112113- **Do not remove or disable existing tests** unless clearly invalid; explain any such changes114- **Do not introduce new frameworks** or major structural changes; work within the existing test stack115- **Keep changes tightly scoped** to covering the latest commit's behavior, not the entire repository116117## Examples118119### Example: New HTTP Handler in Go120121**Commit changes:** Added `internal/api/users.go` with a `CreateUser` handler122123**Phase 1 output:**124```125Changed files:126- internal/api/users.go (new file)127 - Added CreateUser handler128 - Added userRequest/userResponse types129 - Added input validation logic130131Existing test coverage: None (new file)132```133134**Phase 2 output:**135```136Test Plan:1371. Unit tests for input validation138 - Valid request succeeds139 - Missing required fields return 400140 - Invalid email format returns 4001412. Integration tests for CreateUser handler142 - Successful user creation returns 201143 - Duplicate email returns 409144 - Database error returns 5001453. Mocks needed: UserRepository interface146```147148**Phase 3 output:**149```150Created: internal/api/users_test.go151- TestCreateUser_Success152- TestCreateUser_MissingFields153- TestCreateUser_InvalidEmail154- TestCreateUser_DuplicateEmail155- TestCreateUser_DatabaseError156```157158**Phase 5 summary:**159```160Files changed in commit: internal/api/users.go161162Tests added:163- internal/api/users_test.go (5 tests covering CreateUser handler)164165New coverage:166- Input validation for user creation requests167- Success and error paths for CreateUser handler168- Database error handling169170Run tests: go test ./internal/api/... -v171```172173---174175Begin by performing the diff-based analysis for HEAD and drafting the test plan before writing or modifying any tests.