资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Go语言开发(十九)、GoConvey测试框架-创新互联

Go语言开发(十九)、GoConvey测试框架

一、GoConvey简介

1、GoConvey简介

GoConvey是一款针对Golang的测试框架,可以管理和运行测试用例,同时提供了丰富的断言函数,并支持多种Web界面特性。
官方地址:
https://github.com/smartystreets/goconvey
安装:
go get github.com/smartystreets/goconvey

公司主营业务:成都网站设计、成都网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出丹阳免费做网站回馈大家。

2、GoConvey的特点

GoConvey支持 go test,可直接在终端窗口和浏览器上使用。GoConvey特点如下:
A、直接集成go test
B、巨大的回归测试套件
C、可读性强的色彩控制台输出
D、完全自动化的Web UI
E、测试代码生成器

二、GoConvey测试用例编写

1、GoConvey标准断言

官方文档:
https://github.com/smartystreets/goconvey/wiki
GoConvey自带大量的标准断言函数,可以通过So()使用。
(1)通用相等比较
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)
(2)数值比较
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)
带容差比较,默认容差为0.0000000001
So(1.0, ShouldNotAlmostEqual, 0.9, .0001)
(3)数据集合比较
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)
支持map、切片、通道、字符串
(4)字符串比较
So("asdf", ShouldStartWith, "as")
So("asdf", ShouldNotStartWith, "df")
So("asdf", ShouldEndWith, "df")
So("asdf", ShouldNotEndWith, "df")
So("asdf", ShouldContainSubstring, "sd")
So("asdf", ShouldNotContainSubstring, "er")
So("adsf", ShouldBeBlank)
So("asdf", ShouldNotBeBlank)
(5)异常比较
So(func(), ShouldPanic)
So(func(), ShouldNotPanic)
So(func(), ShouldPanicWith, "") // or errors.New("something")
So(func(), ShouldNotPanicWith, "") // or errors.New("something")
(6)类型检查
So(1, ShouldHaveSameTypeAs, 0)
So(1, ShouldNotHaveSameTypeAs, "asdf")
(7)时间比较
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())

2、测试用例编写

官方推荐使用导入GoConvey的辅助包以减少冗余的代码:
. "github.com/smartystreets/goconvey/convey"
GoConvey包导入在工程代码中如下:

import (
   "testing"

   . "github.com/smartystreets/goconvey/convey"
)

每个单元测试的名称需以 Test 开头,并需要接收一个类型为 *testing.T 的参数。
每个测试用例需要使用Convey函数包裹起来,第一个参数为string类型的测试用例描述;第二个参数一般为*testing.T;第三个参数为不接收任何参数也不返回任何值的函数(通常以闭包的形式书写)。
Convey语句可以无限嵌套,以体现各个测试用例之间的关系,只有最外层的 Convey 需要传入*testing.T类型变量,内层嵌套的Convey不需要传入。

func TestAdd(t *testing.T) {
   Convey("将两数相加", t, func() {
      So(Add(1, 2), ShouldEqual, 3)
   })
}

GoConvey提供了Convey/So的Skip宏,用于想忽略某些断言操作但不想删除或注释的场景。
SkipConvey:表明相应的闭包函数将不被执行。
SkipSo:表明相应的断言将不被执行。
当存在SkipConvey或SkipSo时,测试日志中会显式打上"skipped"形式的标记:
当测试代码中存在SkipConvey时,相应闭包函数中不管是否为SkipSo,都将被忽略,测试日志中对应的符号仅为一个"⚠"
当测试代码Convey语句中存在SkipSo时,测试日志中每个So对应一个"✔"或"✘",每个SkipSo对应一个"⚠",按实际顺序排列
不管存在SkipConvey还是SkipSo时,测试日志中都有字符串"{n} total assertions (one or more sections skipped)",其中{n}表示测试中实际已运行的断言语句数。

3、测试用例示例

Operator.go文件:

package Operator

import (
   "errors"
)

func Add(a, b int) int {
   return a + b
}

func Subtract(a, b int) int {
   return a - b
}

func Multiply(a, b int) int {
   return a * b
}

func Division(a, b int) (int, error) {
   if b == 0 {
      return 0, errors.New("Divisor is  0")
   }
   return a / b, nil
}

Operator_test.go文件:

package Operator

import (
   "testing"

   . "github.com/smartystreets/goconvey/convey"
)

func TestAdd(t *testing.T) {
   Convey("将两数相加", t, func() {
      So(Add(1, 2), ShouldEqual, 3)
   })
}

func TestSubtract(t *testing.T) {
   Convey("将两数相减", t, func() {
      So(Subtract(1, 2), ShouldEqual, -1)
   })
}

func TestMultiply(t *testing.T) {
   Convey("将两数相乘", t, func() {
      So(Multiply(3, 2), ShouldEqual, 6)
   })
}

func TestDivision(t *testing.T) {
   Convey("将两数相除", t, func() {

      Convey("除以非 0 数", func() {
         num, err := Division(10, 2)
         So(err, ShouldBeNil)
         So(num, ShouldEqual, 5)
      })

      Convey("除以 0", func() {
         _, err := Division(10, 0)
         So(err, ShouldNotBeNil)
      })
   })
}

4、定制断言函数

So的函数原型如下:

func So(actual interface{}, assert assertion, expected ...interface{})
type assertion func(actual interface{}, expected ...interface{}) string

当assertion的返回值为""时表示断言成功,否则表示失败,GoConvey框架中的相关代码为:

const (
   success                = ""
   needExactValues        = "This assertion requires exactly %d comparison values (you provided %d)."
   needNonEmptyCollection = "This assertion requires at least 1 comparison value (you provided 0)."
)

定制断言函数如下:

func ShouldSummerBeComming(actual interface{}, expected ...interface{}) string {
   if actual == "summer" && expected[0] == "comming" {
      return ""
   } else {
      return "summer is not comming!"
   }
}

单元测试如下:

func TestSummer(t *testing.T) {
   Convey("TestSummer", t, func() {
      So("summer", ShouldSummerBeComming, "comming")
      So("winter", ShouldSummerBeComming, "comming")
   })
}

根据ShouldSummerBeComming的实现,闭包中第一个So将断言成功,第二个So将断言失败。

三、测试结果查看

1、命令行测试结果查看

GoConvey兼容Go原生的单元测试,可以直接使用Go命令来执行测试。
在测试代码目录下运行go test命令:
go test -v 
测试执行结果如下:

=== RUN   TestAdd

将两数相加 ✔

1 total assertion

--- PASS: TestAdd (0.00s)
=== RUN   TestSubtract

将两数相减 ✔

2 total assertions

--- PASS: TestSubtract (0.00s)
=== RUN   TestMultiply

将两数相乘 ✔

3 total assertions

--- PASS: TestMultiply (0.00s)
=== RUN   TestDivision

将两数相除
除以非 0 数 ✔✔
除以 0 ✔

6 total assertions

--- PASS: TestDivision (0.00s)
PASS
ok     GoExample/GoConvey 0.002s

2、Web界面测试结果查看

查看goconvey用法
goconvey -h
-cover:开启覆盖率统计功能
-depth int:扫描目录的深度,-1:扫描无穷深度,0:扫描当前目录
-excludedDirs string:将某些目录排除在扫描外,多个目录使用逗号分割
-host string:指定开启HTTP服务的主机
-launchBrowser:触发自动开启浏览器,默认为true
-port int:指定HTTP服务的端口
-workDir string:指定工作目录,默认为当前目录
在测试用例源码目录下运行goconvey:
goconvey -port 8081
在浏览器打开:
http://localhost:8081
结果如下:
Go语言开发(十九)、GoConvey测试框架

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


网页标题:Go语言开发(十九)、GoConvey测试框架-创新互联
网站URL:http://cdkjz.cn/article/digdij.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220