Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know I'm running within "go test"

When I run "go test", I want to use a different configuration file. How do I know within my code if I'm running within a test context or a normal context? Is there some sort of environment variable to check?

like image 712
Rob Kinyon Avatar asked Jan 10 '13 01:01

Rob Kinyon


People also ask

How do you run a test in go?

At the command line in the greetings directory, run the go test command to execute the test. The go test command executes test functions (whose names begin with Test ) in test files (whose names end with _test.go). You can add the -v flag to get verbose output that lists all of the tests and their results.

Do go tests run concurrently?

Yes, tests are executed as goroutines and, thus, executed concurrently.

Which command is used to run the tests in go?

After the package test finishes, go test prints a summary line showing the test status ('ok' or 'FAIL'), the package name, and elapsed time. To run your tests in this mode, run go test in your project's root directory. In the package list mode, go test compiles and tests each package listed as arguments to the command.

Where do you put tests in go?

By convention, Go testing files are always located in the same folder, or package, where the code they are testing resides. These files are not built by the compiler when you run the go build command, so you needn't worry about them ending up in deployments.

How do I run a package test in go?

After the package test finishes, go test prints a summary line showing the test status (‘ok’ or ‘FAIL’), the package name, and elapsed time. To run your tests in this mode, run go test in your project’s root directory. In the package list mode, go test compiles and tests each package listed as arguments to the command.

What are the running modes of Go test?

go test has two running modes. Understanding them is essential to have an easy time working with the tool: In the local directory mode, go test compiles the package sources and tests found in the current directory and then runs the resulting test binary. This mode disables caching.

How do I get the output of a Go test?

Output of go test ./… If you are using VSCode, the Go extension has the ability to generate the same output as above. You can do so by searching for “go coverage” in the Command Palette. This command is an enhanced version of go test -cover . It renders a HTML page that visualises line-by-line coverage of each affected .go file.

What is a Go test?

These *_test.go files can contain test functions, benchmark functions, and example functions. Each listed package will cause the execution of a separate test binary. go test compiles test files that declare a package, ending with the suffix *_test.go as a separate package. It then links them with the main test binary and runs them.


2 Answers

The testing package modifies the global environment when loaded: it registers a lot of command-line flags. So, we can check if these flags are registered:

func init() {     if flag.Lookup("test.v") == nil {         fmt.Println("normal run")     } else {         fmt.Println("run under go test")     } } 
like image 52
Powerman Avatar answered Sep 28 '22 03:09

Powerman


Since the flag.Lookup("test.v") approach does not work for me, I'd like to share my own solution:

strings.HasSuffix(os.Args[0], ".test") 

Or

strings.Contains(os.Args[0], "/_test/") 

Both seem to work.

like image 44
saeedgnu Avatar answered Sep 28 '22 05:09

saeedgnu