#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}
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.