关于golang:优雅的写单元测试

4次阅读

共计 3516 个字符,预计需要花费 9 分钟才能阅读完成。

简 介

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

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

特 性

  • 间接整合 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 界面

  1. 而后在浏览器关上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 爱上写测试!!写出优雅的测试代码。

相干材料

goconvey: https://github.com/smartystre…

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

正文完
 0