Last Updated: November 08, 2018
·
5.552K
· georgemac

go test package in parallel

By default when using go test you will find that all the tests in a single package will be run in serial.

Which is great! If you want to run them in parallel you can by specifying the desired flag.

The thing to look out for is when you're running the entire test suite of your Go project, with multiple packages.

for example, in the following project structure:
main.go database/ database.go database_test.go process/ process.go process_test.go

Running the following commands from the project root, go test ./... will actually run the tests in database/ folder and process/ folder, in parallel, if you have more than one core on the host machine. Infact it will default to the max number of cores available on host. Note, not the individual tests functions, but the whole packages.

This isn't entirely clear, however, if you look at the go build command itself at line 123: http://golang.org/src/cmd/go/build.go#L123
.You will see that the -p flag defaults to the max number of cores. This results is package builds/tests will be run in parallel.

I have been caught out by this. Particularly when running integration tests with the same database between two packages. What happens is that when one suite starts by populating the database with some fixtures, it may be the case that the tear down of another test suite hasn't finished. If you clean up in a tear down by truncating your tables, you can find that the truncate happens after the insert and none of the data is there when you expect it to be.

Keep this in mind! If you want to force packages to be built/tested in serial; use go test -p 1 ./... and you should be fine!

1 Response
Add your response

Was looking for the test execution flow for a project with multiple packages. This article was really helpful!

over 1 year ago ·