当前位置:网站首页>Go Web 编程入门: 一探优秀测试库 GoConvey
Go Web 编程入门: 一探优秀测试库 GoConvey
2022-06-30 21:43:00 【51CTO】
0 前言
为了弥补内置测试库的缺陷,诞生了优秀的第三方库 goconvey,目前 gtihub stars 数量达到了 7.4k,官网链接: http://goconvey.co/。
口号:Write behavioral tests in your editor. Get live results in your browser.

GoConvey 完美兼容 Go 内置的 testing 库,提供命令行工具简化内置的测试执行命令,测试会自动运行,提供更加直观的 Web 界面,最重要的是能够很轻易的得到测试报告。
1 GoConvey的特性
- 直接集成 Go 内置测试工具,比如可以直接使用
go test - 大量的回归测试套件
- 提供可读的,彩色的控制台输出
- 完全自动化的 Web UI
- 测试代码生成器
- 桌面提醒(可选)
- 自动在终端中运行自动测试脚本
- 可立即在 Sublime Text 中打开测试问题对应的代码行 ( some assembly required)
2 下载安装
安装成功将看到如下输出:
[email protected]:~/GoProjects$ go get github.com/smartystreets/goconvey/convey
go: downloading github.com/smartystreets/assertions v1.2.0
go: downloading github.com/jtolds/gls v4.20.0+incompatible
go: downloading github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1
go get: added github.com/smartystreets/goconvey v1.7.2
- 1.
- 2.
- 3.
- 4.
- 5.
3 如何使用
结构如下:

- 创建一个
utils.go文件,写一个整数求和的 Sum 函数:
然后创建一个单元测试的文件 utils_test.go 文件:
package utils
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestSum(t *testing.T) {
Convey("Test Sum", t, func() {
Convey("1 + 2 + 3 + 4 + 5", func() {
So(Sum(1, 2, 3, 4, 5), ShouldEqual, 15)
})
Convey("1 + 2022", func() {
So(Sum(1, 2022), ShouldEqual, 2023)
})
})
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 第 6 行中 以
.导入库的方式简化调用。 - 第 9 行代码,单元测试函数的命名及注意事项和内置库
testing要求一致(比如测试函数以 Test_开头,传入参数为*testing.T)。 - 函数体中第一层 Convey 提供3个参数:
Test Sum(说明测试的名称)、t和func()。 - 嵌套的 Convey 层提供两个参数:
1 + 2 + 3 + 4 + 5(说明测试的名称)和func()。 - 使用 So 来判断预期值和输出,这里使用
ShouldEqual。
package utils
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestSum(t *testing.T) {
Convey("Test Sum", t, func() {
Convey("1 + 2 + 3 + 4 + 5", func() {
So(Sum(1, 2, 3, 4, 5), ShouldEqual, 15)
})
Convey("1 + 2022", func() {
So(Sum(1, 2022), ShouldEqual, 2023)
})
})
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
因为 GoConvey 很好的集成了 Go 原生 tes在终端中执行 go test -v 命令,得到如下结果,测试通过:
在终端着有着非常人性化带有彩色的界面,如图所示:

断言方法
除了上图中使用的 ShouldEqual 方法外,GoConvey为我们提供了很多种类断言方法在 So() 函数中使用。
一般相等类
So(thing1, ShouldEqual, thing2)
So(thing1, ShouldNotEqual, thing2)
So(thing1, ShouldResemble, thing2) // 用于数组、切片、map和结构体相等
So(thing1, ShouldNotResemble, thing2)
So(thing1, ShouldPointTo, thing2)
So(thing1, ShouldNotPointTo, thing2)
So(thing1, ShouldBeNil)
So(thing1, ShouldNotBeNil)
So(thing1, ShouldBeTrue)
So(thing1, ShouldBeFalse)
So(thing1, ShouldBeZeroValue)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
数字数量比较类
So(1, ShouldBeGreaterThan, 0)
So(1, ShouldBeGreaterThanOrEqualTo, 0)
So(1, ShouldBeLessThan, 2)
So(1, ShouldBeLessThanOrEqualTo, 2)
So(1.1, ShouldBeBetween, .8, 1.2)
So(1.1, ShouldNotBeBetween, 2, 3)
So(1.1, ShouldBeBetweenOrEqual, .9, 1.1)
So(1.1, ShouldNotBeBetweenOrEqual, 1000, 2000)
So(1.0, ShouldAlmostEqual, 0.99999999, .0001) // tolerance is optional; default 0.0000000001
So(1.0, ShouldNotAlmostEqual, 0.9, .0001)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
包含类
So([]int{2, 4, 6}, ShouldContain, 4)
So([]int{2, 4, 6}, ShouldNotContain, 5)
So(4, ShouldBeIn, ...[]int{2, 4, 6})
So(4, ShouldNotBeIn, ...[]int{1, 3, 5})
So([]int{}, ShouldBeEmpty)
So([]int{1}, ShouldNotBeEmpty)
So(map[string]string{"a": "b"}, ShouldContainKey, "a")
So(map[string]string{"a": "b"}, ShouldNotContainKey, "b")
So(map[string]string{"a": "b"}, ShouldNotBeEmpty)
So(map[string]string{}, ShouldBeEmpty)
So(map[string]string{"a": "b"}, ShouldHaveLength, 1) // supports map, slice, chan, and string
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
字符串类
So("asdf", ShouldStartWith, "as")
So("asdf", ShouldNotStartWith, "df")
So("asdf", ShouldEndWith, "df")
So("asdf", ShouldNotEndWith, "df")
So("asdf", ShouldContainSubstring, "稍等一下") // optional 'expected occurences' arguments?
So("asdf", ShouldNotContainSubstring, "er")
So("adsf", ShouldBeBlank)
So("asdf", ShouldNotBeBlank)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
panic类
类型检查类
时间和时间间隔类
So(time.Now(), ShouldHappenBefore, time.Now())
So(time.Now(), ShouldHappenOnOrBefore, time.Now())
So(time.Now(), ShouldHappenAfter, time.Now())
So(time.Now(), ShouldHappenOnOrAfter, time.Now())
So(time.Now(), ShouldHappenBetween, time.Now(), time.Now())
So(time.Now(), ShouldHappenOnOrBetween, time.Now(), time.Now())
So(time.Now(), ShouldNotHappenOnOrBetween, time.Now(), time.Now())
So(time.Now(), ShouldHappenWithin, duration, time.Now())
So(time.Now(), ShouldNotHappenWithin, duration, time.Now())
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
自定义断言方法
如果上面列出来的断言方法都不能满足你的需要,那么你还可以按照下面的格式自定义一个断言方法。
注意:<>中的内容是你需要按照实际需求替换的内容。
4 图形界面
- 安装了库即自动安装了该命令行工具,如果失效,可以查看 GOBIN 是否加入 PATH 环境变量。在项目目录下执行 goconvey 就会自动启动 Web 服务。

默认访问地址为 http://127.0.0.1:8080/,可在 Web 界面上查看到具体的信息,如下界面:

具体内容包括:
- 整体覆盖率
- 单个测试文件的运行情况
- 重新运行测试
- 单个测试文件的覆盖情况或者未覆盖情况。
- 访问
http://127.0.0.1:8080/reports/,可以看测试覆盖率:

点击这个 .txt 文件,有如下结果:
- 通过
http://127.0.0.1:8080/composer.html编写半自动测试用例:

5 总结
本文介绍了 Go 语言非常好用且强大的第三方库 GoConvey,然后进行下载和安装,最后使用了一个简单的加法函数进行了一个完成的 GoConvey 使用流程,该测试库提供了更多的断言方法,可以在实际开发过程中进行运用。
通过运行 goconvey 命令,可以在本地运行一个强大 Web 图形界面,建议读者将 goconvey 配合内置的 testing 使用,以提高开发效率。
参考链接
边栏推荐
- PyTorch量化实践(1)
- Introduce an online platform for multi omics integration and network visual analysis
- 《ClickHouse原理解析与应用实践》读书笔记(1)
- Iclr'22 spotlight | how to measure the amount of information in neural network weights?
- Clickhouse native monitoring item, system table description
- The 16th Heilongjiang Provincial Collegiate Programming Contest
- 1-16 路由的概念
- Deployment and use of Nacos
- Icml2022 | utility theory of sequential decision making
- Open the jupyter notebook/lab and FAQ & settings on the remote server with the local browser
猜你喜欢

1-2 install and configure MySQL related software

Why have the intelligent investment advisory products collectively taken off the shelves of banks become "chicken ribs"?

Bloom filter

It is urgent for enterprises to protect API security

《ClickHouse原理解析与应用实践》读书笔记(1)

jupyter notebook/lab 切换conda环境

Radar data processing technology

Spatiotemporal data mining: an overview

Multi table operation - foreign key constraint

Akk bacteria - the next generation of beneficial bacteria
随机推荐
Upgrade Kube with unknown flag: --network plugin
全面认识痛风:症状、风险因素、发病机理及管理
做一个 Scrollbar 的思考
Anaconda下安装Jupyter notebook
Apply for vector bus protocol color picture wallpaper hanging picture, very good!
Side sleep ha ha ha
请问,启牛证券开户,可以开户吗?安全吗?你想要的答案全在这里
1-18 创建最基本的express服务器&创建路由的API模块
【回溯】全排列 leetcode46
PyTorch量化实践(2)
Test medal 1234
ceshi deces
ArcGIS construction and release of simple road network data service and rest call test
1-15 nodemon
How to move forward when facing confusion in scientific research? How to give full play to women's advantages in scientific research?
Oprator-1 first acquaintance with oprator
Three techniques for reducing debugging time of embedded software
Sqlserver gets the data of numbers, Chinese and characters in the string
vim 常用快捷键
What about degradation of text generation model? Simctg tells you the answer