阅读目录
GoGonvey简介
GoConvey 是一款针对 Go 语言的测试辅助开发包,在兼容 Go 原生测试的基础上,又拓展出便利的语法和大量的内置判断条件,减轻开发人员负担。
此外该工具还提供了实时监控代码编译测试的程序,配以舒服的 Web 面,能够让一个开发人员从此不再排除书写单元测试。
主要特性
- 测试代码优雅、简洁
- 直接集成 Go 原生测试
- 全自动编译测试
- 详细展示测试结果及覆盖率
- 高可读性的命令行输出结果
- 半自动化书写测试用例
下载安装
- 通过 gopm 安装:
gopm get github.com/smartystreets/goconvey
- 通过 go get 安装:
go get github.com/smartystreets/goconvey
root@192:~/www/test# go get github.com/smartystreets/goconvey
go: downloading github.com/smartystreets/goconvey v1.8.0
go: downloading golang.org/x/tools v0.7.0
go: downloading golang.org/x/sys v0.6.0
go: downloading golang.org/x/mod v0.9.0
go: added github.com/smartystreets/goconvey v1.8.0
go: added golang.org/x/mod v0.9.0
go: added golang.org/x/sys v0.6.0
go: added golang.org/x/tools v0.7.0
root@192:~/www/test#
书写代码
我们将实现四则运算的加、减、乘、除 4 个函数。
func Add(a, b int) int {
}
func Subtract(a, b int) int {
}
func Multiply(a, b int) int {
}
func Division(a, b int) (int, error) {
}
“imported and not used” 的错误信息表示在代码中导入了 “errors” 包,但在程序中没有使用它。
编写测试
导入相应的驱动包
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
主要使用 Convey() 和 So()函数来完成测试的定义和条件判断。
import 语句中的点 (.) 允许直接访问 “github.com/smartystreets/goconvey/convey” 包中的已导出名称,而无需显式使用包名作为前缀。
这样可以使代码更加简洁易读,但也可能导致名称冲突,如果两个包具有相同标识符的已导出名称。
需要注意的是,通常不建议在 Go 代码中使用点 (.) 导入语法,因为它可能使代码不够清晰,更容易出错。
建议始终使用完整的包名,除非有特殊原因要求使用点导入。
/root/www/test/main.go
package goconvey
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("被除数不能为零!")
}
return a / b, nil
}
/root/www/test/main_test.go
示由于 GOPROXY=offcompiler 环境变量禁用了模块查找功能,Go 模块系统无法导入 “github.com/smartystreets/goconvey/convey” 包。
GOPROXY 环境变量用于指定 Go 模块代理服务器的位置,用于下载和缓存模块依赖项。
当 GOPROXY 的值设置为 “offcompiler” 时,它完全禁用模块查找功能,这将阻止 Go 能够定位和下载所需的包。
要解决此问题,您可以删除 GOPROXY=offcompiler 环境变量或将其设置为指向有效的 Go 模块代理服务器的值。
您还可以尝试运行 “go mod tidy” 命令以更新和下载任何缺失的模块依赖项。
root@192:~/www/test# go mod tidy
go: downloading github.com/jtolds/gls v4.20.0+incompatible
go: downloading github.com/smartystreets/assertions v1.13.1
go: downloading github.com/gopherjs/gopherjs v1.17.2
root@192:~/www/test#
package goconvey
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)
})
})
}
运行测试
使用 Go 原生方法
$ go test
安装测试覆盖率工具
root@192:~/www/test# go get github.com/smartystreets/goconvey
root@192:~/www/test# go install github.com/smartystreets/goconvey
- 1、在$GOPATH/src目录下新增了github.com子目录,该子目录里包含了GoConvey框架的库代码。
- 2、在$GOPATH/bin目录下新增了GoConvey框架的可执行程序 goconvey。
使用 GoConvey 提供的自动化编译测试:
$ goconvey
然后,进入浏览器访问地址 localhost:8080/ 查看测试结果。
go test
root@192:~/www/test# go test
.
1 total assertion
.
2 total assertions
.
3 total assertions
...
6 total assertions
PASS
ok goconvey 0.002s
root@192:~/www/test# go test -v
=== RUN TestAdd
将两数相加 ✔
1 total assertion
--- PASS: TestAdd (0.00s)
=== RUN TestSubtract
将两数相减 ✔
2 total assertions
--- PASS: TestSubtract (0.00s)
=== RUN TestMultiply
将两数相乘 ✔
3 total assertions
--- PASS: TestMultiply (0.00s)
=== RUN TestDivision
将两数相乘
被乘数为0 ✔
被乘数不为0 ✔✔
6 total assertions
--- PASS: TestDivision (0.00s)
PASS
ok goconvey 0.002s
root@192:~/www/test#
被除数修改错误:So(num, ShouldEqual, 4)
root@192:~/www/test# go test -v
=== RUN TestAdd
将两数相加 ✔
1 total assertion
--- PASS: TestAdd (0.00s)
=== RUN TestSubtract
将两数相减 ✔
2 total assertions
--- PASS: TestSubtract (0.00s)
=== RUN TestMultiply
将两数相乘 ✔
3 total assertions
--- PASS: TestMultiply (0.00s)
=== RUN TestDivision
将两数相除
被除数为0 ✔
被除数不为0 ✔✘
Failures:
* /root/www/test/main_test.go
Line 36:
Expected: '4'
Actual: '5'
(Should be equal)
6 total assertions
--- FAIL: TestDivision (0.00s)
FAIL
exit status 1
FAIL goconvey 0.002s
root@192:~/www/test#
goconvey 测试
root@192:~/www/test# go get github.com/smartystreets/goconvey
root@192:~/www/test#
go install github.com/smartystreets/goconvey
root@192:~/www/test# goconvey
root@192:~/www/test# go get golang.org/x/tools/cmd/cover
go: downloading golang.org/x/tools/cmd/cover v0.1.0-deprecated
go: added golang.org/x/tools/cmd/cover v0.1.0-deprecated
root@192:~/www/test#
工具包
不能强的下载包管理工具
https://gopm.io
应用文档
https://github.com/smartystreets/goconvey/wiki