为Go测试设置环境变量

时间:2015-11-02 06:35:39

标签: go automated-tests environment-variables

我正在尝试像这样运行Go测试,设置一个环境变量:

FOO=BAR go list ./... | grep -v vendor | xargs -n1 go test -timeout=3s

在我的测试中,我做了:

log.Print(os.Getenv("FOO"))

返回一个空字符串。为什么不设置FOO?

3 个答案:

答案 0 :(得分:7)

FOO=BAR bash -c 'go list ./... | grep -v vendor | xargs -n1 go test -timeout=3s'

可以在问题的答案中找到原始命令无效的原因:https://unix.stackexchange.com/questions/134735/environment-variables-are-not-set-when-my-function-is-called-in-a-pipeline

答案 1 :(得分:2)

这不是管道设置变量的工作方式。参见:

 FOO=BAR echo "AAA" | FOO=WAZ printenv | grep FOO

答案 2 :(得分:0)

我喜欢在godotenv中使用command mode

godotenv -f ./.env go test ./internal/... -cover
相关问题