#development #golang #pattern #testing

When you want to set an environment variable in a Go test, you can use the t.Setenv function.

 1package main_test
 2
 3import (
 4    "testing"
 5
 6    "github.com/stretchr/testify/assert"
 7)
 8
 9func Test_UsingEnvVar(t *testing.T) {
10    t.Setenv("ENV_VAR", "value")
11
12    actual := os.Getenv("ENV_VAR")
13    assert.Equals(t, "value", actual)
14}