( ︎ ●—● )

GolangThursday系列-BDD行为测试开发GoConvey

2018-05-17 15:42:11

Go GolangThursday BDD Package

注:无闻大师的系列视频
Goconvey官方文档

主要特性

1.测试代码优雅,简介
2.集成原生测试
3.全自动编译测试
4.详细展示测试结果及覆盖率
5.高可读的命令行输出结果
6.半自动化书写测试用例

Install

1
go get github.com/smartystreets/goconvey

示例代码:
main.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package main

import (
"errors"
)

func Add(a, b int) int {
return a + b
}

func Subtract(a, b int) int {
return a - b
}

func Multiply(a, b int) int {
return a * b
}

func Division(a, b int) (int, error) {
if b == 0 {
return 0, errors.New("被除数不能为 0")
}
return a / b, nil
}

Convey && So

So进行断言操作

main_test.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main

import (
"testing"

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

func TestAdd(t *testing.T) {
Convey("将两数相加", t, func() {
So(Add(1, 2), ShouldEqual, 3)
})
}

func TestSubtract(t *testing.T) {
Convey("将两数相减", t, func() {
So(Subtract(1, 2), ShouldEqual, -1)
})
}

func TestMultiply(t *testing.T) {
Convey("将两数相乘", t, func() {
So(Multiply(3, 2), ShouldEqual, 6)
})
}

func TestDivision(t *testing.T) {
Convey("将两数相除", t, func() {

Convey("除以非 0 数", func() {
num, err := Division(10, 2)
So(err, ShouldBeNil)
So(num, ShouldEqual, 5)
})

Convey("除以 0", func() {
_, err := Division(10, 0)
So(err, ShouldNotBeNil)
})
})
}

Assertions 断言

General Equality

1
2
3
4
5
6
7
8
9
10
11
So(thing1, ShouldEqual, thing2)
So(thing1, ShouldNotEqual, thing2)
So(thing1, ShouldResemble, thing2) // a deep equals for arrays, slices, maps, and structs
So(thing1, ShouldNotResemble, thing2)
So(thing1, ShouldPointTo, thing2)
So(thing1, ShouldNotPointTo, thing2)
So(thing1, ShouldBeNil)
So(thing1, ShouldNotBeNil)
So(thing1, ShouldBeTrue)
So(thing1, ShouldBeFalse)
So(thing1, ShouldBeZeroValue)

Numeric 数字判断

1
2
3
4
5
6
7
8
9
10
So(1, ShouldBeGreaterThan, 0)
So(1, ShouldBeGreaterThanOrEqualTo, 0)
So(1, ShouldBeLessThan, 2)
So(1, ShouldBeLessThanOrEqualTo, 2)
So(1.1, ShouldBeBetween, .8, 1.2)
So(1.1, ShouldNotBeBetween, 2, 3)
So(1.1, ShouldBeBetweenOrEqual, .9, 1.1)
So(1.1, ShouldNotBeBetweenOrEqual, 1000, 2000)
So(1.0, ShouldAlmostEqual, 0.99999999, .0001) // tolerance is optional; default 0.0000000001
So(1.0, ShouldNotAlmostEqual, 0.9, .0001)

Collections 集合判断

1
2
3
4
5
6
7
8
9
10
11
So([]int{2, 4, 6}, ShouldContain, 4)
So([]int{2, 4, 6}, ShouldNotContain, 5)
So(4, ShouldBeIn, ...[]int{2, 4, 6})
So(4, ShouldNotBeIn, ...[]int{1, 3, 5})
So([]int{}, ShouldBeEmpty)
So([]int{1}, ShouldNotBeEmpty)
So(map[string]string{"a": "b"}, ShouldContainKey, "a")
So(map[string]string{"a": "b"}, ShouldNotContainKey, "b")
So(map[string]string{"a": "b"}, ShouldNotBeEmpty)
So(map[string]string{}, ShouldBeEmpty)
So(map[string]string{"a": "b"}, ShouldHaveLength, 1) // supports map, slice, chan, and string

Strings 字符串判断

1
2
3
4
5
6
7
8
So("asdf", ShouldStartWith, "as")
So("asdf", ShouldNotStartWith, "df")
So("asdf", ShouldEndWith, "df")
So("asdf", ShouldNotEndWith, "df")
So("asdf", ShouldContainSubstring, "sd") // optional 'expected occurences' arguments?
So("asdf", ShouldNotContainSubstring, "er")
So("adsf", ShouldBeBlank)
So("asdf", ShouldNotBeBlank)

panic 判断

1
2
3
4
So(func(), ShouldPanic)
So(func(), ShouldNotPanic)
So(func(), ShouldPanicWith, "") // or errors.New("something")
So(func(), ShouldNotPanicWith, "") // or errors.New("something")

Type checking 类型判断

1
2
So(1, ShouldHaveSameTypeAs, 0)
So(1, ShouldNotHaveSameTypeAs, "asdf")

time.Time (and time.Duration)

1
2
3
4
5
6
7
8
9
So(time.Now(), ShouldHappenBefore, time.Now())
So(time.Now(), ShouldHappenOnOrBefore, time.Now())
So(time.Now(), ShouldHappenAfter, time.Now())
So(time.Now(), ShouldHappenOnOrAfter, time.Now())
So(time.Now(), ShouldHappenBetween, time.Now(), time.Now())
So(time.Now(), ShouldHappenOnOrBetween, time.Now(), time.Now())
So(time.Now(), ShouldNotHappenOnOrBetween, time.Now(), time.Now())
So(time.Now(), ShouldHappenWithin, duration, time.Now())
So(time.Now(), ShouldNotHappenWithin, duration, time.Now())

实际测试

go test

使用原生的go test 进行测试

1
go test

Alt text

1
go test -v	#查看详细的test内容

Alt text

goconvey

goconvey依赖于官方tool的cover,如果没有的话请自行安装

1
go get -u golang.org/x/tools/cmd/cover

执行:

1
goconvey

然后就可以使用本地:8080端口查看测试结果
Alt text

Goconvey 测试gin 路由

举个例子:
main.go

1
2
3
4
5
6
7
8
func main(){
r := gin.New()
r.GET("/", homePage)
r.Run()
}
func homePage(c *gin.Context){
c.JSON(200,gin.H{"message":"homepage"})
}

main_test.go

1
2
3
4
5
6
7
func TestHomePage(t *testing.T){
w := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
test_mux := gin.New()
test_mux.ServeHTTP(w, req)
So(w.Code, ShouldEqual, http.StatusOK)
}