henriqueatila/golang-gin-best-practices / skills/golang-gin-testing
golang-gin-testing
VerifiedTest Go Gin APIs with httptest, table-driven tests, testcontainers. Use when writing tests for Gin handlers, services, middleware, or setting up integration and e2e tests.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 7.7 KB |
| metadata.json | config | 495 B |
| references/e2e-cicd-config-and-cleanup.md | markdown | 5.1 KB |
| references/e2e-structure-and-critical-flow.md | markdown | 6.9 KB |
| references/integration-tests-api-migrations-and-fixtures.md | markdown | 3.6 KB |
| references/integration-tests-cleanup-and-repository.md | markdown | 5.8 KB |
| references/integration-tests-setup-and-testmain.md | markdown | 4.3 KB |
| references/load-testing-benchmarks-and-vegeta.md | markdown | 2.1 KB |
| references/load-testing-k6-and-ci-regression.md | markdown | 3.1 KB |
| references/test-patterns-helpers-and-handler-tests.md | markdown | 4.8 KB |
| references/test-patterns-service-tests-and-running.md | markdown | 2.8 KB |
| references/unit-tests-benchmarks-fuzz-and-golden.md | markdown | 5.1 KB |
| references/unit-tests-fixtures-helpers-and-middleware.md | markdown | 4.7 KB |
| references/unit-tests-handler-and-json.md | markdown | 5.8 KB |
| references/unit-tests-service-mocks-and-table-driven.md | markdown | 5.6 KB |
references/test-patterns-service-tests-and-running.md
View on GitHub# Test Patterns — Service Tests with Mocked Repository and Running Tests
## Service Tests with Mocked Repository
Test business logic without touching the database. The mock implements `domain.UserRepository`.
```go
// internal/service/user_service_test.go
package service_test
// mockUserRepository implements domain.UserRepository for service tests.
type mockUserRepository struct {
createFn func(ctx context.Context, user *domain.User) error
getByEmailFn func(ctx context.Context, email string) (*domain.User, error)
getByIDFn func(ctx context.Context, id string) (*domain.User, error)
}
func (m *mockUserRepository) Create(ctx context.Context, user *domain.User) error {
return m.createFn(ctx, user)
}
func (m *mockUserRepository) GetByEmail(ctx context.Context, email string) (*domain.User, error) {
return m.getByEmailFn(ctx, email)
}
func (m *mockUserRepository) GetByID(ctx context.Context, id string) (*domain.User, error) {
if m.getByIDFn != nil { return m.getByIDFn(ctx, id) }
return nil, domain.ErrNotFound
}
func (m *mockUserRepository) List(ctx context.Context, opts domain.ListOptions) ([]domain.User, int64, error) { return nil, 0, nil }
func (m *mockUserRepository) Update(ctx context.Context, user *domain.User) error { return nil }
func (m *mockUserRepository) Delete(ctx context.Context, id string) error { return nil }
func TestUserService_Create_DuplicateEmail(t *testing.T) {
repo := &mockUserRepository{
getByEmailFn: func(_ context.Context, email string) (*domain.User, error) {
return &domain.User{Email: email}, nil // email already taken
},
createFn: func(_ context.Context, user *domain.User) error { return nil },
}
svc := service.NewUserService(repo, slog.Default())
_, err := svc.Create(context.Background(), domain.CreateUserRequest{
Name: "Alice", Email: "alice@example.com", Password: "secret123",
})
// Use errors.As to unwrap *AppError and inspect the HTTP status code.
// errors.As is always safe; errors.Is only works if AppError implements Is().
var appErr *domain.AppError
if !errors.As(err, &appErr) || appErr.Code != 409 {
t.Errorf("expected ErrConflict (409 AppError), got %v", err)
}
}
```
---
## Running Tests
```bash
# All tests with race detector and coverage
go test -v -race -cover ./...
# Specific package
go test -v -race ./internal/handler/...
# Unit tests only (exclude integration build tag)
go test -v -race -cover -tags='!integration' ./...
# Integration tests only (requires Docker or live DB)
go test -v -race -tags=integration ./internal/repository/...
# Coverage report
go test -race -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
```
> For test helpers and handler tests: see [test-patterns-helpers-and-handler-tests.md](test-patterns-helpers-and-handler-tests.md).
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |