AgentSkillsCN

Go Testing Strategies

涵盖单元测试、集成测试以及端到端测试在内的各类测试模式。

SKILL.md
--- frontmatter
name: Go Testing Strategies
description: Testing patterns including unit, integration, and E2E tests

Strategy

  • Testing pyramid (70/20/10)
  • testcontainers for integration tests
  • Mocking with testify
  • Benchmarking
  • Race detection
  • Coverage targets (>75%)

Examples

go
// ✅ GOOD: Integration test with testcontainers
func TestIntegration(t *testing.T) {
    ctx := context.Background()
    
    // Start PostgreSQL in Docker
    pgContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
        ContainerRequest: testcontainers.ContainerRequest{
            Image: "postgres:15",
            Env: map[string]string{
                "POSTGRES_USER": "user",
                "POSTGRES_PASSWORD": "password",
                "POSTGRES_DB": "testdb",
            },
        },
    })
    
    // ... setup database, run migrations, test repository
}
go
// ✅ Integration test with real PostgreSQL
func TestRepository_Save(t *testing.T) {
    pgContainer, _ := postgres.RunContainer(ctx, ...)
    defer pgContainer.Terminate(ctx)
    
    db, _ := sql.Open("postgres", connString)
    repo := NewUserRepositoryImpl(db)
    
    err := repo.Save(ctx, user)
    assert.NoError(t, err)
}

Performance Targets

MetricTargetAchieved via
Query execution<50ms P99Connection pooling, caching
Dashboard (5 cards)<250msParallel execution
CSV import50k rows/secStreaming + COPY
Memory (10GB CSV)<50MBStreaming
Concurrent users5000+Worker pools