Set up Go testing with table-driven tests and mocks
✓Works with OpenClaudeYou are a Go developer setting up comprehensive unit tests. The user wants to write table-driven tests and mock external dependencies in Go.
What to check first
- Run
go versionto confirm Go 1.11+ is installed (required for go modules) - Check if
testingpackage is already imported in your*_test.gofiles - Verify you have a mocking library available—
github.com/golang/mockorgithub.com/stretchr/testify/mockare standard
Steps
- Create a
*_test.gofile in the same package as the code you're testing (e.g.,calculator_test.goforcalculator.go) - Write a table-driven test using a slice of structs with
name,input, andexpectedfields - Loop through the test cases with
for _, tt := range testsand runt.Run(tt.name, ...)for each - Use
t.Errorf()to fail the test with a formatted message if the actual result doesn't match expected - Install the mock generation tool:
go install github.com/golang/mock/mockgen@latest - Generate mocks from interfaces using
mockgen -source=myfile.go -destination=mocks/mock_myfile.go - Import the generated mock package and use
gomock.NewController(t)to create a controller for assertions - Set up mock expectations with
.EXPECT().MethodName().Return(value)before calling the function under test
Code
package calculator
import (
"testing"
"github.com/golang/mock/gomock"
)
func TestAdd(t *testing.T) {
tests := []struct {
name string
a int
b int
expected int
}{
{name: "positive numbers", a: 2, b: 3, expected: 5},
{name: "negative numbers", a: -2, b: -3, expected: -5},
{name: "mixed signs", a: 5, b: -3, expected: 2},
{name: "zero", a: 0, b: 0, expected: 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := Add(tt.a, tt.b)
if result != tt.expected {
t.Errorf("Add(%d, %d) = %d; want %d", tt.a, tt.b, result, tt.expected)
}
})
}
}
// Example with mocks (assuming Logger interface exists)
func TestProcessWithLogger(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockLogger := NewMockLogger(ctrl)
mockLogger.EXPECT().
Log(gomock
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Go Skills
Other Claude Code skills in the same category — free to download.
Go API Setup
Scaffold Go REST API with Gin or Chi router
Go Modules
Manage Go modules and dependency versioning
Go Concurrency
Implement goroutines, channels, and concurrency patterns
Go Docker
Create optimized multi-stage Docker build for Go apps
Go gRPC
Set up gRPC server and client in Go
Go Middleware
Create HTTP middleware chain in Go
Go CLI
Build CLI applications with Cobra in Go
Want a Go skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.