优雅的编写单元测试

简 介

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

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

在这里插入图片描述

特 性

gotestWeb 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 命令。

快速上手

goconveyGo testing

在项目下面安装 goconery

gogetgithub.com/smartystreets/goconvey

在项目下创建一个 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
}

创建一个单元测试并且使用 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界面 了
在这里插入图片描述

然后在浏览器打开 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 函数我做了处理,所以不会抛异常,如果异常了就是下面的👇截图
在这里插入图片描述

在这里插入图片描述

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

界面一些帮助信息

在这里插入图片描述

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 爱上写测试!!写出优雅的测试代码。

最后|资源分享

下面这些是我的收集和整理的资料,对于开始学习【软件测试】或是技能进阶的朋友来说,绝对是最全面的教程仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你

在这里插入图片描述

程序媛木子微信公众号里资源将免费获取,技术交流群(644956177)群里有技术大佬的各种技术交流和经验之谈。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值