简 介

Gopher单元测试GoGoConveyGoConvey单元测试
GoConveyGogo testing

特 性

go testWeb 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
go get github.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)
    })
}
goconveyweb界面
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)
      })
    })
}
GoConveygoconveyweb界面
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")

总 结

goconerygopher

相干材料

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

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