Menu

1 / 2

Sub-tests Done Right: t.Run, t.Parallel, and the Cleanup-Order Trap

DEV Community·Gabriel Anhaia·6 months ago
#zv6eOd0f
#rule#go#testing#concurrency#parallel
Reading 0:00
15s threshold

Book: The Complete Guide to Go Programming

Also by me: Thinking in Go (2-book series) — Complete Guide to Go Programming + Hexagonal Architecture in Go

My project: Hermes IDE | GitHub — an IDE for developers who ship with Claude Code and other AI coding tools

Me: xgabriel.com | GitHub

A backend team I talked to last quarter had a flaky integration

suite that passed locally and failed once a week in CI. The

failure was always the same: a Postgres test row that should not

exist still existed when a later test queried for it. They added

sleeps. They added retries. They added a "skip flaky" tag. The

test stayed flaky for four months.

The bug was four lines of test setup. A parent test opened a

shared transaction, registered t.Cleanup to roll it back, then

spawned three sub-tests with t.Parallel() . Sometimes the

rollback ran while a parallel child was still mid-INSERT. Race

detector said nothing. go test -v said the parent passed.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.