How to Run Go Tests in Parallel in CI
Go parallelism has two knobs: t.Parallel() runs tests within a package concurrently, and go test -p controls how many packages run at once.
Mark independent tests with t.Parallel(), then tune package-level concurrency with go test -p and per-package parallelism with -parallel.
Steps
- Call
t.Parallel()at the top of tests that are independent. - Set
-pto bound how many packages run concurrently. - Set
-parallelto bound parallel tests within a package.
Test code
thing_test.go
func TestThing(t *testing.T) {
t.Parallel()
// ... independent assertions
}Terminal
Terminal
go test -p 4 -parallel 8 ./...Gotchas
t.Parallel()tests share package-level state; guard globals or they will race.- Run with
-racein CI to catch data races that parallelism exposes.
Related guides
How to Run Rust Tests in Parallel With nextestRun Rust tests faster in CI with cargo-nextest, which executes each test in its own process and supports part…
How to Isolate a Database Per Parallel Test WorkerGive each parallel test worker its own database or schema so concurrent tests do not clobber shared rows, usi…