How to Know If a Golang App is Executed with go test

Furkan Danışmaz
Picus Security Engineering
2 min readMay 18, 2021

If you came across this story, it is obvious that you want to be able to know (programmatically) if your code is being executed with go or go test and may be you are wondering if your code should know the difference.

To start with, of course it is better if you can avoid putting if statements inside your code that checks if the code is being executed with go test. However, you may come across with some cases that you have no better option. In my case, we are initializing some background jobs that creates resources and open session to cloud resources and I don’t want them to be initialized during test execution.

What’s Wrong With Using An Environment Variable?

The first thing that comes to mind is to use some environment variable. That’s a very easy way to solve this problem:

  • Define an environment variable. EXECUTION_ENVIRONMENT for example
func IsTestRun() bool {
return os.Getenv("EXECUTION_ENVIRONMENT") == "test"
}
  • and set that environment variable before running your application in test mode:
export EXECUTION_ENVIRONMENT="test"
go test ./...

Down side of this approach is you always have to set the environment variable before running your tests.

Using the flag Package

While executing our code with go test, we are sending some extra flags as OS arguments (like the test itself after the go command) and we can access them in our programs, so why don’t we just make use of them.

if flag.Lookup("test.v") == nil {
// normal execution
} else {
// test execution
}

However, for this code to work first you should define all the possible flags and call flag.Parse() function.

package testfunc init() {
if flag.Lookup("test.v") == nil {
flag.Bool("test.v", false, "verbose output")
}
if flag.Lookup("test.v") == nil {
flag.String("test.run", "", "run tests matching expression")
}
// define all possible flags here flag.Parse()
}
func IsTestRun() bool {
return flag.Lookup("test.v").Value.(flag.Getter).Get().(bool)
}

OK, I think this is a better way than using an extra environment variable but still defining all the possible flags is annoying, isn’t it?

And there is another problem with this code. It panics :) with a message telling that you are registering the same flags more than once even though we put a nil check before registering them.

Using the testing Package

The testing package already has Init function that handles all the possible flag registrations for go test which is exactly what I was looking for. With the help of the testing.Init() my code that checks if the app is executed with go test became as follows:

package testfunc init() {
testing.Init()
flag.Parse()
}
func IsTestRun() bool {
return flag.Lookup("test.v").Value.(flag.Getter).Get().(bool)
}

Now, you can call this function and know that if your code is executed with go test

func init() {
// do common initializations
if test.IsTestRun() {
// some mock initializations for tests if needed
} else {
// actual initializations
}
}

--

--