In Go, how do I get a test environment at runtime?
1 answer
Just indicate that you are running the test in the test init
. For example, in pkg.go:
package pkg
var isTesting = false
// ...
And in pkg_test.go:
package pkg
func init() {
isTesting = true
}
// ...
This method can be used not only with bool
, but also with any data or function. If your package has a variable (in your case a config variable), you can simply override it in init
.
+2
source to share