Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does go test run unit tests concurrently?

When go test is ran it runs your files ending in _test.go by running the functions that start in the format TestXxx and use the (*t testing.T) module. I was wondering if each function in the _test.go file were ran concurrently or if it definitively ran each function separately? Does it create a go routine for each one? If it does create a go routine for each one, can I monitor the go routines in some way? Is it ever possible to do something like golibrary.GoRoutines() and get an instance for each one and monitor them some how or something like that?


Note: this question assumes you using the testing framework that comes with go (testing).

like image 898
Charlie Parker Avatar asked Jun 23 '14 23:06

Charlie Parker


People also ask

Does go run unit tests in parallel?

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').

How do you do unit testing in go?

Go's built-in support for unit testing makes it easier to test as you go. Specifically, using naming conventions, Go's testing package, and the go test command, you can quickly write and execute tests. In the greetings directory, create a file called greetings_test.go.

How long should it take to run unit tests?

Still, it seems as though a 10 second short-term attention span is more or less hard-wired into the human brain. Thus, a unit test suite used for TDD should run in less than 10 seconds. If it's slower, you'll be less productive because you'll constantly lose focus.

How do go tests work?

In the local directory mode, go test compiles the package sources and tests found in the current directory and then runs the resulting test binary. This mode disables caching. After the package test finishes, go test prints a summary line showing the test status ('ok' or 'FAIL'), the package name, and elapsed time.


Video Answer


2 Answers

Yes, tests are executed as goroutines and, thus, executed concurrently.

However, tests do not run in parallel by default as pointed out by @jacobsa. To enable parallel execution you would have to call t.Parallel() in your test case and set GOMAXPROCS appropriately or supply -parallel N (which is set to GOMAXPROCS by default).

The simplest solution for your case when running tests in parallel would be to have a global slice for port numbers and a global atomically incremented index that serves as association between test and port from the slice. That way you control the port numbers and have one port for each test. Example:

import "sync/atomic"  var ports [...]uint64 = {10, 5, 55} var portIndex uint32  func nextPort() uint32 {     return atomic.AddUint32(&portIndex, 1) } 
like image 64
nemo Avatar answered Sep 20 '22 14:09

nemo


Yes, as other answers already pointed, tests inside one _test.go file are running in parallel only if you add t.Parallel() and run with -parallel tag.

BUT - by default the command go test runs in parallel tests for different packages, and default is the number of CPUs available (see go help build)

So to disable parallel execution you should run tests like this go test -p 1 ./...

like image 21
Dmitry Davydov Avatar answered Sep 23 '22 14:09

Dmitry Davydov