
Adopt the Newest Go Testing Features in an Old Codebase
Adopt newer Go testing features in an old codebase: t.Context, t.Chdir, b.Loop, t.Attr, t.Output and testing/synctest, with the go.mod rules that gate each.
Tag
6 articles

Adopt newer Go testing features in an old codebase: t.Context, t.Chdir, b.Loop, t.Attr, t.Output and testing/synctest, with the go.mod rules that gate each.

Move testify suites to plain Go tests: TestMain for suite hooks, a helper plus t.Cleanup for SetupTest, subtests for methods. Both versions run, order shown.

Remove testify from a Go project in steps: map assert and require calls to if statements, keep go-cmp for diffs, convert mocks to fakes and drop the module.

When a mutex around a single value can become atomic.Int64, atomic.Bool or atomic.Pointer, with before and after code and a read-heavy benchmark.

Move a Gin API to net/http route by route, using a parity test that finds the real differences: 405 versus 404 and trailing slashes.

Update old Go concurrency idioms to current ones, with before and after code that gives the same result: WaitGroup.Go, typed atomics, OnceValue.