Go parallelize your tests

less than 1 minute read

As a note to the reader, all my code examples use ‘tip’ for the Go runtime.

go version devel +006bc57095 Sun Oct 22 15:50:50 2017 +0000 darwin/amd64

Go’s testing package has a method to parallelize the execution of your tests. Parallelizing your tests and running with the race detector can help detect unexpected shared mutable state.

Below is an example that demonstrates this possible interaction.

Below are two different commands that execute tests sequentially or in parallel: “-parallel=1 runs tests sequentially and -parallel=2 runs that in parallel”.

You should include t.Parallel() on the first line of all your unit tests by default. I am not suggesting running parallel tests as the best or only way to find shared mutable state. My suggestion is merely an additional safeguard. Parallel tests also execute faster by utilizing multiple processors. These benefits behoove me to recommend parallelizing all Go unit tests.

Updated: