38 words, 1 min read

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)
}