简书Go语言专栏

golang分层测试之单元测试-testing使用

2018-10-30  本文已影响147人  周学习的名字被占了

前言

testing框架使用

示例代码

package hello
import "testing"

func TestHelloWorld(t *testing.T) {
    t.Log("hello world")
}
  1. 每个测试文件必须以 _test.go 结尾,并使用go test命令执行测试模块,否则golang无法识别出测试模块
  2. 作为测试模块文件,必须导入testing包
  3. 功能测试方法必须以 Test 开头,一般是接被测方法的名字,如TestAdd,但不强制
  4. 测试用例会按照源代码中写的顺序依次执行
  5. 测试方法TestXxx()的参数是testing.T,用于收集记录测试结果
  6. testing中的断言需要自己编写,通过在方法中通过调用testing.TError, Errorf, FailNow, Fatal, FatalIf方法来进识别测试通过与否,调用Log方法用来记录测试的信息

命令行

go test [-c] [-i] [build flags] [packages] [flags for test binary]
$ go test -v helloworld_test.go
=== RUN   TestHelloWorld
--- PASS: TestHelloWorld (0.00s)
        helloworld_test.go:8: hello world
PASS
ok          command-line-arguments        0.004s
go test //默认执行当前目录下以xxx_test.go的测试文件
go test -v //可以看到详细的输出信息。
go test -v xxx_test.go //指定测试单个文件,但是该文件中如果调用了其它文件中的模块会报错
go test -v xxx_test.go xxx.go //指定测试单个文件和被测文件
go test -v -test.run Testxxx //指定某个测试函数运行,注意点是函数会用Testxxx去匹配所有包含Testxxx字符串作方法名的方法
go test -v -cover //显示覆盖的声明程度

实战应用

//demo by terrychow
package main
import "fmt"

func Add(x,y int) int   
{
return x+y
}

func main(){
  z:=Add(1,2)
  fmt.Print(z)
}

测试模块 demo_test.go

//demo_test by terrychow
package main
import "testing"

func  TestAdd(t *testing.T) {
    s:=Add(1,2)
    if s!=3{
        t.Error("Expected 3, got ", s)
    }
    
}
go test demo_test.go demo.go -v -cover
=== RUN   TestAdd
--- PASS: TestAdd (0.00s)
PASS
coverage: 33.3% of statements
ok      command-line-arguments  0.613s  coverage: 33.3% of statements
//demo_test by terrychow
package main
import "testing"

func  TestAdd(t *testing.T) {
    s:=Add(1,2)
    if s!=4{//此处修改了错误的返回
        t.Error("Expected 4, got ", s)
    }
    
}
=== RUN   TestAdd
--- FAIL: TestAdd (0.00s)
    demo_test.go:8: Expected 4, got  3
FAIL
coverage: 33.3% of statements
FAIL    command-line-arguments  0.668s

进阶玩法

示例代码

//demo by terrychow
package main
import "testing"

func TestGroupAdd(t *testing.T){
    var testdata=[] struct{
     arg1 int
     arg2 int
     sum int
}{
    {1,1,2},
    {-1,-1,-2},
    {-1,1,0},
}
    for _, test:=range testdata{
    sum:=Add(test.arg1,test.arg2)
    if sum!=test.sum{
    t.Errorf("arg1 %v and arg2 %v not except sum %v",test.arg1,test.arg2,test.sum)
    }
    }
}
=== RUN   TestGroupAdd
--- PASS: TestGroupAdd (0.00s)
PASS
coverage: 33.3% of statements
ok      command-line-arguments  0.583s  coverage: 33.3% of statements
package main
import "testing"

func TestGroupAdd(t *testing.T){
    var testdata=[] struct{
     arg1 int
     arg2 int
     sum int
}{
    {1,1,2},
    {-1,-1,-1},//此处的sum为错误值
    {-1,1,0},
}
    for _, test:=range testdata{
    sum:=Add(test.arg1,test.arg2)
    if sum!=test.sum{
    t.Errorf("arg1 %v and arg2 %v not except sum %v",test.arg1,test.arg2,test.sum)
    }
    }
}
=== RUN   TestGroupAdd
--- FAIL: TestGroupAdd (0.00s)
    demo1_test.go:17: arg1 -1 and arg2 -1 not except sum -1
FAIL
coverage: 33.3% of statements
FAIL    command-line-arguments  0.688s

小结

上一篇 下一篇

猜你喜欢

热点阅读