Coding Bits
June 4th, 2024

Disabling Parallel Test Runs In Go

Go

Go has had parallel tests enabled by default for a while now. But if you can't run your tests in parallel, what should you do?

Most people probably know of the -p 1 option to disable parallel execution of tests:

go test -p 1 . 

But this only works if you have access to the command itself. If you've got the go test command in a Makefile running in a CI/CD environment that's difficult to change, this may not be available to you (it wasn't available to me when I faced this problem yesterday).

A hint to an alternative approach is given in the -p documentation. It's actually a build switch, so running go help test won't reveal anything. But run go help build, and you'll see this help string:

-p n
        the number of programs, such as build commands or
        test binaries, that can be run in parallel.
        The default is GOMAXPROCS, normally the number of CPUs available.

Looks to be that the default value of -p is whatever GOMAXPROCS is, so it stands to reason that if you were to set it to 1, it should be effectively the same as using -p 1.

After trying this myself, I can confirm that this works: setting the environment variable GOMAXPROCS to 1 will disable parallel execution of tests.