Generate Go tests for a package or file
Generate comprehensive tests for {{target}}.
Use table-driven tests with t.Run() subtests:
tests := []struct {
name string
input Type
want Type
wantErr bool
}{
// test cases
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// test logic
})
}
Test coverage priorities:
For node tests, mock external dependencies:
httptest.NewServert.TempDir()File placement: Create *_test.go adjacent to source file
Naming conventions:
TestFunctionNamefilename_test.gopackage nodes
import (
"context"
"testing"
)
func TestNodeType_Execute(t *testing.T) {
tests := []struct {
name string
config map[string]interface{}
want interface{}
wantErr bool
}{
{
name: "valid config",
config: map[string]interface{}{
"key": "value",
},
want: expectedResult,
wantErr: false,
},
{
name: "missing required field",
config: map[string]interface{}{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
n := &NodeType{}
got, err := n.Execute(context.Background(), tt.config)
if (err != nil) != tt.wantErr {
t.Errorf("Execute() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && got.Data != tt.want {
t.Errorf("Execute() = %v, want %v", got.Data, tt.want)
}
})
}
}