
Testing a Go Service in Microseconds: The Hexagonal Testing Strategy
Your Go tests take 3 minutes because every "unit test" spins up PostgreSQL in Docker. That's not a testing problem. That's an architecture problem. When your business logic is tangled with your database calls, the only way to test it is to bring the database along. Hexagonal architecture untangles them — and testing becomes almost mechanical. Three Layers, Three Strategies Layer 1: Domain Tests — Microseconds The domain is pure Go. No imports from database/sql , net/http , or any external package. Testing it is trivial: func TestNewOrder_RejectsEmpty ( t * testing . T ) { _ , err := NewOrder ( "ord-1" , "cust-1" , nil ) if ! errors . Is ( err , ErrOrderEmpty ) { t . Errorf ( "got %v, want ErrOrderEmpty" , err ) } } func TestOrder_Confirm ( t * testing . T ) { order := validOrder ( t ) if err := order . Confirm (); err != nil { t . Fatalf ( "unexpected error: %v" , err ) } if order . Status != OrderStatusConfirmed { t . Errorf ( "status = %q, want confirmed" , order . Status ) } } No se
Continue reading on Dev.to
Opens in a new tab

