setting a variable from test file in golang

Pierre Prinetti's Answer doesn't work in 2019.

Instead, do this. It's less then ideal, but gets the job done

//In the module that you are testing (not your test module:
func init() {
    if len(os.Args) > 1 && os.Args[1][:5] == "-test" {
        log.Println("testing")//special test setup goes goes here
        return // ...or just skip the setup entirely
    }
    //...
}

Try this:

Define your variable as global in main.go:

var testingMode bool

And then set it to be true in your test file main_test.go:

func init() {
    testingMode = true
}

Tags:

Go