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/load-testing-benchmarks-and-vegeta.md
View on GitHub# Load Testing — Go Benchmarks and Vegeta
## Go Benchmark Tests
Use `testing.B` with `httptest` to benchmark handlers without a running server.
```go
// internal/handler/user_handler_bench_test.go
package handler_test
func BenchmarkGetUserHandler(b *testing.B) {
gin.SetMode(gin.TestMode)
svc := &mockUserService{
getByIDFn: func(ctx context.Context, id uint) (*domain.User, error) {
return &domain.User{ID: id, Name: "Alice"}, nil
},
}
router := gin.New()
h := NewUserHandler(svc)
router.GET("/users/:id", h.GetByID)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/users/1", nil)
router.ServeHTTP(w, req)
}
}
```
```bash
go test -bench=BenchmarkGetUserHandler -benchmem -count=3 ./internal/handler/...
# BenchmarkGetUserHandler-8 120000 9823 ns/op 2048 B/op 18 allocs/op
```
Flags: `-benchmem` shows B/op + allocs/op; `-count=3` for stable results; `-benchtime=5s` for slow handlers.
---
## Vegeta Load Testing
CLI-based HTTP load tester. Good for quick rate/duration sweeps.
```bash
go install github.com/tsenart/vegeta@latest
```
**GET — 30s at 100 RPS:**
```bash
echo "GET http://localhost:8080/api/v1/users/1" | vegeta attack -duration=30s -rate=100 | vegeta report
```
**POST with JSON body** — create `targets.txt`:
```
POST http://localhost:8080/api/v1/users
Content-Type: application/json
@body.json
```
```bash
vegeta attack -targets=targets.txt -duration=30s -rate=50 | vegeta report
```
**Plot latency histogram:**
```bash
echo "GET http://localhost:8080/api/v1/users/1" \
| vegeta attack -duration=30s -rate=100 | tee results.bin | vegeta report
vegeta plot results.bin > report.html
```
**Ramp up rate:**
```bash
for rate in 50 100 200 500; do
echo "GET http://localhost:8080/api/v1/users/1" \
| vegeta attack -duration=10s -rate=$rate | vegeta report --type=text
done
```
> For k6, performance targets, and CI benchmark regression detection: see [load-testing-k6-and-ci-regression.md](load-testing-k6-and-ci-regression.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 |