0
点赞
收藏
分享

微信扫一扫

Go Web 编程入门: 一探优秀测试库 GoConvey

0 前言

为了弥补内置测试库的缺陷,诞生了优秀的第三方库​​ goconvey​​,目前 gtihub stars 数量达到了 7.4k,官网链接:​​http://goconvey.co/​​。

口号:Write behavioral tests in your editor. Get live results in your browser.

Go Web 编程入门: 一探优秀测试库 GoConvey_命令行工具

GoConvey 完美兼容 Go 内置的 ​​testing​​ 库,提供命令行工具简化内置的测试执行命令,测试会自动运行,提供更加直观的 Web 界面,最重要的是能够很轻易的得到测试报告。

1 GoConvey的特性

  • 直接集成 Go 内置测试工具,比如可以直接使用go test
  • 大量的回归测试套件
  • 提供可读的,彩色的控制台输出
  • 完全自动化的 Web UI
  • 测试代码生成器
  • 桌面提醒(可选)
  • 自动在终端中运行自动测试脚本
  • 可立即在Sublime Text 中打开测试问题对应的代码行 (some assembly required)

2 下载安装

$ go get github.com/smartystreets/goconvey
$ $GOPATH/bin/goconvey

安装成功将看到如下输出:

yuzhou@yuzhou:~/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

3 如何使用

结构如下:

Go Web 编程入门: 一探优秀测试库 GoConvey_命令行工具_02

  1. 创建一个utils.go 文件,写一个整数求和的 Sum 函数:
package utils

func Sum(nums ...int) int {
sum := 0
for i := 0; i < len(nums); i++ {
sum += nums[i]
}

return 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)
})
})
}
  • 第 6 行中 以. 导入库的方式简化调用。
  • 第 9 行代码,单元测试函数的命名及注意事项和内置库testing 要求一致(比如测试函数以 Test_开头,传入参数为*testing.T)。
  • 函数体中第一层 Convey 提供3个参数:Test Sum(说明测试的名称)、tfunc()
  • 嵌套的 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)
})
})
}

因为 GoConvey 很好的集成了 Go 原生 tes在终端中执行 ​​go test -v​​ 命令,得到如下结果,测试通过:

=== RUN   TestSum

Test Sum
1 + 2 + 3 + 4 + 5 ✔
1 + 2022 ✔


2 total assertions

--- PASS: TestSum (0.00s)
PASS
ok utils.go 0.012s

在终端着有着非常人性化带有彩色的界面,如图所示:

Go Web 编程入门: 一探优秀测试库 GoConvey_自定义_03

断言方法

除了上图中使用的 ​​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)
数字数量比较类
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)
包含类
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
字符串类
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)
panic类
So(func(), ShouldPanic)
So(func(), ShouldNotPanic)
So(func(), ShouldPanicWith, "") // or errors.New("something")
So(func(), ShouldNotPanicWith, "") // or errors.New("something")
类型检查类
So(1, ShouldHaveSameTypeAs, 0)
So(1, ShouldNotHaveSameTypeAs, "asdf")
时间和时间间隔类
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())
自定义断言方法

如果上面列出来的断言方法都不能满足你的需要,那么你还可以按照下面的格式自定义一个断言方法。

注意:​​<>​​中的内容是你需要按照实际需求替换的内容。

func should<do-something>(actual interface{}, expected ...interface{}) string {
if <some-important-condition-is-met(actual, expected)> {
return "" // 返回空字符串表示断言通过
}
return "<一些描述性消息详细说明断言失败的原因...>"
}

4 图形界面

  1. 安装了库即自动安装了该命令行工具,如果失效,可以查看 GOBIN 是否加入 PATH 环境变量。在项目目录下执行 goconvey 就会自动启动 Web 服务。

Go Web 编程入门: 一探优秀测试库 GoConvey_github_04

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

Go Web 编程入门: 一探优秀测试库 GoConvey_github_05

具体内容包括:

  • 整体覆盖率
  • 单个测试文件的运行情况
  • 重新运行测试
  • 单个测试文件的覆盖情况或者未覆盖情况。
  1. 访问http://127.0.0.1:8080/reports/,可以看测试覆盖率:

Go Web 编程入门: 一探优秀测试库 GoConvey_自定义_06

点击这个 ​​.txt​​ 文件,有如下结果:

mode: set
utils.go/utils.go:3.27,5.33 2 1
utils.go/utils.go:9.2,9.12 1 1
utils.go/utils.go:5.33,7.3 1 1
  1. 通过http://127.0.0.1:8080/composer.html 编写半自动测试用例:

Go Web 编程入门: 一探优秀测试库 GoConvey_命令行工具_07

5 总结

本文介绍了 Go 语言非常好用且强大的第三方库 GoConvey,然后进行下载和安装,最后使用了一个简单的加法函数进行了一个完成的 GoConvey 使用流程,该测试库提供了更多的断言方法,可以在实际开发过程中进行运用。

通过运行 ​​goconvey​​ 命令,可以在本地运行一个强大 Web 图形界面,建议读者将 goconvey 配合内置的 testing 使用,以提高开发效率。

参考链接

  • ​​Go单测从零到溜系列5—goconvey的使用​​
  • ​​[Golang] GoConvey测试框架使用指南​​
  • ​​GoConvey框架使用指南​​
举报

相关推荐

0 条评论