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 8 hands-on exercises. Estimated time: 24 minutes.
- What is a test in Go — A test is just a function that checks whether your code does what it should. In Go, tests follow one rule: `func TestXxx…
- t.Errorf — report a failure — `t.Errorf` marks the test as failed and logs a message but the test keeps running. Add a second test case: create an emp…
- Table-driven tests — The most common Go testing pattern: define a slice of test cases, loop through them. Add two more cases: searching for "…
- t.Run — subtests with names — `t.Run` gives each test case its own name. When a test fails, you see exactly which case broke. Wrap each case in `t.Run…
- Testing with interfaces — mocking — Define dependencies as interfaces so you can swap in fake implementations in tests. Run this — `mockService` returns a c…
- Test helpers with t.Helper() — Extract repeated assertion logic into a helper function and call `t.Helper()` inside it. Add a fourth call that checks "…
- Fix the failing test — `lookupByISBN` is broken — it compares `Title` instead of `ISBN`. Fix it so all 4 test cases pass.
- Build a tested library catalog — Build a library catalog with full test coverage. Define `Book` and `Library`, implement `addBook`, `findByISBN`, `listBo…
Go Testing in Go concepts covered
- Test once, ship with confidence