Table-driven tests for business logic
Table-driven tests are the idiomatic pattern in Go for verifying business rules, domain entities, and data transformations. You define a slice of test cases containing inputs and expected outputs, iterating over them with t.Run. This structure makes adding new boundary conditions straightforward without duplicating assertion boilerplate.
Keep test cases readable by naming each scenario clearly. When an assertion fails, the test runner points directly to the subtest name, pinpointing whether an empty string, an invalid enum value, or a boundary condition caused the regression.
Integration tests with real PostgreSQL in containers
Testing database repositories against in-memory emulators or SQL driver mocks often hides dialect discrepancies, constraint failures, and transaction rollbacks. Running integration tests against an ephemeral PostgreSQL instance launched in a container gives an accurate validation of schema migrations, complex queries, and concurrency locks.
- Execute actual database migrations before running test suites against the container.
- Wrap test cases in database transactions that roll back on cleanup, or truncate modified tables between suites.
- Avoid shared mutable database state between parallel subtests to prevent unexpected locks.
Mocks belong at system boundaries
Over-mocking internal layers makes refactoring painful, as tests couple to implementation details instead of behavior. In Go, interfaces should be small, defined where they are consumed, and reserved for external boundaries such as third-party payment gateways, message brokers, and email services.
Write lightweight stubs or generate interface mocks only for boundaries that communicate outside the process. Your domain handlers and service layers should interact with concrete types or real dependencies whenever local execution is deterministic.
Testing HTTP handlers with httptest
The standard library net/http/httptest package allows testing HTTP routers, middlewares, and handlers without binding to a physical network port. Using httptest.NewRecorder and httptest.NewRequest lets you verify status codes, response headers, and serialized JSON payloads directly in memory.
Validate middleware chains for authentication, request tracing, and payload validation in isolation. This keeps route handler tests focused on payload decoding, domain delegation, and response status mapping.
Detecting concurrency issues with the race detector
Go makes concurrent programming straightforward with goroutines and channels, but subtle data races can pass standard unit tests while causing silent state corruption in production. Running your test suite with the race detector enabled identifies unsynchronized memory accesses across concurrent routines.
Make running tests with the race flag a mandatory check in continuous integration pipelines. Catching shared map mutations or unprotected pointer access early prevents critical runtime panics under real traffic.
What to test in an MVP and what to leave for later
When developing an early-stage version, attempting full test coverage across rapidly changing interfaces slows down development. Testing effort must focus on areas where failures cause data loss or corrupt business state: Our approach to backends in Go is described on the Go backend development page.
- Core domain calculations, state machine transitions, and financial rules.
- Database migrations and storage constraints for primary domain entities.
- Critical external integrations that handle authentication and external callbacks.
- Omit end-to-end browser flows, edge-case validation of transient UI requests, and redundant tests of standard library behavior.