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/integration-tests-api-migrations-and-fixtures.md
View on GitHub# Integration Tests — Migration Tests and Fixture Loading
## Testing Migrations
Verify migrations apply cleanly and the schema matches expected structure.
```go
//go:build integration
package repository_test
func TestMigrations_UpAndDown(t *testing.T) {
ctx := context.Background()
pgc, err := tcpostgres.RunContainer(ctx,
testcontainers.WithImage("postgres:16-alpine"),
tcpostgres.WithDatabase("migrate_test"), tcpostgres.WithUsername("u"), tcpostgres.WithPassword("p"),
testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").WithOccurrence(2).WithStartupTimeout(20*time.Second)),
)
if err != nil { t.Fatalf("start container: %v", err) }
t.Cleanup(func() { pgc.Terminate(ctx) })
host, _ := pgc.Host(ctx)
port, _ := pgc.MappedPort(ctx, "5432")
dsn := fmt.Sprintf("host=%s port=%s user=u password=p dbname=migrate_test sslmode=disable", host, port.Port())
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil { t.Fatalf("gorm.Open: %v", err) }
sqlDB, _ := db.DB()
driver, err := migratepg.WithInstance(sqlDB, &migratepg.Config{})
if err != nil { t.Fatalf("migrate driver: %v", err) }
m, err := migrate.NewWithDatabaseInstance("file://../../migrations", "postgres", driver)
if err != nil { t.Fatalf("migrate.New: %v", err) }
// Apply all migrations
if err := m.Up(); err != nil && err != migrate.ErrNoChange { t.Fatalf("migrate Up: %v", err) }
// Verify expected tables exist
var tableName string
row := sqlDB.QueryRow("SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_name='users'")
if err := row.Scan(&tableName); err != nil { t.Fatalf("users table not found after migration: %v", err) }
// Roll back all migrations
if err := m.Down(); err != nil && err != migrate.ErrNoChange { t.Fatalf("migrate Down: %v", err) }
}
```
---
## Fixture Loading
Load SQL fixture files to populate a known database state before tests.
```go
//go:build integration
package testdb
// LoadFixture executes SQL from a fixture file against the given db.
// Fixture files live in internal/testutil/fixtures/*.sql
func LoadFixture(t *testing.T, db *gorm.DB, path string) {
t.Helper()
sql, err := os.ReadFile(path)
if err != nil { t.Fatalf("LoadFixture: read %q: %v", path, err) }
if err := db.Exec(string(sql)).Error; err != nil { t.Fatalf("LoadFixture: exec %q: %v", path, err) }
}
```
Example fixture `internal/testutil/fixtures/users.sql`:
```sql
INSERT INTO users (id, name, email, role, created_at, updated_at) VALUES
('seed-user-1', 'Alice', 'alice@example.com', 'user', NOW(), NOW()),
('seed-user-2', 'Bob', 'bob@example.com', 'user', NOW(), NOW()),
('seed-admin', 'Admin', 'admin@example.com', 'admin', NOW(), NOW());
```
Usage:
```go
//go:build integration
func TestUserRepository_List_WithFixtures(t *testing.T) {
t.Cleanup(func() { testDB.Exec("TRUNCATE TABLE users RESTART IDENTITY CASCADE") })
testdb.LoadFixture(t, testDB, "../../testutil/fixtures/users.sql")
users, total, err := repository.NewUserRepository(testDB).List(context.Background(), domain.ListOptions{Page: 1, Limit: 10})
if err != nil { t.Fatalf("List: %v", err) }
if total != 3 { t.Errorf("want 3 users from fixture, got %d", total) }
_ = users
}
```
> For setup, TestMain, and DB helper: see [integration-tests-setup-and-testmain.md](integration-tests-setup-and-testmain.md).
> For cleanup patterns and repository/API tests: see [integration-tests-cleanup-and-repository.md](integration-tests-cleanup-and-repository.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 |