当go test被运行它运行在您的文件,在结束_test.go通过运行启动格式的功能TestXxx和使用(*T testing.T)模块.我想知道_test.go文件中的每个函数是否同时运行,或者它是否分别运行每个函数?它是否为每一个创建了一个例程?如果它确实为每个人创建了一个例程,我可以用某种方式监控go例程吗?是否有可能golibrary.GoRoutines()为每个人做一些事情并获得一个实例,并监控他们一些类似的东西?
注意:这个问题假设您使用go(测试)附带的测试框架.
Dmi*_*dov 20
是的,正如其他答案已经指出的那样,只有当您添加t.Parallel()并使用-parallel标记运行时,一个 _test.go 文件中的测试才会并行运行。
但是 - 默认情况下,该命令go test在针对不同包的并行测试中运行,默认情况下是可用的 CPU 数量(请参阅 参考资料go help build)
所以要禁用并行执行,你应该像这样运行测试
go test -p 1 ./...
nem*_*emo 11
是的,测试作为goroutine执行,因此同时执行.
但是,如@jacobsa所指出的,默认情况下测试不会并行运行.要启用并行执行,您必须调用测试用例并进行适当设置或提供.t.Parallel()GOMAXPROCS-parallel N
在并行运行测试时,最简单的解决方案是获得端口号的全局切片和全局原子递增的索引,该索引用作切片中测试和端口之间的关联.这样您就可以控制端口号并为每个测试设置一个端口.例:
import "sync/atomic"
var ports [...]uint64 = {10, 5, 55}
var portIndex uint32
func nextPort() uint32 {
return atomic.AddUint32(&portIndex, 1)
}
Run Code Online (Sandbox Code Playgroud)
JAW*_*eak 10
Go中的“并行测试”有两个方面。
\n-p。go help build:
-p n\n the number of programs, such as build commands or\n test binaries, that can be run in parallel.\n The default is GOMAXPROCS, normally the number of CPUs available.\nRun Code Online (Sandbox Code Playgroud)\n-parallel,测试需要使用 启用此功能t.Parallel()。go help testflag:
-parallel n\n Allow parallel execution of test functions that call t.Parallel.\n The value of this flag is the maximum number of tests to run\n simultaneously; by default, it is set to the value of GOMAXPROCS.\n Note that -parallel only applies within a single test binary.\n The \'go test\' command may run tests for different packages\n in parallel as well, according to the setting of the -p flag\n (see \'go help build\').\nRun Code Online (Sandbox Code Playgroud)\n对于#1,您可以通过在多个包中进行测试并强制它们在睡眠状态下长时间运行来直接观察这一点。然后 ps aux 和 grep for go running,您将看到并行的单独进程。
\n或者使用-x详细标志运行。这非常清楚地显示了\xe2\x80\x99s 正在发生什么。每个包启动一次测试(例如,在多个包中都有 *_test.go 文件。注意 I\xe2\x80\x99m 使用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\n[2022-05-09 17:22:26] $WORK/b001/main.test -test.paniconexit0 -test.timeout=10m0s -test.parallel=1 -test.v=true -test.count=1\n[2022-05-09 17:22:26] $WORK/b062/mainsub.test -test.paniconexit0 -test.timeout=10m0s -test.parallel=1 -test.v=true -test.count=1\nRun Code Online (Sandbox Code Playgroud)\n
小智 5
是的,如果您将以下代码添加到函数中:t.Parallel()。
像这样:
func TestMyFunction(t *testing.T) {
t.Parallel()
//your test code here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11714 次 |
| 最近记录: |