go语言使用GoConvey框架进行测试
本周作业为在go-online上完成一个最小堆算法,在完成之后我使用GoConvey进行测试。
要想写出好的的代码,必须学会测试框架,对于golang,可以使用自带的go test
测试,也可以使用其他框架如
GoConvey,GoStub,GoMock,Monkey,本次我学习使用GoConvey。
安装GoConvey
go get github.com/smartystreets/goconvey
需要等待较长的一段时间,然后查看$GOPATH/src/github.com
目录下增加了smartystreets
文件夹即可。
使用GoConvey
将作业代码复制到go工作空间中命名为myheap.go
,并且在同一目录下创建myheap_test.go
,
先go build
或者go install
生成包,不需main主函数。因为要测试的函数需要有返回值,所以简单修改一下作业中的函数,将nodes返回。
myheap_test.go文件如下:
package myheap
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestInit(t *testing.T) {
Convey("test", t, func() {
nodes := []Node{
Node{
3},
Node{
6},
Node{
9},
Node{
1},
Node{
2},
Node{
5},
Node{
8},
Node{
4},
Node{
7},
}
Convey("Test Init()", func(){
So(Init(nodes), ShouldResemble, []Node{
Node{
1},
Node{
2},
Node{
5},
Node{
4},
Node{
3},
Node{
9},
Node