Golang测试func TestXX(t *testing.T)的使用

一般Golang中的测试代码都以xxx_test.go的样式,在命名测试函数的时候以Testxx开头。
以下是我写的一个单元:

package tests

import "strings"

func Split(s, sep string) (res []string) {
	i := strings.Index(s, sep)
	for i > -1 {
		res = append(res, s[:i])
		s = s[i+len(sep):]
		i = strings.Index(s, sep)
	}
	res = append(res, s)
	return
}

第一种测试方法:

func TestSplit(t *testing.T) {
	inputs := Split("a:b:c", ":")
	want := []string{"a", "b", "c"}
	if !reflect.DeepEqual(inputs, want) {
		t.Errorf("inputs:%v, want:%v", inputs, want)
	}
}

这种直接定义好输入、期望值,进行对比,这种不适合大量数据比较。

第二种测试方法:

func TestSplit(t *testing.T) {
	testCases := []struct {
		input string
		sep   string
		want  []string
	}{
		{input: "a:b:c", sep: ":", want: []string{"a", "b", "c"}},
		{input: "a:b:c", sep: ",", want: []string{"a:b:c"}},
		{input: "abcd", sep: "bc", want: []string{"a", "d"}},
	}

	for _, tc := range testCases {
		got := Split(tc.input, tc.sep)
		if !reflect.DeepEqual(got, tc.want) {
			t.Errorf("期望值:%v,实际值:%v\n", tc.want, got)
		}
	}
}

使用结构体测试,然后使用for range遍历,是比较方便的方式,但是如果我的测试数据很多,但是我其中一个测试出现错误了,我现在需要找到那一个,那么这个方式就有点不适用了。

第三种测试方法(推荐使用):

func TestSplit(t *testing.T) {
	testCases := map[string]struct {
		input string
		sep   string
		want  []string
	}{
		"one":   {input: "a:b:c", sep: ":", want: []string{"a", "b", "c"}},
		"two":   {input: "a:b:c", sep: ":", want: []string{"a", "b", "c"}},
		"three": {input: "a:b:c", sep: ":", want: []string{"a", "b", "c"}},
		"four":  {input: "a:b:c", sep: ":", want: []string{"a", "b", "c"}},
		"five":  {input: "a:b:c", sep: ":", want: []string{"b", "b", "c"}},
	}

	for name, tc := range testCases {
		t.Run(name, func(t *testing.T) {
			got := Split(tc.input, tc.sep)
			if !reflect.DeepEqual(got, tc.want) {
				t.Errorf("期望值:%v,实际值:%v", tc.want, got)
			}
		})
	}
}

在这里插入图片描述

这里我们使用子测试的方法,主要可以看到第五个测试案例直接报错,信息并显示出来。

同样,也有一些其他的测试方法,后续如果了解更多的话,在这里补上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

席万里

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

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

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

打赏作者

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

抵扣说明:

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

余额充值