test-go
Go Test Skill
Generate unit tests by analyzing the source file's functions, branches, and logic flow.
Commands
| Command | Description |
|---|---|
/test-go create <path> |
Create tests for a file, folder, or module |
/test-go update <path> |
Update existing tests |
<path> can be:
- A file:
user.goor#File - A folder:
internal/usecase/or#Folder - A module/package name:
auth,user
Tip: Use Kiro's #File or #Folder context to quickly reference paths.
Workflow
Step 1: Analyze Source File
Read the source file and identify:
- All exported functions/methods
- All code branches (if/else, switch, for loops)
- All error return paths
- All dependencies (interfaces) that need mocking
- All possible panic scenarios
Step 2: Create Test Plan
For each function/method found:
- List all execution paths
- Identify inputs that trigger each path
- Determine expected outputs for each path
- Note edge cases (nil, empty, zero, boundary)
Step 3: Generate Tests
Create table-driven tests that cover every path identified in Step 2.
Coverage Target: 80%+
Every test file must cover:
- All exported functions
- All if/else branches
- All switch cases
- All error return statements
- All loop conditions
Consistent Pattern
func TestFunctionName(t *testing.T) {
tests := []struct {
name string
input InputType
want OutputType
wantErr bool
}{
// Test cases for each execution path
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := FunctionName(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}
Naming Convention
- Test file:
[source-file]_test.go(same directory) - Test function:
Test[FunctionName] - Test case name: Describes the scenario being tested
Mocking
Use interfaces for dependencies and create mock implementations:
type mockDep struct {
funcName func(args) (result, error)
}
Running Tests
go test ./... # Run all
go test -cover ./... # With coverage
go test -coverprofile=coverage.out ./... # Generate report
More from habonn/portal-skills
daily-commit-summary
|
26commit
Smart git commit workflow using Conventional Commits format with AI-generated commit message suggestions based on staged changes.
22skill-auditor
|
11e2e
Create or update Playwright E2E tests following the project's Page Object Model structure. Use /e2e create for new modules or /e2e update for existing ones.
6sprint-commit-summary
|
6test-ts
Generate TypeScript/Vitest unit tests by analyzing source file flow and ensuring 80%+ coverage.
5