Running Go tests in Parallel

Featured image for sharing metadata for article

If you're writing software in Go, you're likely to not be hitting any particularly slow tests, as the language and tooling is very efficient.

But as written about in Default Your Tests to run in Parallel, I recommend always running in parallel, so you can surface implementation issues, as well as further speed up an already quick codebase.

As highlighted on StackOverflow, and in the docs for go help testflag , running parallel tests via go test require the -parallel flag:

-parallel n Allow parallel execution of test functions that call t.Parallel, and fuzz targets that call t.Parallel when running the seed corpus. The value of this flag is the maximum number of tests to run simultaneously. While fuzzing, the value of this flag is the maximum number of subprocesses that may call the fuzz function simultaneously, regardless of whether T.Parallel is called. By default, -parallel is set to the value of GOMAXPROCS. Setting -parallel to values higher than GOMAXPROCS may cause degraded performance due to CPU contention, especially when fuzzing. Note that -parallel only applies within a single test binary. The 'go test' command may run tests for different packages in parallel as well, according to the setting of the -p flag (see 'go help build').

But it also needs us to explicitly opt in tests by making the following change:

 func TestTimeConsuming(t *testing.T) {
+    t.Parallel()
     // the test
 }

This is a bit of a shame, as it'd be great to be able to parallelise by default, but I get that it's useful to allow tests to opt-in.

You may also want to read Brandur's post On using Go's t.Parallel() as there are some interesting points on the practicality of it.

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #go #testing.

Also on:

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.