Table-driven tests, mocking strategies, and comprehensive testing patterns. Use when writing tests.
This skill inherits all available tools. When active, it can use any tool Claude has access to.
Comprehensive Go testing patterns following community best practices.
Use this skill when writing or reviewing tests.
func TestAdd(t *testing.T) {
tests := []struct {
name string
a, b int
want int
}{
{"positive", 1, 2, 3},
{"negative", -1, -2, -3},
{"zero", 0, 0, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Add(tt.a, tt.b)
if got != tt.want {
t.Errorf("Add(%d, %d) = %d; want %d", tt.a, tt.b, got, tt.want)
}
})
}
}
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestService_GetUser(t *testing.T) {
svc := NewService()
user, err := svc.GetUser(1)
require.NoError(t, err)
assert.Equal(t, "John", user.Name)
assert.Greater(t, user.ID, 0)
}
type UserRepository interface {
GetUser(id int) (*User, error)
}
type mockUserRepo struct {
users map[int]*User
}
func (m *mockUserRepo) GetUser(id int) (*User, error) {
if user, ok := m.users[id]; ok {
return user, nil
}
return nil, ErrNotFound
}
func TestService(t *testing.T) {
repo := &mockUserRepo{
users: map[int]*User{
1: {ID: 1, Name: "John"},
},
}
svc := NewService(repo)
// test with mock
}
func TestHandler(t *testing.T) {
req := httptest.NewRequest("GET", "/users/1", nil)
w := httptest.NewRecorder()
handler := NewHandler(mockService)
handler.GetUser(w, req)
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Body.String(), "John")
}
go test -race