Testing checks that your Go code is correct before it ships, while monitoring observes how the running program behaves in production. Tests answer "does this code do what we intended?" and run on demand with go test. Monitoring answers "is the system healthy right now, and how is it changing?" and runs continuously through logs, metrics, traces and alerts.
You need both. Tests catch known failure modes early and cheaply, but they cannot reproduce real traffic, real data or real infrastructure failures. Monitoring catches what tests missed, but it only tells you after users are affected.
| Aspect | Testing | Monitoring |
|---|---|---|
| Question answered | Is the code correct? | Is the running system healthy? |
| When it runs | Before merge and release, in CI | Continuously, in staging and production |
| Input | Controlled, hand-written cases | Real traffic and real data |
| Typical Go tools | testing, go test, -race, fuzzing, testify | log/slog, Prometheus, OpenTelemetry, Grafana |
| Failure signal | A red build | An alert or a degraded dashboard |
| Cost of a miss | A bug reaches users | An outage lasts longer than it should |
The standard library ships everything you need for the basics, so most Go projects start without a third-party framework.
A unit test lives in a _test.go file next to the code and exercises one function. The idiomatic Go style is the table-driven test, with a subtest per case.
Run it with go test ./.... Add -race to enable the data race detector and -cover to see coverage.
Benchmarks measure performance and are part of the same testing package. Since Go 1.24 you can loop with b.Loop(), and the older for range b.N form still works.
Native fuzzing (func FuzzXxx(f *testing.F), available since Go 1.18) generates inputs automatically and is useful for parsers and decoders. Integration tests, which talk to a real database or HTTP server, are usually separated with build tags or run with -short to skip them locally. HTTP handlers can be tested without a network using net/http/httptest.
Monitoring is built into the program, so you add instrumentation while writing it. The three common signals are logs, metrics and traces.
Since Go 1.21 the standard library includes log/slog for structured logs that log platforms can parse.
Metrics are numbers you can aggregate, graph and alert on. The official Prometheus client exposes them over HTTP on a /metrics endpoint.
Use a fixed route name as the label, not r.URL.Path. Raw paths with IDs create an unbounded number of time series and can overwhelm Prometheus.
Point Prometheus at the endpoint with a scrape configuration:
Grafana then connects to Prometheus as a data source to build dashboards for request rate, error rate and latency.
net/http/pprof and runtime/pprof expose CPU and memory profiles from a live process. Protect the pprof endpoints; never expose them publicly.-race.Monitoring feeds testing: every incident should end with a new test that would have caught it.
No. Monitoring detects problems after they reach users, while tests prevent most of them from shipping. Relying on monitoring alone turns your users into the test suite.
testing package enough?For most projects, yes. Add testify for assertions or gomock and similar tools only when they remove real boilerplate.
Logs are individual events, metrics are aggregated numbers over time, and traces follow a single request across components. Metrics tell you something is wrong, traces show where, and logs show why.
Test the behaviour that matters, such as a counter incrementing on a failed request. Do not assert on exact log text unless another system depends on it.
Testing and monitoring are complementary parts of one quality strategy. Use go test, table-driven tests, -race, benchmarks and fuzzing to prove correctness before release. Use log/slog, Prometheus metrics, OpenTelemetry traces and alerts to watch real behaviour afterwards, and turn every production incident into a regression test.