Go Tutorial: Testing in Go
Catch bugs before your users do. Go has world-class testing built right in — no framework, no setup, just write and run.
Test once, ship with confidence
Every function you have written so far might be broken. You ran it, it looked right, and you moved on. But what happens when you change something six weeks from now and quietly break something else?
Tests are the answer. Write a test once, run it forever. Every change you make either passes the tests — or tells you exactly what broke and where.
Go's testing is built into the language. No third-party framework. No configuration files. Just a naming convention and the `go test` command.
Twelve steps.
What you'll learn in this Go testing in go tutorial
This interactive Go tutorial has 12 hands-on exercises. Estimated time: 24 minutes.
- What is a test in Go — In Go, a test is just a function with a specific signature:
- t.Errorf — report a failure — `t.Errorf` marks the test as failed and logs a message — but the test keeps running.
- t.Fatal — stop on first failure — Sometimes a failure makes the rest of the test pointless. Use `t.Fatalf` to stop immediately.
- Table-driven tests — The most common Go testing pattern: define a slice of test cases, loop through them.
- Testing error cases — Your tests should cover failure paths too — not just the happy path.
- t.Run — subtests with names — `t.Run` gives each test case its own name. When a test fails, you see exactly which case broke — not just a line number.
- Testing with interfaces — mocking — Real code often depends on external things: databases, APIs, email services. You cannot call those in tests.
- Benchmarks — A benchmark measures how fast your code runs.
- Test helpers with t.Helper() — When you have repeated assertion logic, extract it into a helper function and call `t.Helper()` inside it.
- Test a real function — isPalindrome — Write tests for a function you define yourself.
- Fix the failing test — The test exists. The function is broken.
- Build a tested calculator — No starter code.
Go Testing in Go concepts covered
- Test once, ship with confidence