When you want to set an environment variable in a Go test, you can use the os.Setenv function. If you have multiple tests, you want to ensure the environment variable is unset after the test. This can be done with a defer call to the os.Unsetenv function:

package main_test
 
import (
"testing"
 
"github.com/stretchr/testify/assert"
)
 
func Test_UsingEnvvar(t *testing.T) {
 
os.Setenv("ENV_VAR", "value")
defer os.Unsetenv("ENV_VAR")
 
actual := os.Getenv("ENV_VAR")
assert.Equals(t, "value", actual)
 
}