Skip to content

Instantly share code, notes, and snippets.

@hauxe
Created October 24, 2022 08:44
Show Gist options
  • Save hauxe/696aa156a2ef025d803709faf1241d6e to your computer and use it in GitHub Desktop.
Save hauxe/696aa156a2ef025d803709faf1241d6e to your computer and use it in GitHub Desktop.
Generic get environment unit test
package utils
import (
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestGetEnvT(t *testing.T) {
t.Run("string", func(t *testing.T) {
env := "UNITTEST_GETENVT_STRING"
os.Setenv(env, "string")
defer os.Unsetenv(env)
s := GetEnvT(env, EnvString)
require.Equal(t, "string", s)
})
t.Run("bool", func(t *testing.T) {
env := "UNITTEST_GETENVT_BOOL"
os.Setenv(env, "true")
defer os.Unsetenv(env)
b := GetEnvT(env, EnvBool)
require.True(t, b)
})
t.Run("int", func(t *testing.T) {
env := "UNITTEST_GETENVT_INT"
os.Setenv(env, "123")
defer os.Unsetenv(env)
i := GetEnvT(env, EnvInt)
require.Equal(t, 123, i)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment