一键导入
go-integration-test
Use when writing integration tests for Go services that need real PostgreSQL, Redis, or other external dependencies via testcontainers
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use when writing integration tests for Go services that need real PostgreSQL, Redis, or other external dependencies via testcontainers
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | go-integration-test |
| description | Use when writing integration tests for Go services that need real PostgreSQL, Redis, or other external dependencies via testcontainers |
Integration tests with real dependencies using testcontainers-go. No mocks for infrastructure — test against actual Postgres and Redis.
Core principle: If it talks to a database, test it with a real database.
//go:build integration
package postgres_test
import (
"context"
"testing"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/testcontainers/testcontainers-go/wait"
)
func setupPostgres(t *testing.T) *pgxpool.Pool {
t.Helper()
ctx := context.Background()
container, err := postgres.Run(ctx,
"postgres:16-alpine",
postgres.WithDatabase("testdb"),
postgres.WithUsername("test"),
postgres.WithPassword("test"),
testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").
WithOccurrence(2)),
)
require.NoError(t, err)
t.Cleanup(func() { container.Terminate(ctx) })
connStr, err := container.ConnectionString(ctx, "sslmode=disable")
require.NoError(t, err)
pool, err := pgxpool.New(ctx, connStr)
require.NoError(t, err)
t.Cleanup(pool.Close)
// Run migrations
runMigrations(t, connStr)
return pool
}
func runMigrations(t *testing.T, connStr string) {
t.Helper()
m, err := migrate.New("file://../../migrations", connStr)
require.NoError(t, err)
err = m.Up()
require.NoError(t, err)
}
// Option 1: Transaction rollback (fastest)
func withTx(t *testing.T, pool *pgxpool.Pool) pgx.Tx {
t.Helper()
tx, err := pool.Begin(context.Background())
require.NoError(t, err)
t.Cleanup(func() { tx.Rollback(context.Background()) })
return tx
}
// Option 2: Truncate tables between tests
func cleanTables(t *testing.T, pool *pgxpool.Pool, tables ...string) {
t.Helper()
for _, table := range tables {
_, err := pool.Exec(context.Background(),
"TRUNCATE TABLE "+table+" CASCADE")
require.NoError(t, err)
}
}
func TestUserRepository_Create(t *testing.T) {
pool := setupPostgres(t)
repo := postgres.NewUserRepository(pool)
user, err := domain.NewUser("test@example.com", "Test User")
require.NoError(t, err)
err = repo.Create(context.Background(), user)
require.NoError(t, err)
found, err := repo.GetByID(context.Background(), user.ID)
require.NoError(t, err)
assert.Equal(t, user.Email, found.Email)
}
func TestCreateUser_Integration(t *testing.T) {
pool := setupPostgres(t)
app := setupApp(pool) // wire up fiber app with real deps
body := `{"email":"test@example.com","name":"Test"}`
req := httptest.NewRequest("POST", "/api/v1/users", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
resp, err := app.Test(req)
require.NoError(t, err)
assert.Equal(t, 201, resp.StatusCode)
}
test:
go test ./... -race -count=1
test-integration:
go test ./... -race -count=1 -tags=integration
test-all:
go test ./... -race -count=1 -tags=integration
//go:build integration tag — tests run in CI without Dockert.Parallel() carefully — shared state conflictsUse when the user wants to free disk space, investigate disk usage, clean caches, remove unused artifacts, or when disk capacity is high. Also use when asked to "clean up", "free space", "disk full", or "what's using disk space". Always uses AskUserQuestion for every deletion decision.
Use at the start of every conversation and for every user message — orchestrates the full engineering skills suite by understanding intent, clarifying requirements interactively, and invoking the right skills
Use when refactoring Go code, cleaning up legacy codebases, optimizing performance, or enforcing clean architecture boundaries in a Go/Fiber backend
Use when refactoring Python code, cleaning up legacy codebases, optimizing performance, enforcing type safety, or improving clean architecture in a FastAPI backend
Use when refactoring React components, cleaning up legacy frontends, optimizing performance, improving component architecture, or reducing bundle size
Use when reviewing changed code before committing or after completing a feature — checks performance, architecture, type safety, and code quality against project standards