Go主题月 | 优雅的编写单元测试

717 阅读4分钟

简 介

相信各位Gopher 在编写代码的时候都离不开编写单元测试Go语言虽然自带单元测试功能,但是使用起来有点乏味和枯燥。在GoConvey诞生之前也出现了许多第三方辅助库。但没有一个辅助库能够像GoConvey这样优雅地书写代码的单元测试,简洁的语法和舒适的界面能够让一个不爱书写单元测试的开发人员从此爱上单元测试

GoConvey 是个相当不错的 Go 测试框架,兼容go testing,并且可直接在终端窗口和浏览器上使用。

GoConvey Web UI

特 性

  • 直接整合 go test
  • 丰富的测试断言规则
  • 可读的彩色控制台输出
  • 全自动的Web UI

快速体验

快速体验,先看看我们的代码

func TestSpec(t *testing.T) {
	Convey("给出一个带有起始值的整数", t, func() {
		x := 1

		Convey("当整数递增时", func() {
			x++

			Convey("该值的结果应是大于>1 ?", func() {
				So(x, ShouldEqual, 2)
			})
		})
	})
}

然后跑起来:go test -v

测试结果

So函数的第一个参数你自己被测试逻辑函数,第二个参数 断言规则,第三个参数预计结果,可以看到层次清晰,并且兼容go test -v命令。

快速上手

  • 在你的项目安装goconvey
  • Go testing文件里面编写测试代码
  • 运行即可看到测试结果
  1. 在项目下面安装goconery
go get github.com/smartystreets/goconvey
  1. 在项目下创建一个operator.go文件,这个文件只是为了演示单元测试使用的例子

operator.go文件

package testing

import(
	  "errors"
)

// 为了测试编写的加减乘除函数
func Add(x,y int) int {
    return x + y
}

func  Subtract(x,y int) int {
    return x - y
}

func Division(x,y int)(int,error){
    if y == 0 {
        return 0,errors.New("被除数不能为零")
    }
    return x / y,nil
}

func Multiply(x,y int) int{
    return x * y
}
  1. 创建一个单元测试并且使用goconery进行单元测试

operator_test.go文件

// 单元测试函数和普通go测试命名一样
func TestAdd(t *testing.T) {

    //  Convey 函数 第一个参数是测试名称
    //  t = *testing.T
    // 	func = 自定义测试逻辑
    Convey("两数相加测试,11 + 11 = 22 ?", t, func() {
        x, y := 11, 11
        // So 函数比较结果 ShouldEqual 是相等的意思
        So(Add(x,y), ShouldEqual, 22)
    })
}

注意: 在默认的testing包之外还要导入一个. "github.com/smartystreets/goconvey/convey"包!!!

然后测试看看:

go test -v
=== RUN   TestAdd

  两数相加测试 ✔


1 total assertion

--- PASS: TestAdd (0.00s)
PASS
ok  	github.com/higker/testing	0.005s

正确✅

接下来我们多写几个函数测试一下:

func TestSubtract(t *testing.T) {
    Convey("测试两数相减,22 - 11 != 22 ?", t, func() {
        x, y := 22, 11
        // So 函数比较结果 ShouldNotEqual 是不相等的意思
        So(Subtract(x,y), ShouldNotEqual, 22)
    })
}

func TestMultiply(t *testing.T) {
    Convey("将两数相乘,11 * 2 = 22 ?", t, func() {
        x, y := 11, 2
        So(Multiply(x,y), ShouldEqual, 22)
    })
}

新加了2个函数,我们在下面目录启动命令行输入goconvey,这个时候就可以启动web界面

goconvey命令启动web ui

  1. 然后在浏览器打开http://127.0.0.1:8080/,即可看到我们刚刚编写的测试结果通过界面的方式展示出来了

http://127.0.0.1:8080/

是不是很cool很哇塞😜。。

现在我在测试故意加入一个运算错误的逻辑,看看会怎么样。

func TestDivision(t *testing.T) {
	Convey("将两数相除", t, func() {
      x, y := 10, 2
      Convey("除以非 0 数", func() {
          n,_ := Division(x, y)
          So(n, ShouldEqual, 5)
      })
  
      Convey("除以 0", func() {
          y = 0 // 我们将被除数设置为0
          _,err := Division(x, y)
          So(err, ShouldNotBeNil)
      })
	})
}

结果

只要GoConvey正在运行,测试结果就会在您的浏览器窗口中自动更新。该设计是响应式的,因此如果需要将其放在代码旁边,则可以将浏览器紧紧压缩,这时我不需要手动跑我们的测试,因为我刚刚通过goconvey起来了一个进程,它会监测我们的代码改动,并且实时在web界面上展示我的测试结果。

上面的TestDivision函数我做了处理,所以不会抛异常,如果异常了就是下面的👇截图

可以看到界面error了显示成红色了

相关的错误信息

好了相关的演示到此为止了,goconery通过很多测试规则和断言规则,也可以自定义规则,大家可以在他的官方网站是查到相关的资料,大家可以自己动手试试,搞起来!!!

界面一些帮助信息

ui图解

Assertions 内置的断言

General Equality

So(thing, ShouldEqual, thing2)
So(thing, ShouldNotEqual, thing2)
So(thing, ShouldResemble, thing2)
So(thing, ShouldNotResemble, thing2)
So(thing, ShouldPointTo, thing2)
So(thing, ShouldNotPointTo, thing2)
So(thing, ShouldBeNil, thing2)
So(thing, ShouldNotBeNil, thing2)
So(thing, ShouldBeTrue)
So(thing, ShouldBeFalse)

Numeric Quantity comparison

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)

Collections

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})

Strings

So("asdf", ShouldStartWith, "as")
So("asdf", ShouldNotStartWith, "df")
So("asdf", ShouldEndWith, "df")
So("asdf", ShouldNotEndWith, "df")
So("asdf", ShouldContain, "sd")  // optional 'expected occurences' arguments?
So("asdf", ShouldNotContain, "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")

总 结

goconery会让一个不爱写单元测试的gopher爱上写测试!!写出优雅的测试代码。

相关资料

goconvey: github.com/smartystree…

本例子仓库:github.com/higker/goco…