一. 关于测试

  • 测试文件以_test.go结尾
  • 功能测试的方法名以Test开头
  • 基准测试的方法名以Benchmark开头
  • testing库包含T、B、M,T是功能测试,B是基准测试、M是主函数
  • 基准测试主要包含:覆盖率、CPU分析、内存分析、块分析、并发测试
  • 测试命令:go test [-c] [-i] [build flags] [packages] [flags for test binary]

go test 常用参数

-cpu: 设置测试最大 cpu 逻辑数(也就是 GPM 中 P, 最大并行执行的 gorouting 数量, 默认等于 cpu 核数)
-count: 设置执行测试函数的次数, 默认为 1
-run: 执行功能测试函数, 支持正则匹配, 可以选择测试函数或者测试文件来仅测试单个函数或者单个文件
-bench: 执行基准测试函数, 支持正在匹配
-benchtime: 基准测试最大探索时间上限
-parallel: 设置同一个被测试代码包中的功能测试函数的最大并发执行数
-v: 是展示测试过程信息

一. 功能测试

测试命令:

shell
1
2
3
$ go test demo_test.go demo.go           # 单元测试
$ go test --cover demo_test.go demo.go   # 单元测试覆盖率
$ go test -bench=. demo_test.go demo.go  # 性能测试

简单测试:

go
1
2
3
4
5
6
func TestAddSimple(t *testing.T) {
   expect, actual := 3, Add(1, 2)
   if expect != actual {
      t.Errorf("预期:%d,实际:%v", expect, actual)
   }
}

表格驱动: - 功能测试主要以表格驱动测试为主

go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
func TestMethodName(t *testing.T) {
   for _, unit := range []struct {
      param1 string //参数1
      param2 string //参数1
      expect string //期望结果
   }{
      {"hello", "world", "hello world"},
      {"hello", "golang", "hell_golang"},
   } {
      if actual := func(x, y string) string { return x + y }(unit.param1, unit.param2);
         actual != unit.expect {
         t.Errorf("期望:[%v],实际:[%v]", unit.expect, actual)
      }
   }
}

主函数


#二. 基准测试

  • 测试函数须以BenchmarkFoo(b *testing.B)
  • b.N即运行次数,会根据运行时间自动取值
  • 执行结果27.9 ns/op, 即平均每次27.9纳秒
  • 并发测试即对象或函数在多线程下是否安全

并发测试:

go
1
2
3
4
5
func BenchmarkAdd(b *testing.B) {
   for i := 0; i < b.N; i++ {
      Add(i+1, rand.Intn(i+1))
   }
}

并发测试:

go
1
2
3
4
5
6
7
8
9
func BenchmarkAddParallel(b *testing.B) {
   b.RunParallel(func(pb *testing.PB) {
      for pb.Next() {
         m := rand.Intn(100) + 1
         n := rand.Intn(m)
         Add(m, n)
      }
   })
}

覆盖率

  • 指测试用例能覆盖到到代码比率
go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
//业务代码: 成绩评级
func Rating(x int) string {
   switch {
   case x < 60:
      return "flunk"
   case x < 80:
      return "pass"
   default:
      return "excellent"
   }
}
go
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// 测试用例
func TestDiv(t *testing.T) {
   if r := Rating(50); r != "flunk" {
      t.Errorf("实际:%v", r)
   }
   if r := Rating(85); r != "excellent" {
      t.Errorf("实际:%v", r)
   }
    //没考虑到<80的情况
}
shell
1
2
3
4
[sybs@hello] $ go test --cover
PASS
coverage: 75.0% of statements
ok hello 0.005s

显示覆盖率只有75%

测试入口

text
1
2
3
4
5
6
7
// 每个测试方法都会执行
func TestMain(m *testing.M) {
   setup, teardown := "安装测试...", "卸载测试..."
   fmt.Println(setup)
   m.Run()
   fmt.Println(teardown)
}

三. 实战.gomock测试

https://godoc.org/github.com/golang/mock/gomock


四. 实战.httpexpect测试

https://github.com/gavv/httpexpect


五. 实战.go-sqlmock测试

https://github.com/DATA-DOG/go-sqlmock


goconvey

压力测试
https://www.jianshu.com/p/7f28aad6ecc5
ab

参考:
https://cloud.tencent.com/developer/article/1004535
http://www.hatlonely.com/2018/01/31/golang-%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95/
https://cloud.tencent.com/developer/article/1004535
http://www.imooc.com/article/269074?block_id=tuijian_wz
https://www.cnblogs.com/jssyjam/p/10728801.html


「真诚赞赏,手留余香」

Hollson码夫庄园

嗯,我的梦想是被读者的稿费包养~

使用微信扫描二维码完成支付

Hollson

评论