Are tests executed in parallel in Go or one by one?

Viewed 9467

I have a Go file with unit tests and some of them use a common variable. Another global variable is used in the code that I'm testing. All of this can cause a problem.

In Go when we execute tests that are located in the same file how does they run? In parallel or next one will not start before previous one is finished ?

2 Answers

By default, all the tests within a package are run sequentially.

Also by default, all of the sets of tests are run in parallel.

This may cause issues if you use defaults and have different packages that test against a common persistence layer (like a rdbms). If you have issues with this case, then running go test ./... -p 1 will prevent different test suites from running concurrently.

Related