In Go,how to get test environment at run time?

烈酒焚心 提交于 2019-12-20 06:32:08

问题


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


回答1:


Just specify that you run a test in test's init. For example, in pkg.go:

package pkg

var isTesting = false

// ...

And in pkg_test.go:

package pkg

func init() {
    isTesting = true
}

// ...

This technique can be used not just with bools 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.



来源:https://stackoverflow.com/questions/28476307/in-go-how-to-get-test-environment-at-run-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!