How to detect at run time that a program is being tested

Perhaps Go sets environment variables during testing and can they be obtained? I have work with a network which is better to omit during the test. I don’t really want to do this through global flags

I’m not sure how reliable this is or if it fits your use case, but when doing something like this in the past I remember looking for a -test.v in the arguments. Example:

for _, arg := range os.Args {
	if strings.Contains(arg, "test.v") {
		fmt.Println("In a test")
	}
}
1 Like

It works! Thanks!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.