Does go test run unit tests concurrently?

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 ./...


Yes if you add this : t.Parallel() to your functions.

like this:

func TestMyFunction(t *testing.T) {
    t.Parallel()
    //your test code here

}

You can run them concurrently by flagging the test with t.Parallel and then run the test using the -parallel flag.

You can see other testing flags here


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)
}