When you want to set an environment variable in a Go test, you can use the t.Setenv
function.
package main_test
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_UsingEnvVar(t *testing.T) {
t.Setenv("ENV_VAR", "value")
actual := os.Getenv("ENV_VAR")
assert.Equals(t, "value", actual)
}
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.