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

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

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

这是我的代码:

type MyStruct struct {
    Name string `json:"name"`
}

func MyFunc() ([]MyStruct, error) {
    str := os.Getenv("MyStruct")  // 失败
    // str := `[{\"name\": \"one\"}, {\"name\": \"two\"}]` // 成功

    var myData []MyStruct
    err := json.Unmarshal([]byte(str), &myData)
    if err != nil {
        return nil, err
    }
    return myData, nil
}

我的测试代码:

func TestMyFunc(t *testing.T) {
    tests := []struct {
        name    string
        want    []MyStruct
        wantErr error
    }{
        {
            name: "success",
            want: []MyStruct{
                {
                    Name: "one",
                },
                {
                    Name: "two",
                },
            },
            wantErr: nil,
        },
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            got, err := MyFunc()
            assert.Equal(t, err, tt.wantErr)
            assert.Equal(t, got, tt.want)
        })
    }
}

我的环境:

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:

type MyStruct struct {
    Name string `json:&quot;name&quot;`
}

func MyFunc() ([]MyStruct, error) {
	str := os.Getenv(&quot;MyStruct&quot;)  // Fail 
    // str := `[{&quot;name&quot;: &quot;one&quot;}, {&quot;name&quot;: &quot;two&quot;}]` // Success

	var myData []MyStruct
	err := json.Unmarshal([]byte(str), &amp;myData)
	if err != nil {
		return nil, err
	}
	return myData, nil
}

My test code:

func TestMyFunc(t *testing.T) {
	tests := []struct {
		name    string
		want    []MyStruct
		wantErr error
	}{
		{
			name: &quot;success&quot;,
			want: []MyStruct{
				{
					Name: &quot;one&quot;,
				},
				{
					Name: &quot;two&quot;,
				},
			},
			wantErr: nil,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := MyFunc()
			assert.Equal(t, err, tt.wantErr)
			assert.Equal(t, got, tt.want)
		})
	}
}

My env:

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

Using

答案1

得分: 0

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

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

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

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

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:

确定