当前位置:   article > 正文

go test单元测试详解

go test

目录

介绍&测试范围

测试函数

执行机制

常用执行模式

子测试

帮助函数Helper()

测试覆盖率


 

介绍&测试范围

go test测试是go自带的测试工具,主要包括单元测试和性能测试两大类。

包括了工程目录下所有以_test.go为后缀名的源代码文件,这些文件在编译时不会被编译到最终的可执行文件中。

测试函数

命名
        一般是以Test开头再加自定义函数名

正常:TestAnd

错误:Testand

参数
        测试函数参数只有一个,普通的单元测试均为*testing.T,参数如果不对则会提示:Wrong test signature

        可允许的参数类型如:

    *testing.T:普通单元测试
    *testing.B:benchmark基准测试,一般用于测试性能
    *testing.M:测试主函数,在运行指定测试函数之前首先运行测试文件中的func TestMain(m *testing.M),TestMain运行在主goroutine中。

如待测函数为Add:

  1. func Add(a int, b int) int {
  2.     return a + b
  3. }

  1. func TestAdd(t *testing.T) {
  2.     if result := Add(1, 2); result != 3 {
  3.         t.Errorf("1 + 2 = 3, not %d", result)
  4.     }
  5.     if result := Add(-1, -2); result != -3 {
  6.         t.Errorf("-1 + -2 = -3, not %d", result)
  7.     }
  8. }

执行机制

go test过程首先扫描*_test.go的文件,会生成一个临时的main包用于调用相应的测试函数,然后构建、运行、产生测试结果,最后清理测试中生成的临时文件。

常用执行模式

1、仅运行一个测试函数

go test -run TestAdd

2、运行一个包下所有的测试函数

根目录下运行指定包

go test awesomeProject/testt 

或进入指定包下再执行

go test -v

其中 -v用于显示具体用例测试结果,不是必须的

注意,当有两个测试函数 TestAdd、TestAdd0时,执行go test -run TestAdd会将这两个都进行测试。

测试打印

测试函数与其它go函数一样都是函数,可使用fmt、log等进行打印,但参数t自带了常用的打印方法:

  1.     // 打印日志
  2.     t.Log()
  3.     t.Logf()
  4.     
  5.     // 打印错误,遇到错误终止执行
  6.     t.Fatal()
  7.     t.Fatalf()
  8.     
  9.     // 打印错误,遇到错误继续执行
  10.     t.Error()
  11.     t.Errorf()

子测试

子测试是指在测试场景下允许使用t.Run来创建不同的子测试用例。

举例如下:

  1. func TestAdd(t *testing.T) {
  2.     testsAdd := []struct {
  3.         a, b int
  4.         want int
  5.     }{
  6.         {0, 1, 1},
  7.         {1, 2, 3},
  8.         {2, 2, 4},
  9.         {0, -1, -1},
  10.         {-3, 4, 1},
  11.         {-3, 1, -2},
  12.         {-1, -1, -2},
  13.         {-1, -2, -3},
  14.     }
  15.     for _, tt := range testsAdd {
  16.         t.Run("", func(t *testing.T) {
  17.             if got := Add(tt.a, tt.b); got != tt.want {
  18.                 t.Errorf("Add(%d,%d) = %d, want %d", tt.a, tt.b, got, tt.want)
  19.             }
  20.         })
  21.     }
  22. }

这种写法直观、清晰,新增用例方便、格式统一。用例正常时结果如下:

  1. PASS
  2. ok      awesomeProject/testt    0.754s

若将第一个用例写错模拟Add函数问题,结果如下:

  1. --- FAIL: TestAdd (0.00s)
  2.     --- FAIL: TestAdd/#00 (0.00s)
  3.         f_test.go:35: Add(0,1) = 1, want 2
  4. FAIL
  5. exit status 1
  6. FAIL    awesomeProject/testt    0.669s

帮助函数Helper()

改造上述测试函数,设定写法如下:

  1. func TestAddWithHelper(t *testing.T) {
  2.     type model struct {
  3.         a, b int
  4.         want int
  5.     }
  6.     execRun := func(t *testing.T, tt model) {
  7.         // t.Helper()
  8.         if got := Add(tt.a, tt.b); got != tt.want {
  9.             t.Errorf("Add(%d,%d) = %d, want %d", tt.a, tt.b, got, tt.want)  // 52行
  10.         }
  11.     }
  12.     execRun(t, model{a: 0, b: 1, want: 0})  // 56行 设定错误,模拟测试不通过
  13.     execRun(t, model{a: 1, b: 2, want: 3})
  14. }

多个自定义execRun在执行时都有出问题的可能,对于上述代码,当不加t.Helper()时提示如下:

  1. --- FAIL: TestAddWithHelper (0.00s)
  2.     f_test.go:52: Add(0,1) = 1, want 0
  3. FAIL
  4. FAIL    awesomeProject/testt    0.707s
  5. FAIL

可以看到是t.Errorf这一行,不是特别直观,下面加上t.Helper(),结果如下:

  1. --- FAIL: TestAddWithHelper (0.00s)
  2.     f_test.go:56: Add(0,1) = 1, want 0
  3. FAIL
  4. FAIL    awesomeProject/testt    0.743s
  5. FAIL

可以看到直接提示是56行的问题。

测试覆盖率

go test awesomeProject/testt -run TestAdd0 -cover

其中-cover测试表示本次测试启用覆盖率检测。

覆盖率的含义是,代码被测试套件覆盖的百分比,即测试中至少被运行一次的代码占总代码的比例。

将覆盖率结果生成报告:

go test awesomeProject/testt -run TestAdd0 -cover -coverprofile=a

如上,-coverprofile=a 参数会将覆盖率信息输出到文件a中,接着使用go tool调用本地浏览器来生成一个html格式的报告:

go tool cover -html=a

效果如下 

77b55392177d4665bcbeb85dccffde80.png

加函数测试了,减函数未测试。 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/999119
推荐阅读
相关标签
  

闽ICP备14008679号