Eli's Blog

1. GoConvey简介

  • GoConvey是一款针对Go语言的测试辅助开发包,在兼容Go原生测试的基础上,又拓展出便利的语法和大量的内置判断条件,减轻开发人员负担。
  • 提供实时监控代码编译测试的程序,配以舒服的Web解码,能够让一个开发人员从此不再排斥写单元测试

2. 安装

1
go get github.com/smartystreets/goconvey

3. 编写测试

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
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() {
_, err := Division(10, 0)
So(err, ShouldNotBeNil)
})

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

4. 运行测试


 上一页

go