Repeatedly run flaky Go tests with stress
It runs your test hundreds of times, which can be useful for finding flaky failures.
I was trying to debug a flaky Go test, and Paul pointed me at a Go utility called stress, which is specifically designed to catch sporadic failures. It was able to reproduce my flaky test failures, and showed that my fix was incomplete.
Installing stress
I installed stress
with the following command:
$ go install golang.org/x/tools/cmd/stress@latest
This created a binary stress
in ~/go/bin
, which I had to add to my PATH.
Using stress to run a flaky test repeatedly
Here’s a simple Go program which includes two tests: one which always passes, and one which fails on ~0.03% of runs:
package flaky
import (
"math/rand/v2"
"testing"
)
func TestReliable(t *testing.T) {}
func TestFlaky(t *testing.T) {
r := rand.IntN(10000)
if r > 9996 {
t.Errorf("r = %d, which is too big", r)
}
}
If you run this with go test flaky_test.go
, it will almost always pass.
We can compile this test into a binary, and then run that binary using stress
. It reports any failures, and every so often prints a progress report on what it’s found so far:
$ go test -o flaky.test flaky_test.go
$ stress ./flaky.test
ok command-line-arguments 0.003s
./T/go-stress-20250819T191229-3552286396
--- FAIL: TestFlaky (0.00s)
flaky_test.go:14: r = 9999, which is too big
FAIL
ERROR: exit status 1
./T/go-stress-20250819T191229-2915137162
--- FAIL: TestFlaky (0.00s)
flaky_test.go:14: r = 9998, which is too big
FAIL
ERROR: exit status 1
5s: 8809 runs so far, 2 failures (0.02%), 16 active
If your test suite contains many tests and you only want to stress one of them, you can add the -test.run
flag to select a subset of tests. Here’s an example where we skip the known-reliable test:
$ stress ./flaky.test -test.run=TestFlaky