Go Testing Patterns
Table-Driven Tests (Standard Pattern)
func TestFunction(t *testing.T) {
tests := []struct {
name string
input SomeType
wantOutput ExpectedType
wantErr bool
errContains string
}{
{
name: "happy path",
input: SomeType{...},
wantOutput: ExpectedType{...},
wantErr: false,
},
{
name: "error case",
input: SomeType{...},
wantErr: true,
errContains: "expected error message",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := FunctionUnderTest(tt.input)
if tt.wantErr {
assert.Error(t, err)
if tt.errContains != "" {
assert.Contains(t, err.Error(), tt.errContains)
}
return
}
assert.NoError(t, err)
assert.Equal(t, tt.wantOutput, got)
})
}
}
Mockery v3.5 Usage
// Create mock with automatic cleanup
executor := mocks.NewMockForgeExecutor(t)
// Type-safe expectation setting
executor.EXPECT().Execute(
mock.Anything, // context
mock.MatchedBy(func(cfg Config) bool {
return cfg.TestFile != ""
}),
).Return(&Result{Success: true}, nil).Once()
// AssertExpectations called automatically via t.Cleanup
Table-Driven Tests with Mocks
tests := []struct {
name string
setupMocks func(*mocks.MockForgeExecutor)
wantResult *Result
wantErr error
}{
{
name: "successful execution",
setupMocks: func(m *mocks.MockForgeExecutor) {
m.EXPECT().Execute(mock.Anything, mock.Anything).
Return(&Result{Success: true}, nil).Once()
},
wantResult: &Result{Success: true},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
executor := mocks.NewMockForgeExecutor(t)
tt.setupMocks(executor)
// ... test logic
})
}
Mock Commands
task mocks # Generate all mocks (regenerates from scratch)
Test Organization
- Test behaviors, not implementation - Focus on public APIs
- Use
t.Helper()for test utilities - Coverage targets: ≥80% unit tests, ≥70% integration tests
- Run with race detector:
task test-race
Security in Tests
NEVER hardcode secrets!
// WRONG
const testAPIKey = "sk-test-12345"
// RIGHT
apiKey := "test-" + generateRandomString(32)
Build Tags
All test commands require the -tags=testmode build tag:
gotestsum --format pkgname -- -tags=testmode -run TestFunctionName ./internal/hooks/...
Test Execution
task test # Fast tests (-short, 30s timeout)
task test-race # Tests with race detector
task coverage # HTML coverage report
Converted and distributed by TomeVault — claim your Tome and manage your conversions.