[英]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.当运行
go test
时,它会通过运行以TestXxx
格式开始并使用 (*t testing.T) 模块的函数来运行以_test.go
结尾的文件。 I was wondering if each function in the _test.go
file were ran concurrently or if it definitively ran each function separately?我想知道
_test.go
文件中的每个 function 是否同时运行,或者它是否明确地分别运行每个 function? Does it create a go routine for each one?它是否为每个程序创建一个 go 例程? If it does create a go routine for each one, can I monitor the go routines in some way?
如果它确实为每个创建一个 go 例程,我可以以某种方式监视 go 例程吗? 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?是否有可能做类似
golibrary.GoRoutines()
的事情并为每个实例获取一个实例并以某种方式或类似方式监视它们?
Note: this question assumes you using the testing framework that comes with go (testing).注意:此问题假设您使用 go(测试)附带的测试框架。
Yes , tests are executed as goroutines and, thus, executed concurrently .是的, 测试是作为 goroutines 执行的,因此是并发执行的。
However, tests do not run in parallel by default as pointed out by @jacobsa .但是,正如@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).要启用并行执行,您必须在测试用例中调用
t.Parallel()
并适当设置GOMAXPROCS
或提供-parallel N
(默认设置为GOMAXPROCS
)。
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.是的,正如其他答案已经指出的那样,只有在添加
t.Parallel()
并使用-parallel
标记运行时,一个 _test.go 文件中的测试才会并行运行。
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
)但是 - 默认情况下,
go test
命令在不同包的并行测试中运行,默认是可用的 CPU 数量(请参阅go help build
)
So to disable parallel execution you should run tests like this go test -p 1 ./...
因此,要禁用并行执行,您应该运行这样的测试
go test -p 1 ./...
Yes if you add this : t.Parallel() to your functions.是的,如果您将这个 : t.Parallel() 添加到您的函数中。
like this:像这样:
func TestMyFunction(t *testing.T) {
t.Parallel()
//your test code here
}
There are two aspects of "parallel tests" in go. go中的“并行测试”有两个方面。
-p
flag.-p
标志控制。 go help build
: 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.
-parallel
flag, and tests need this enabled with t.Parallel()
.-parallel
标志控制,并且测试需要使用t.Parallel()
启用此功能。 go help testflag
: 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.对于 #1,您可以通过在多个包中进行测试并强制它们在休眠状态下长时间运行来直接观察到这一点。 Then ps aux and grep for go running and you'll see separate processes in parallel.
然后 ps aux 和 grep 运行 go,您将看到并行的独立进程。
Or run with -x
detail flag.或者使用
-x
detail 标志运行。 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:测试每 package 启动一次(例如,在几个 package 中有 *_test.go 文件。注意我正在运行
go test -x./... -v -count=1 |& ruby -pe 'print Time.now.strftime("[%Y-%m-%d %H:%M:%S] ")' |& tee temp.out
各种配置的-p=N -parallel=M,你可以看到并行跑步:
[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.您可以通过使用t.Parallel标记测试来同时运行它们,然后使用
-parallel
标志运行测试。
You can see other testing flags here您可以在此处查看其他测试标志
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.