go测试之Convey+monkey+coverage组合

go语言的测试采用的框架如下:

一、go test

在前面进行了beego框架的搭建,beego自带的有test,但是通过IDE(如VScode)不能直接运行测试用例,如下:
在这里插入图片描述

解决方法:

1、可以通过在测试用例里加main来调用测试用例
2、直接在终端进行运行,如在test目录下运行go test -v可以输出test目录下所以用例运行结果及显示详细的流程
在这里插入图片描述
也可以运行某一个测试用例:

$ go test helloworld_test.go
ok          command-line-arguments        0.003s
$ go test -v helloworld_test.go
=== RUN   TestHelloWorld
--- PASS: TestHelloWorld (0.00s)
        helloworld_test.go:8: hello world
PASS
ok          command-line-arguments        0.004s

二、goConvey

goConvey是作为外层框架进行单元测试管理。

1、安装
$ go get github.com/smartystreets/goconvey

go get下载不下来的可以通过https://github.com/smartystreets/goconvey 下载zip,然后解压到$ GOPATH/src/github.com/smartystreets/goconvey go clean -r -i 和 go install -v 安装,如果在$GOPATH/bin 下生成goconvey二进制文件,说明安装成功

2、写测试用例
package test

import (
	"net/http"
	"net/http/httptest"
	"testing"
	"runtime"
	"path/filepath"
	_ "myproject/routers"

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

func init() {
	_, file, _, _ := runtime.Caller(0)
	apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator))))
	beego.TestBeegoInit(apppath)
}


// TestBeego is a sample to run an endpoint test
func TestBeego(t *testing.T) {
	r, _ := http.NewRequest("GET", "/", nil)
	w := httptest.NewRecorder()
	beego.BeeApp.Handlers.ServeHTTP(w, r)

	beego.Trace("testing", "TestBeego", "Code[%d]\n%s", w.Code, w.Body.String())

	Convey("Subject: Test Station Endpoint\n", t, func() {
	        Convey("Status Code Should Be 200", func() {
	                So(w.Code, ShouldEqual, 200)
	        })
	        Convey("The Result Should Not Be Empty", func() {
	                So(w.Body.Len(), ShouldBeGreaterThan, 0)
	        })
	})
}


3、浏览器查看

在$GOPATH/bin/下执行./goconvey(也可直接点击goconvey二进制文件,不过要手动关闭8080),一开始会报错,浏览器也是404,后来在提示下添加目录,可以正常浏览了
在这里插入图片描述
在这里插入图片描述
但是此时浏览器没有任何test运行,上面的文件目录显示是/usr/lib/go/bin,然后把目录修改为/usr/lib/go/src/github.com/smartystreets/goconvey,最后刷新一下就可以显示测试用例了。
在这里插入图片描述
如果想看自己的项目,修改为自己项目目录即可。
在这里插入图片描述

三、monkey

1、安装

go get github.com/bouk/monkey 或者 https://github.com/bouk/monkey下载解压到$ GOPATH/src/github.com/bouk/monkey(无需生成二进制文件)

2、使用

直接在测试用例中导入monkey调用相关函数就可,可参考文档 https://github.com/bouk/monkey

四、gocoverage

代码测试用例的覆盖率有三种方式:

1、go命令
root@ZTE:/usr/lib/go/src/myproject/tests# go test  -cover
PASS
coverage: 0.0% of statements
ok      _/usr/lib/go/src/myproject/tests        0.021s
2、goConvey

goConvey浏览器在运行测试用例时,自动会显示覆盖率。
在这里插入图片描述

3、gocoverage

https://github.com/philwinder/gocoverage下载gocoverage组件,解压到$ GOPATH/src/github.com/ go clean -r -i 和 go install -v 安装,如果在$GOPATH/bin 下生成gocoverage二进制文件,说明安装成功。然后直接

gocoverage [flags...] rootpath filterpath1 ... filterpathn

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用一些工具来自动生成测试脚本,比如GoConvey和Testify等。这些工具可以帮助你编写和运行测试,并生成测试报告。 使用GoConvey,你可以在项目中编写测试代码,并通过浏览器查看测试结果。首先,你需要在项目中安装GoConvey: ``` go get github.com/smartystreets/goconvey ``` 然后,在你的测试文件中导入GoConvey包,并使用`convey`函数创建一个测试套件: ```go package main_test import ( "testing" . "github.com/smartystreets/goconvey/convey" ) func TestMath(t *testing.T) { Convey("Given two numbers", t, func() { a := 2 b := 3 Convey("When adding them together", func() { sum := a + b Convey("The result should be correct", func() { So(sum, ShouldEqual, 5) }) }) }) } ``` 运行测试脚本: ``` goconvey ``` 这将启动一个Web界面,你可以在浏览器中查看测试结果。 另外一个流行的测试工具是Testify。你可以使用Testify编写更传统的单元测试,并生成测试报告。首先,你需要在项目中安装Testify: ``` go get github.com/stretchr/testify ``` 然后,你可以在你的测试文件中导入Testify包,并编写测试代码: ```go package main_test import ( "testing" "github.com/stretchr/testify/assert" ) func TestMath(t *testing.T) { a := 2 b := 3 sum := a + b assert.Equal(t, 5, sum) } ``` 运行测试脚本: ``` go test ``` 这将执行你的测试并生成测试报告。 这些工具都可以帮助你自动生成测试脚本,并且提供了丰富的断言函数和测试辅助工具,使得编写和运行测试更加方便。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值