golang单测

goland自动生成

  1. 鼠标移动到函数名处右击鼠标
  2. 点击:生成
  3. 点击:函数测试
func TestGetFieldIds(t *testing.T) {
    type args struct {
       fieldIdsStr string
    }
    tests := []struct {
       name       string
       args       args
       wantResult []uint
    }{
       // TODO: Add test cases.
    }
    for _, tt := range tests {
       t.Run(tt.name, func(t *testing.T) {
          if gotResult := GetFieldIds(tt.args.fieldIdsStr); !reflect.DeepEqual(gotResult, tt.wantResult) {
             t.Errorf("GetFieldIds() = %v, want %v", gotResult, tt.wantResult)
          }
       })
    }
}

由于goland自动生成的测试函数使用了反射,但是大多数情况下是不使用的,使用断言这种方式,所以就用了下面的Testify。

Testify

Testify是一个功能强大且易于使用的测试工具包,提供了丰富的断言方法和辅助函数。它扩展了Go的内置testing包,使测试编写更简洁、可读性更高。
导入库:go get github.com/stretchr/testify/assert

func TestGetFieldIds(t *testing.T) {
    testCases := []struct {
       input    string
       expected []uint
    }{
       {input: "1,2,3,4,5", expected: []uint{1, 2, 3, 4, 5}},
       {input: "10,20,30", expected: []uint{10, 20, 30}},
       {input: "100,200,300,400", expected: []uint{100, 200, 300, 400}},
       // Add more test cases as needed
    }
    for _, tc := range testCases {
       result := GetFieldIds(tc.input)
       assert.Equal(t, tc.expected, result, "GetFieldIds result should match expected")
    }
}

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

终生成长者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值