使用os.Getenv切换测试覆盖率。

huangapple go评论123阅读模式
英文:

Toggle Test Coverage with os.Getenv

问题

我正在进行一个简单的测试,并在Visual Studio Code中使用Go: Toggle Test Coverage In Current Package进行测试,就像这样<br>
使用os.Getenv切换测试覆盖率。

在我的代码中,我使用os.Getenv从我的环境中读取一个变量。<br>
使用os.Getenv时,它返回FAIL,但是硬编码时返回SUCCESS<br>
<br>
如何在使用Go: Toggle Test Coverage In Current Package时,即使从env中获取数据,也能获得SUCCESS

这是我的代码:

  1. type MyStruct struct {
  2. Name string `json:"name"`
  3. }
  4. func MyFunc() ([]MyStruct, error) {
  5. str := os.Getenv("MyStruct") // 失败
  6. // str := `[{\"name\": \"one\"}, {\"name\": \"two\"}]` // 成功
  7. var myData []MyStruct
  8. err := json.Unmarshal([]byte(str), &myData)
  9. if err != nil {
  10. return nil, err
  11. }
  12. return myData, nil
  13. }

我的测试代码:

  1. func TestMyFunc(t *testing.T) {
  2. tests := []struct {
  3. name string
  4. want []MyStruct
  5. wantErr error
  6. }{
  7. {
  8. name: "success",
  9. want: []MyStruct{
  10. {
  11. Name: "one",
  12. },
  13. {
  14. Name: "two",
  15. },
  16. },
  17. wantErr: nil,
  18. },
  19. }
  20. for _, tt := range tests {
  21. t.Run(tt.name, func(t *testing.T) {
  22. got, err := MyFunc()
  23. assert.Equal(t, err, tt.wantErr)
  24. assert.Equal(t, got, tt.want)
  25. })
  26. }
  27. }

我的环境:

  1. export MyStruct='[{\"name\": \"one\"}, {\"name\": \"two\"}]'

使用

英文:

I'm making a simple test, and test using Go: Toggle Test Coverage In Current Package in Visual Studio Code like this<br>
使用os.Getenv切换测试覆盖率。

In my code is using os.Getenv to read a variable from my own env.<br>
It get FAIL when using os.Getenv, but SUCCESS when hardcoded<br>
<br>
How to get SUCCESS even get data from env using Go: Toggle Test Coverage In Current Package?

Here is my code:

  1. type MyStruct struct {
  2. Name string `json:&quot;name&quot;`
  3. }
  4. func MyFunc() ([]MyStruct, error) {
  5. str := os.Getenv(&quot;MyStruct&quot;) // Fail
  6. // str := `[{&quot;name&quot;: &quot;one&quot;}, {&quot;name&quot;: &quot;two&quot;}]` // Success
  7. var myData []MyStruct
  8. err := json.Unmarshal([]byte(str), &amp;myData)
  9. if err != nil {
  10. return nil, err
  11. }
  12. return myData, nil
  13. }

My test code:

  1. func TestMyFunc(t *testing.T) {
  2. tests := []struct {
  3. name string
  4. want []MyStruct
  5. wantErr error
  6. }{
  7. {
  8. name: &quot;success&quot;,
  9. want: []MyStruct{
  10. {
  11. Name: &quot;one&quot;,
  12. },
  13. {
  14. Name: &quot;two&quot;,
  15. },
  16. },
  17. wantErr: nil,
  18. },
  19. }
  20. for _, tt := range tests {
  21. t.Run(tt.name, func(t *testing.T) {
  22. got, err := MyFunc()
  23. assert.Equal(t, err, tt.wantErr)
  24. assert.Equal(t, got, tt.want)
  25. })
  26. }
  27. }

My env:

  1. export MyStruct=&#39;[{&quot;name&quot;: &quot;one&quot;}, {&quot;name&quot;: &quot;two&quot;}]&#39;

Using

答案1

得分: 0

我找到了解决方案<br>
只需将以下代码添加到测试代码中:

  1. strEnv := `[{&quot;name&quot;: &quot;one&quot;}, {&quot;name&quot;: &quot;two&quot;}]`
  2. err := os.Setenv(&quot;MyStruct&quot;, strEnv)
  3. if err != nil {
  4. return
  5. }
英文:

I found the solution<br>
Just add this code to test code:

  1. strEnv := `[{&quot;name&quot;: &quot;one&quot;}, {&quot;name&quot;: &quot;two&quot;}]`
  2. err := os.Setenv(&quot;MyStruct&quot;, strEnv)
  3. if err != nil {
  4. return
  5. }

huangapple
  • 本文由 发表于 2021年6月30日 11:57:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/68188248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定