← Back to list

testing-patterns
by IvanTorresEdge
⭐ 0🍴 1📅 Jan 13, 2026
SKILL.md
name: testing-patterns description: Table-driven tests, mocking strategies, and comprehensive testing patterns. Use when writing tests.
Testing Patterns Skill
Comprehensive Go testing patterns following community best practices.
When to Use
Use this skill when writing or reviewing tests.
Table-Driven 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)
}
})
}
}
Using Testify
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)
}
Mocking with Interfaces
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
}
HTTP Testing
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")
}
Best Practices
- Use table-driven tests
- Test one thing per test
- Use testify for clarity
- Mock external dependencies
- Test edge cases
- Run with race detector:
go test -race
Score
Total Score
60/100
Based on repository quality metrics
✓SKILL.md
SKILL.mdファイルが含まれている
+20
✓LICENSE
ライセンスが設定されている
+10
○説明文
100文字以上の説明がある
0/10
○人気
GitHub Stars 100以上
0/15
○最近の活動
3ヶ月以内に更新がある
0/10
○フォーク
10回以上フォークされている
0/5
✓Issue管理
オープンIssueが50未満
+5
✓言語
プログラミング言語が設定されている
+5
○タグ
1つ以上のタグが設定されている
0/5
Reviews
💬
Reviews coming soon