Skip to content

Unit Testing in Go — Patterns from real projects

Full documentation site: https://padiazg.github.io/unit-test-book

A hands-on guide to Go unit testing through real-world patterns extracted from 7 production Go projects.

Each chapter is a standalone Go module with production code, tests, and an explanation of the testing approach.

Table of Contents

Section 1: Foundations

# Chapter Pattern Source
01 Classic Table-Driven Tests wantErr bool pantry/domain, hexago/version
02 Value Assertions want T + deep equal go-testgen/utils, hexago/version
03 Fields Struct for Inputs Grouped input structs hexago/version
04 Subtest Naming Strategies Constants as test names hexago/version

Section 2: Closure-Check Pattern

# Chapter Pattern Source
05 Typed Check Functions type checkXxxFn func(...) pantry, jokes
06 Check Collection Builder var checkXxx = func(fns...) notifier, pantry
07 Check Factory Closures checkStatus(want) closures go-crap, pantry
08 Error Message Verification assert.Contains(err.Error()) notifier, pantry
09 Output String Inspection assert.Contains/NotContains go-testgen

Section 3: Mocking & Dependency Injection

# Chapter Pattern Source
10 The before Hook Pattern before func(*SUT) notifier, pantry, jokes
11 HTTP Client Interface Mock HTTPClient + mockHTTPClient notifier, ollama-tools
12 RoundTripper Mock DryRunTransport ollama-tools
13 testify/mock for Interfaces mock.Mock + .On().Return() pantry, notifier/amqp
14 Function Variable Injection jsonMarshal, httpNewRequest notifier
15 Package-Level Var Swap randRead override + defer restore notifier/utils

Section 4: HTTP & I/O Testing

# Chapter Pattern Source
16 httptest.Server Integration Real HTTP test server notes
17 httptest.ResponseRecorder Handler unit tests pantry/http
18 Error Readers errorReader for IO errors notes
19 Temporary Files & Parsing t.TempDir() + file ops go-testgen, go-crap

Section 5: Concurrency

# Chapter Pattern Source
20 Channel Delivery Tests Buffered channel + select notifier
21 Panic Recovery in Tests defer recover() + wantPanic notifier
22 Goroutine Run Loops go n.Run() + WaitGroup notifier

Section 6: Advanced Go Features

# Chapter Pattern Source
23 Goroutine Leak Detection goleak.VerifyTestMain go-crap
24 AST Parsing for Cyclomatic Complexity go/parser + go/ast go-crap/complexity
25 Benchmark Tests b.Benchmark + b.Loop() go-testgen, go-crap
26 Parallel Tests t.Parallel() pantry/database

Section 7: Integration Patterns

# Chapter Pattern Source
27 Service Layer with Mocked Ports setupService + Teardown pantry/services
28 JSON Format Verification Marshal/unmarshal roundtrip go-crap/report
29 Setup/Teardown Fixtures Fixture struct + teardown pantry/services

How to Use

# Run tests for a specific chapter
cd chapter-01-classic-table-driven
go test -v ./...

# Run all tests
go test ./chapter-*/...

Repository structure

Path Description
chapter-XX-*/ 29 standalone Go modules with code + tests + README
doc/ MkDocs site (generates the documentation site)
doc/docs/ Generated markdown pages (37 files)
doc/generate.py Script that builds mkdocs pages from chapter READMEs
doc/mkdocs.yml MkDocs site configuration

Build docs locally

cd doc
pip install mkdocs-material pymdown-extensions
mkdocs serve

Real-World Sources

The patterns in this book are extracted from these production Go projects:

Project Description
hexago Go project scaffolding with hexagonal architecture
jokes Joke API client
go-testgen Go test generator
go-crap CRAP score analysis tool
ollama-tools Ollama model tools
pantry Inventory management with hexagonal architecture
notifier Multi-channel notification system