一、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 Operatorimport ("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 Operatorimport ("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

结果如下:

Hello Go(十九)、GoConvey测试框架相关推荐

  1. [Golang] GoConvey测试框架使用指南

    GoConvey 是一款针对Golang的测试框架,可以管理和运行测试用例,同时提供了丰富的断言函数,并支持很多 Web 界面特性. GoConvey 网站 : http://smartystreet ...

  2. python布尔测试对象_面试题十九期-测试开发面试题之python系列-这个中~

    坚持梦想 就算所有人都不支持你.这条路会很曲折,你也会一度认为是不是自己选错了,但只要坚持,就算最后没有成功,但努力了就不会有遗憾. 测试开发python系列面试题 01 单选题 1.下列哪个语句在P ...

  3. 十九、Beego框架快速入门

    Beego框架快速入门 1.框架了解 go语言的web框架:beego,gin,echo等等,那为什么我们选择beego呢? 第一,beego是中国人开发的,开发文档比较详细,beego官网网址: h ...

  4. 微软出品自动化神器【Playwright+Java】系列(十二)测试框架的设计与开发

    一.前言 大家好,我是六哥! 又有好长一段时间没更文了,不是我懒,而是确实在更文上,没有以前积极了,这里是该自我检讨的. 其实不是我不积极,而是相对更文学习来说,优先级不是最高. 对我而言,目前最重要 ...

  5. 三十九、Scrapy-redis框架分布式部署

    @Author:Runsen 文章目录 scrapy-redis框架 分布式原理 分布式爬虫的实现 scrapy-redis框架的安装 部署scrapy-redis 运行slave 运行master ...

  6. 问题二十九:测试ray tracing中camera几个主要参数

    camera(vec3 lookfrom, vec3 lookat, vec3vup, float vfov, float aspect, float aperture, float focus_di ...

  7. 二十九、PHP框架Laravel学习笔记——Debugbar 调试器

    二.安装使用 通过 composer 在项目中安装 Debugbar,命令如下: composer require barryvdh/laravel-debugbar 生成一个配置文件,给用户配置,可 ...

  8. 十九、PHP框架Laravel学习笔记——批量赋值和软删除

    一.批量赋值 上一节增删改中,新增中我们发现需要进行批量赋值的许可: 一般情况下,是为了防止提交过来的字段在部分场景中不需要或不能: 所以,我们需要通过黑白名单机制进行过滤掉必要的字段: //通过提交 ...

  9. python中scrapy的middleware是干嘛的_Python之爬虫(十九) Scrapy框架中Download Middleware用法...

    这篇文章中写了常用的下载中间件的用法和例子. Downloader Middleware处理的过程主要在调度器发送requests请求的时候以及网页将response结果返回给spiders的时候,所 ...

最新文章

  1. php防止网站被镜像,网站被等恶意镜像的解决、反制措施详细教程
  2. 2020德勤面试开始了吗_2020下半年教师资格证面试时间已出!12月10日开始报名
  3. 如何在MFC中调用CUDA
  4. 如何在时间紧迫情况下进行机器学习:构建标记的新闻 数据 库 开发 标记 网站 阅读1629 原文:How we built Tagger News: machine learning on a
  5. display:none和visible:hidden两者的区别
  6. kafka自动提交offset失败:Auto offset commit failed
  7. Spark+Alluxio性能调优十大技巧
  8. 为什么你学HTML5前端这么久,水平还是烂成渣?
  9. 网络抓包,不能使用路由器和交换机,必须是具有镜像功能的HUB(集线器)
  10. Officescan防毒墙安装部署手册
  11. linux oracle12c rman,12C CDB模式下RMAN备份与恢复
  12. creo怎么返回上一步_creo零基础教程,教你creo4.0复制粘贴怎么用
  13. usb转232串口线驱动android,prolific usb转串口驱动下载
  14. 在iOS 14中使用带有SF Symbols 2的彩色图标
  15. 局域网SDN技术硬核内幕 二 云网CP的日常恩爱——硬件VXLAN转发平面
  16. 利用ARCGIS和QGIS画等值线图
  17. 给女生发信息不回怎么办?不想被拉黑,就好好掌握这几个技巧
  18. 杭电oj第1000题—— A + B Problem
  19. error: variable has incomplete type ‘QApplication‘ 错误解决
  20. Mysql+Canal1.1.5+Es实现数据同步

热门文章

  1. Error:Execution failed for task ':app:mergeDebugResources'. \re
  2. 告别移动飞信,选择电信天翼Live
  3. 打印显示服务器脱机win10,win10共享打印机显示脱机怎么处理_win10打印机老是脱机如何修复...
  4. ios通过app读取通讯录信息
  5. css预处理Less
  6. 论文笔记-Flocks, Herds, and Schools: A Distributed Behavioral Model
  7. 射手播放器SPlayer,编解码
  8. Mac微信小助手安装2.8.4防撤回闪退的解决方案
  9. JsBridge的学习
  10. mandriava 上下载ed2k链接的文件