Apply Go testing patterns and best practices. Use when writing tests, setting up mocks, creating test fixtures, or reviewing test code. Includes table-driven tests and Mockery v3.5 patterns.
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)
})
}
}
// 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
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
})
}
task mocks # Generate all mocks (regenerates from scratch)
t.Helper() for test utilitiestask test-raceNEVER hardcode secrets!
// WRONG
const testAPIKey = "sk-test-12345"
// RIGHT
apiKey := "test-" + generateRandomString(32)
All test commands require the -tags=testmode build tag:
gotestsum --format pkgname -- -tags=testmode -run TestFunctionName ./internal/hooks/...
task test # Fast tests (-short, 30s timeout)
task test-race # Tests with race detector
task coverage # HTML coverage report