| name | write-tests |
| description | Write unit tests for Go code following the insights-operator project conventions |
write-tests
Write unit tests for Go code following the insights-operator project conventions.
Usage
/write-tests <source-file> [test-file]
Arguments:
<source-file> - Path to the Go source file containing the functions to test
[test-file] - Optional path to the test file (defaults to <source-file>_test.go)
What this skill does
- Reads the source file to identify all exported and unexported functions
- Reads the existing test file (if it exists) to identify what's already tested
- Analyzes each function to identify critical code paths (error paths, branches, edge cases)
- Generates minimal table-driven tests covering all code paths
- Follows insights-operator conventions:
- Test method naming:
Test_<FunctionName> or Test_<GatherName>_<FunctionName>
- Table-driven test structure
- Uses
testify/assert for assertions
- Uses fake Kubernetes clients when needed
- Imports grouped: stdlib, external dependencies, current project
- Focuses on path coverage, not exhaustive edge cases
Examples
/write-tests pkg/utils/myutil.go
/write-tests pkg/gather/data.go pkg/gather/data_custom_test.go
/write-tests pkg/gatherers/clusterconfig/gather_nodes.go
prompt: |
You are writing unit tests for the insights-operator project. Follow these strict guidelines:
Project Test Conventions (from STYLEGUIDE.md)
- Use table-driven tests
- Method names:
Test_<FunctionName> or Test_<GatherName>_<FunctionName>
- Import groups: stdlib, external dependencies, current project
- Use
github.com/stretchr/testify/assert for assertions
- Use
k8s.io/client-go/kubernetes/fake for Kubernetes client mocks
Your Task
- Read the source file: {{arg1}}
- Read the test file (if exists): {{arg2 || arg1 with _test.go suffix}}
- Identify untested functions or functions with incomplete coverage
- For each function, identify the critical code paths:
- Error return paths
- Conditional branches (if/else, switch cases)
- Loop edge cases (empty, single item, multiple items)
- Nil/empty input handling
- Boundary conditions
- Write MINIMAL tests that cover these paths - avoid testing the same logic multiple times
- Use table-driven structure with clear test case names
Test Structure Pattern
func Test_FunctionName(t *testing.T) {
tests := []struct {
name string
want expectedType
wantErr error
}{
{
name: "descriptive case name",
want: expected,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
})
}
}
What NOT to do
- Don't create dozens of similar test cases - one per path is enough
- Don't test trivial getter/setter functions
- Don't duplicate tests that already exist
- Don't add tests for unexported helper functions unless they have complex logic
- Don't over-test - focus on the essential paths
Process
- First, output a brief analysis of what functions need testing
- Then write or update the test file
- Explain what coverage was added
Source file: {{arg1}}
Test file: {{arg2}}