1 / 11
Build a complete, practical testing strategy for Go services: table-driven tests and subtests, go-cmp, hand-written fakes and generated mocks, httptest for handlers and clients, in-process and end-to-end server tests, real SQLite database tests, contract tests, Testcontainers, the race detector…
A Practical Strategy from Unit Tests to HTTP, Databases, Fuzzing, Concurrency, and CI
Read before you buy: the PDF here is a free sample — the first 20 pages of the 522-page book (payments, sales and reviews on this demo site are sample data). Get the full e-book at thenexbooks on Gumroad.
Turn Go's simple testing tools into a test suite you can trust — for every layer of a real service.
Go makes the first test easy: the testing package ships with the language and go test needs no setup. What is hard is everything after that. Which parts deserve unit tests and which need a real database? When is a fake better than a mock? How do you test an HTTP client that retries, a background worker driven by a ticker, or code that depends on the clock? Why does a test pass alone and fail in CI? This book answers those questions with working code, real output, and honest trade-offs.
You will take a working but barely tested Go service and build its complete test suite chapter by chapter — running every command and reading every failure — until you can change any part of it with confidence.
What's inside
- A testing strategy for Go: the test pyramid and its critics, and what to test at each layer
- The testing package and go test in depth: failures, flags, caching, subtests, parallel tests, TestMain, cleanup, testdata, and the newer t.Context, t.Chdir, and t.ArtifactDir
- Table-driven tests, clear failure messages, go-cmp and cmpopts, error checks with errors.Is and errors.As, and an honest look at testify
- Designing for testability: small consumer-side interfaces, dependency injection, and seams
- Hand-written stubs, fakes, and spies, generated mocks with go.uber.org/mock, and when each is the right tool
- Testing time, randomness, the environment, error paths, edge cases, and panics
- HTTP: handlers and middleware with httptest, JSON APIs, HTTP clients against fake servers with timeouts and retries, the whole server in-process, and end-to-end tests of the real binary
- Databases: real SQLite in every test run, repository contract tests, test data builders, isolation, build tags, short mode, and Testcontainers for PostgreSQL
- Concurrency: the race detector, goroutine leaks with goleak, cancellation, and deterministic tests with testing/synctest
- Golden files, example tests, fuzzing that finds a real bug, and benchmarks with b.Loop, allocation guards, and benchstat
- Coverage for unit and integration tests, curing flaky and slow tests, a complete GitHub Actions pipeline, and getting legacy Go code under test
One running example, start to finish. You test Paddlebook, the booking service of Larchmere Paddle Co., a fictional dock that rents kayaks, canoes, and paddleboards by the hour. It has pricing and refund rules, booking codes, in-memory and SQLite storage, a weather API client that can stop a booking when it is too windy, a background worker that expires unpaid holds, a daily launch manifest, and a JSON HTTP API. It starts with six tests and ends with a complete, trustworthy suite and CI pipeline.
Current and honest. The examples target Go 1.27 and run on Go 1.26 and later; version-specific features are labeled. Test output in the book is real output, and there are no invented benchmarks or statistics. Every chapter includes runnable code, Tips, Notes, Warnings, Try It Yourself exercises, and Key Takeaways.
Format: PDF, 522 pages (A4), linked table of contents and bookmarks. English. First Edition, 2026. Published by thenexbooks. Includes the Paddlebook starter project (Go module) that the book builds on. This book is independent and is not affiliated with or endorsed by Google, the Go project, or the maintainers of any library it discusses.
Table of Contents
Part I — Foundations of Go Testing
- Ch 1 · Why Test Go Code? A Strategy, the Test Pyramid, and the Paddlebook Service
- Ch 2 · The testing Package and go test: First Tests, Failures, and Flags
- Ch 3 · Table-Driven Tests and Subtests
- Ch 4 · Comparing Results and Reporting Failures: go-cmp, Errors, Helpers, and testify
- Ch 5 · Organizing Tests: Packages, testdata, TestMain, Cleanup, and the Test Lifecycle
Part II — Testable Design and Test Doubles
- Ch 6 · Designing Testable Go: Small Interfaces, Dependency Injection, and Seams
- Ch 7 · Hand-Written Test Doubles: Stubs, Fakes, Spies, and a Test Environment
- Ch 8 · Generated Mocks with go.uber.org/mock — and When Not to Use Them
- Ch 9 · Testing Time, Randomness, and the Environment
- Ch 10 · Testing Error Paths, Edge Cases, and Panics
Part III — Testing HTTP Services
- Ch 11 · Testing HTTP Handlers with httptest
- Ch 12 · Testing JSON APIs, Routing, and Middleware
- Ch 13 · Testing HTTP Clients: Fake Servers, Timeouts, and Retries
- Ch 14 · Testing the Whole Server In-Process: Wiring, Startup, and Shutdown
- Ch 15 · End-to-End Tests: Building the Binary and Driving It from Go
Part IV — Databases and Integration Tests
- Ch 16 · Testing Database Code with database/sql and SQLite
- Ch 17 · Repository Contract Tests: One Suite for Every Implementation
- Ch 18 · Test Data: Fixtures, Builders, Seeds, and Isolation
- Ch 19 · Integration Test Strategy: Build Tags, Short Mode, Testcontainers, and Real PostgreSQL
Part V — Concurrency, Golden Files, Fuzzing, and Benchmarks
- Ch 20 · Testing Concurrent Code: The Race Detector, Goroutine Leaks, and Cancellation
- Ch 21 · Deterministic Time and Concurrency with testing/synctest
- Ch 22 · Golden Files, Snapshots, and Example Tests
- Ch 23 · Fuzzing with go test -fuzz
- Ch 24 · Benchmarks as Tests: b.Loop, Allocations, and Catching Regressions
Part VI — Suite Quality, Coverage, and CI
- Ch 25 · Code Coverage: Unit, Integration, and What the Numbers Mean
- Ch 26 · Flaky and Slow Tests: Diagnosis, Parallelism, and Cures
- Ch 27 · Continuous Integration for Go Tests: GitHub Actions, Reports, and Quality Gates
- Ch 28 · Testing Legacy Go Code and Paddlebook's Complete Testing Strategy
Appendices
A · Go Testing Cheat Sheet
B · Test Templates and Reference
C · Test-Suite Production Checklist
D · Glossary
New to Go? Go Programming from Zero teaches the language from the ground up, and Go (Golang) for Backend Developers covers production services with gRPC, PostgreSQL, Redis, and observability.
Book details
| Detail | |
|---|---|
| Pages | 522 |
| Format | PDF (A4) + starter project (Go) |
| Language | English |
| Level | Intermediate |
| Edition | 1st Edition (2026) |
| Download on this site | Free sample, first 20 pages (PDF) |
| Publisher | thenexbooks |
Files you get
No reviews yet. Only buyers can review.
More from thenexbooks