如何在运行时获取测试环境 [英] How to get test environment at run time
本文介绍了如何在运行时获取测试环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检查 go test
是否正在运行代码,以便进行一些配置.
I want to check if codes are running for the go test
,so that I could make some configurations.
有什么功能可以做到这一点吗?喜欢:
Is there any function to do that? Like:
runtime.IsBeingTested()
runtime.IsBeingTested()
推荐答案
只需指定您在测试的 init
中运行测试即可.例如,在pkg.go中:
Just specify that you run a test in test's init
. For example, in pkg.go:
package pkg
var isTesting = false
// ...
在pkg_test.go中:
And in pkg_test.go:
package pkg
func init() {
isTesting = true
}
// ...
该技术不仅可以用于 bool
,而且可以用于任何数据或函数.如果您的软件包中有一些变量(在您的情况下为配置变量),则可以在 init
中覆盖它.
This technique can be used not just with bool
s but with any data or function. If you have some variable in your package (in your case, a configuration variable), you can just override it in init
.
这篇关于如何在运行时获取测试环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文