使用第三方测试工具
发布者:admin 发表于:438天前 阅读数:560 评论:0

Go测试有许多有用的工具。 这些工具可以让你更容易地了解每个功能级别的代码覆盖率,还可以使用断言工具来减少测试代码行。 本文将介绍 github.com/axw/gocov和github.com/smartystreets/goconvey 软件包,以展示其中的一些功能。此外 github.com/smartystreets/goconvey 包支持断言和运行时测试。

实践

获取第三方库:

go get github.com/axw/gocov 
go get github.com/smartystreets/goconvey/

建立 funcs.go:

package tools

import (
    "fmt"
)

func example() error {
    fmt.Println("in example")
    return nil
}

var example2 = func() int {
    fmt.Println("in example2")
    return 10
}

建立 structs.go:

package tools

import (
    "errors"
    "fmt"
)

type c struct {
    Branch bool
}

func (c *c) example3() error {
    fmt.Println("in example3")
    if c.Branch {
        fmt.Println("branching code!")
        return errors.New("bad branch")
    }
    return nil
}

建立 funcs_test.go:

package tools

import (
    "testing"

    . "github.com/smartystreets/goconvey/convey"
)

func Test_example(t *testing.T) {
    tests := []struct {
        name string
    }{
        {"base-case"},
    }
    for _, tt := range tests {
        Convey(tt.name, t, func() {
            res := example()
            So(res, ShouldBeNil)
        })
    }
}

func Test_example2(t *testing.T) {
    tests := []struct {
        name string
    }{
        {"base-case"},
    }
    for _, tt := range tests {
        Convey(tt.name, t, func() {
            res := example2()
            So(res, ShouldBeGreaterThanOrEqualTo, 1)
        })
    }
}

建立 structs_test.go:

package tools

import (
    "testing"

    . "github.com/smartystreets/goconvey/convey"
)

func Test_c_example3(t *testing.T) {
    type fields struct {
        Branch bool
    }
    tests := []struct {
        name    string
        fields  fields
        wantErr bool
    }{
        {"no branch", fields{false}, false},
        {"branch", fields{true}, true},
    }
    for _, tt := range tests {
        Convey(tt.name, t, func() {
            c := &c{
                Branch: tt.fields.Branch,
            }
            So((c.example3() != nil), ShouldEqual, tt.wantErr)
        })
    }
}

运行:

$ gocov test | gocov report
ok github.com/agtorre/go-cookbook/chapter8/tools 0.006s
coverage: 100.0% of statements
github.com/agtorre/go-cookbook/chapter8/tools/struct.go
c.example3 100.00% (5/5)
github.com/agtorre/go-cookbook/chapter8/tools/funcs.go example
100.00% (2/2)
github.com/agtorre/go-cookbook/chapter8/tools/funcs.go @12:16
100.00% (2/2)
github.com/agtorre/go-cookbook/chapter8/tools ----------
100.00% (9/9)
Total Coverage: 100.00% (9/9)

执行goconvey命令,会在浏览器中显示:

说明

本节演示了如何使用goconvry。与前面的小节相比,Convey关键字基本替代了t.Run,并且可以生成在goconvey生成的UI中显示的标签,但与t,Run的表现有所不同。如果你有嵌套的这样测试块:

Convey("Outer loop", t, func(){
    a := 1
    Convey("Inner loop", t, func() {
        a = 2
    })
    Convey ("Inner loop2", t, func(){
        fmt.Println(a)
    })
})

使用goconvey命令,将打印1。如果我们使用t.Run,将打印2。换句话说,t.Run按顺序运行测试,永远不会重复。此行为对于将变量设置在外部代码块中非常有用。如果混合使用,就必须记住这种区别。

在使用convey断言时,在UI中有成功的复选标记和其他统计信息。使用它还可以减少if检查单行的大小,甚至可以创建自定义断言。

如果保留goconvey Web界面并打开通知,则在保存代码时,将自动运行测试,并且将收到有关任何增加或减少覆盖范围以及构建失败时的通知。

以上功能都可以单独或一起使用。

在努力提高测试覆盖率时,gocov工具非常有用。它可以快速识别尚未测试的函数,并帮助了解覆盖率报告。此外,gocov可用于生成使用 github.com/matm/gocov-html 包随Go代码一起提供的备用HTML报告。