一、GoConvey简介1、GoConvey简介2、GoConvey的特点二、GoConvey测试用例编写1、GoConvey标准断言2、测试用例编写3、测试用例示例4、定制断言函数三、测试结果查看1、命令行测试结果查看2、Web界面测试结果查看

一、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

GoConvey总结相关推荐

  1. go语言的goconvey

    goconvey是一个支持golang的单元测试框架 goconvey能够自动监控文件修改并启动测试,并可以将测试结果实时输出到web界面 goconvey提供了丰富的断言简化测试用例的编写

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

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

  3. Go:测试库(GoConvey,testify,GoStub,GoMonkey)对比及简介

    文章目录 一.测试框架 stretchr/testify 1.stretchr/testify/assert 2.stretchr/testify/require 二.测试框架GoConvey 1. ...

  4. Hello Go(十九)、GoConvey测试框架

    一.GoConvey简介 1.GoConvey简介 GoConvey是一款针对Golang的测试框架,可以管理和运行测试用例,同时提供了丰富的断言函数,并支持多种Web界面特性. 官方地址: http ...

  5. goconvey简单介绍

    goconvey测试框架 1.说明 2.github仓库 3.提供的断言以及方法 3.1 类型 3.1.1 判断是否 3.1.2 数值比较 3.1.3 包含内容 3.1.4 字符串 3.1.5 异常 ...

  6. goconvey调研及学习

    文章目录 goconvey调研及学习 goconvey概述 安装 快速开始: 浏览器中查看测试结果 goconvey的使用场景 1.example_test 2.bowling_game 3. ass ...

  7. go语言使用GoConvey框架进行测试

    go语言使用GoConvey框架进行测试 本周作业为在go-online上完成一个最小堆算法,在完成之后我使用GoConvey进行测试. 要想写出好的的代码,必须学会测试框架,对于golang,可以使 ...

  8. golang开发:类库篇(五)go测试工具goconvey的使用

    为什么要使用goconvey测试程序 goconvey 集成go test,go test 无缝接入.管理运行测试用例,而且提供了丰富的函数断言.非常友好的WEB界面,直观的查看测试结果. 如果没有g ...

  9. GO单元测试-GoConvey

    原文:http://www.lampnick.com/php/732 特性 集成go test 可读的,带色彩的控制台输出 全自动Web UI 大量的回归测试套件 测试代码生成 快速开始 安装 $ c ...

最新文章

  1. eclipse team 没有svn
  2. 为什么你应该深入Github
  3. 15+ tar command usages with examples – Unix/Linux--reference
  4. 元宇宙iwemeta:元宇宙催生新的行业机会,看看你能抓住哪些机遇?
  5. 给力登场:15款免费的Windows系统工具
  6. 使用tab键分割的文章能快速转换成表格。( )_EXCEL的163种使用技巧集锦-42~62
  7. fprintf函数的用法matlab_极力推荐这个Matlab教程
  8. Autodesk Flame 2022 for Mac - 高级三维视觉特效合成软件
  9. [django]list_display 中包含外键内的字段
  10. 俄罗斯被指为 SolarWinds 供应链事件元凶,技术公司受制裁,常用5大漏洞遭曝光...
  11. Cookie enable 的检测
  12. RSA 非对称加密之 PKCS8 格式秘钥
  13. shellcode免杀
  14. 性能测试20--Analysis -- 内存与硬盘
  15. lisp princ详解_LISP - 输入和输出(Input Output)
  16. Android 客户端上开发人人客户端系列教程
  17. 仿微信、qq聊天,@好友功能
  18. win7下安装centOS7双系统
  19. python命令行窗口最大化_基于python的豆瓣FM(终端命令行界面)
  20. 直播APP开发技术原理分享

热门文章

  1. 通用服务网格框架Kuma开源了,可运行于任何平台!
  2. 淘宝数据库内核月报搜索工具
  3. 【渝粤教育】国家开放大学2018年春季 8616-21T食品营养卫生 参考试题
  4. slice()的用法
  5. 变频电源怎么区分单相还是三相
  6. 黑盒测试用例测试方法
  7. #009#献给阿尔吉侬的花束
  8. HTML 颜色代码网站
  9. [M贪心] lc1846. 减小和重新排列数组后的最大元素(贪心+双周赛51_3)
  10. 详细的OS X Yosemite 10.10懒人版安装教程