单元测试
- 需要测试的文件 + _test.go
- 需要测试的函数, func Test + 函数名
go test -v // 测试所有
go test -v xxx_test.go // 测试文件中所有函数
go test -v -run Test hello_test.go //测试hello_tst.go文件中所有以Test开头(正则)的函数
go test -v -run Test$ hello_test.go //只测试Test函数
func Split(str string, sep string) (st []string){
// "a:b:c" 分割 ":"--> [a b c]
index := strings.Index(str, sep)
//var strLeft []string
var strLeft = make([]string, 0, strings.Count(str, sep) + 1)
for index >= 0 {
strLeft = append(strLeft, str[:index])
str = str[index+len(sep):]
index = strings.Index(str, sep)
}
st = append(strLeft, str[:])
return st
}
func TestSplit(t *testing.T) {
type testCase struct{
got string
sep string
want []string
}
testGroup := map[string]testCase{
"case1": {
got: "a:b:c",
sep: ":",
want: []string{"a", "b", "c"},
},
"case2": {
got: "a:b:c",
sep: ":b:",
want: []string{"a", "c"},
},
}
for _, v := range testGroup{
fact := Split(v.got, v.sep)
if !reflect.DeepEqual(v.want, fact){
t.Errorf("testsplit error, fact: %s, want: %s", fact, v.want)
}
}
}
运行:go test -v
测试组
func TestSplit(t *testing.T) {
type testCase struct{
got string
sep string
want []string
}
testGroup := map[string]testCase{
"case1": {
got: "a:b:c",
sep: ":",
want: []string{"a", "b", "c"},
},
"case2": {
got: "a:b:c",
sep: ":b:",
want: []string{"a", "c"},
},
}
for _, v := range testGroup{
fact := Split(v.got, v.sep)
if !reflect.DeepEqual(v.want, fact){
t.Errorf("testsplit error, fact: %s, want: %s", fact, v.want)
}
}
}
运行:go test -v
子测试:添加测试名称测试
func TestSplit2(t *testing.T) {
type testCase struct{
got string
sep string
want []string
}
testGroup := map[string]testCase{
"case1": {
got: "a:b:c",
sep: ":",
want: []string{"a", "b", "c"},
},
"case2": {
got: "a:b:c",
sep: ":b:",
want: []string{"a", "c"},
},
"case3": {
got: "阳光彩虹小白马",
sep: "白",
want: []string{"阳光彩虹小", "马"},
},
}
for k, v := range testGroup{
t.Run(k, func(t *testing.T) {
fact := Split(v.got, v.sep)
if !reflect.DeepEqual(v.want, fact){
t.Errorf("testsplit error, fact: %s, want: %s", fact, v.want)
}
})
}
}
可以单独运行: go test -run=case1
测试覆盖率
运行:go test -cover
输出到文件:go test -coverprofile=x.out
然后输出的文件转html格式打开: go tool cover -html=x.out
基准测试
func BenchmarkSplit(b *testing.B) {
for i := 0; i < b.N; i++ {
Split("沙河有沙又有河", "沙")
}
}
运行:go test -bench=Split
gocode$ go test -bench=Split -benchmem
goos: windows
goarch: amd64
pkg: StudyGO/day12/like_testgo
BenchmarkSplit-4 2708648 447 ns/op 112 B/op 3 allocs/op
PASS
ok StudyGO/day12/like_testgo 1.700s
斐波那契数列:
func Fib(n int) int {
if n < 2 {
return n
}
return Fib(n-1) + Fib(n-2)
}
func benchmarkFib(b *testing.B, n int) {
time.Sleep(1 * time.Second) // 假设需要做一些耗时的无关操作
b.ResetTimer() // 重置计时器
for i := 0; i < b.N; i++ {
Fib(n)
}
}
func BenchmarkFib1(b *testing.B) { benchmarkFib(b, 1) }
func BenchmarkFib2(b *testing.B) { benchmarkFib(b, 2) }
func BenchmarkFib3(b *testing.B) { benchmarkFib(b, 3) }
func BenchmarkFib10(b *testing.B) { benchmarkFib(b, 10) }
func BenchmarkFib20(b *testing.B) { benchmarkFib(b, 20) }
func BenchmarkFib40(b *testing.B) { benchmarkFib(b, 40) }
gocode$ go test -bench=.
goos: darwin
goarch: amd64
pkg: github.com/Q1mi/studygo/code_demo/test_demo/fib
BenchmarkFib1-8 1000000000 2.03 ns/op
BenchmarkFib2-8 300000000 5.39 ns/op
BenchmarkFib3-8 200000000 9.71 ns/op
BenchmarkFib10-8 5000000 325 ns/op
BenchmarkFib20-8 30000 42460 ns/op
BenchmarkFib40-8 2 638524980 ns/op
PASS
ok github.com/Q1mi/studygo/code_demo/test_demo/fib 12.944s