Load .env.test file when running go test

Hello i want to load .env.test file when running go test command but maintain my current .env when running go.
run my current code is like this

err := godotenv.Load(filepath.Join("…/…/", “.env”))
if err != nil {
fmt.Println("Error: ", err)

I really can´t find teh solution, can you provide and example with what i say?

It result that if i run go run main.go it loads the .env but when i run go test it will take whatever i pass in this line err := godotenv.Load(filepath.Join("…/…/", “.env.test”))
so now it works

The link points to documentation for a TestMain function you can provide to do special initialization for tests. That’s probably where you want to load your env vars


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