是否可以同时进行测试运行单元测试? [英] Does go test run unit tests concurrently?

查看:52
本文介绍了是否可以同时进行测试运行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行 go test 后,它会运行以 TestXxx 格式开头的函数来运行以 _test.go 结尾的文件并使用(* t testing.T)模块.我想知道 _test.go 文件中的每个功能是否同时运行,还是确定地分别运行每个功能?它会为每个人创建一个执行例程吗?如果确实为每个人创建了go例程,是否可以某种方式监视go例程?

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?

注意:此问题假定您使用go(测试)随附的测试框架.

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

推荐答案

但是,

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.

在并行运行测试时,最简单的解决方案是为端口号创建一个全局切片,并使用一个原子级递增的全局索引作为该切片的测试和端口之间的关联.这样,您可以控制端口号,并且每个测试都有一个端口.示例:

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

这篇关于是否可以同时进行测试运行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆