In Go, how do I get a test environment at runtime?

I want to check if the codes are working for go test

so that I can create some configurations.

Is there any function for this? How:

runtime.IsBeingTested ()

0


source to share


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







All Articles