简体   繁体   中英

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

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

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

}

There are two aspects of "parallel tests" in go.

  1. Testing of packages (folders) in parallel. Controlled by -p flag.

go help build :

    -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.
  1. Testing of tests within a package. Controlled by -parallel flag, and tests need this enabled with t.Parallel() .

go help testflag :

    -parallel n
        Allow parallel execution of test functions that call t.Parallel.
        The value of this flag is the maximum number of tests to run
        simultaneously; by default, it is set to the value of GOMAXPROCS.
        Note that -parallel only applies within a single test binary.
        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').

For #1, you can observe this directly by having tests in multiple packages, and forcing them to be long running with a sleep. Then ps aux and grep for go running and you'll see separate processes in parallel.

Or run with -x detail flag. That pretty clearly shows what's going on. The tests are launched once per package (eg given have *_test.go files in several package. Note I'm running go test -x./... -v -count=1 |& ruby -pe 'print Time.now.strftime("[%Y-%m-%d %H:%M:%S] ")' |& tee temp.out with various configurations of -p=N -parallel=M, you can see in parallel them running:

[2022-05-09 17:22:26] $WORK/b065/understandinit.test -test.paniconexit0 -test.timeout=10m0s -test.parallel=1 -test.v=true -test.count=1
[2022-05-09 17:22:26] $WORK/b001/main.test -test.paniconexit0 -test.timeout=10m0s -test.parallel=1 -test.v=true -test.count=1
[2022-05-09 17:22:26] $WORK/b062/mainsub.test -test.paniconexit0 -test.timeout=10m0s -test.parallel=1 -test.v=true -test.count=1

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM