0004: Testing Depends on Interfaces
Date: 2026-06-14 Status: Accepted
Context
Learner has completed interfaces (0001), error handling (0002), and struct tags (0003). The next essential skill for cloud-native contribution is writing tests — table-driven tests, subtests, and mocking via interfaces.
Insight
Testing in Go is fundamentally tied to interfaces. Mocking in Go doesn't use mocking frameworks — it uses interface satisfaction. Define an interface, create a test double that implements it, pass it in. This is the same pattern from Lesson 0001 applied to test doubles.
The table-driven test pattern is the most idiomatic Go testing convention. Every cloud-native project uses it. Kubernetes controllers are tested with fake clients that implement the Client interface. Understanding this pattern is the difference between tests that pass review and tests that get rejected.
The testing.T type should be taught as a minimal API — t.Logf, t.Errorf, t.Fatalf, t.Run, t.Helper, t.Parallel. No complex testing frameworks needed.
Consequences
- Lesson 0004: Table-driven tests, subtests, mocks via interfaces, test helpers, benchmarking
- Should include a real Kubernetes-style controller test pattern
- The
httptestpackage deserves mention as a standard library testing gem - Cheat sheet should cover
testingpackage API, table-driven test template, and common patterns