一、介绍
是一款针对Golang的测试框架,可以管理和运行测试用例,同时提供了丰富的断言函数,并支持很多 Web 界面特性。
GoConvey 网站 : http://smartystreets.github.io/goconvey/
GoConvey 支持 go test。可直接在终端窗口和浏览器上使用。
安装
go get github.com/smartystreets/goconvey #安装中可能会提示golang.org/x/tools/go/packages安装错误, 这里直接去github上下载后,解压到上面提示错误的文件位置 https://github.com/golang/tools/
在$GOPATH/bin目录下新增了GoConvey框架的可执行程序goconvey,运行后就可以访问web的UI界面了
http://localhost:8080/
二、代码
#goconvey_demo.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("被除数不能为 0") } return a / b, nil } func SquareSubtract(a, b int) int { return a*a + b*b }
#goconvey_text.go注意必须以_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() { num, err := Division(10, 2) So(err, ShouldBeNil) So(num, ShouldEqual, 5) }) Convey("除以 0", func() { _, err := Division(10, 0) So(err, ShouldNotBeNil) }) }) } func TestSquareSubtract(t *testing.T) { Convey("将两数平方相加",t, func() { So(SquareSubtract(5,6),ShouldEqual,61) }) }
打开命令行,然后输入 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) === RUN TestSquareSubtract 将两数平方相加 . 7 total assertions --- PASS: TestSquareSubtract (0.00s) PASS ok awesomeProject/goconvey 0.658s