为什么要使用goconvey测试程序

goconvey 集成go test,go test 无缝接入。管理运行测试用例,而且提供了丰富的函数断言、非常友好的WEB界面,直观的查看测试结果。

如果没有goconvey的话,编写一个测试结果,首先运行被测试函数,然后判断被测试函数的运行结果,各种if判断,各种输出提示信息,而且回归测试也比较麻烦。但是如果使用了goconvey这些都就变得无比的简单。

还是看些使用代码比较简单明了。

怎么使用goconvey测试程序

第一步当然是安装goconvey

go get github.com/smartystreets/goconvey

看下被测试的代码

package main

import "fmt"

type Student struct {
Num int
Name string Chinaese int
English int
Math int
} func NewStudent(num int, name string) (*Student,error) {
if num < 1 || len(name) < 1 {
return nil,fmt.Errorf("num name empty")
}
stu := new(Student)
stu.Num = num
stu.Name = name
return stu,nil
} func (this *Student) GetAve() (int,error) {
score := this.Chinaese + this.English + this.Math
if score == 0 {
return 0,fmt.Errorf("score is 0")
}
return score/3,nil
}

主要看下goconvey的测试代码

package main

import (
"testing"
. "github.com/smartystreets/goconvey/convey"
) func TestNew(t *testing.T) {
Convey("start test new", t, func() {
stu,err := NewStudent(0,"")
Convey("have error", func() {
So(err, ShouldBeError)
})
Convey("stu is nil", func() {
So(stu, ShouldBeNil)
})
})
} func TestScore(t *testing.T) {
stu,_ := NewStudent(1,"test")
Convey("if error", t, func() {
_,err := stu.GetAve()
Convey("have error", func() {
So(err, ShouldBeError)
})
}) Convey("normal", t, func() {
stu.Math = 60
stu.Chinaese = 70
stu.English = 80
score,err := stu.GetAve()
Convey("have error", func() {
So(err, ShouldBeError)
})
Convey("score > 60", func() {
So(score, ShouldBeGreaterThan, 60)
})
})
}

进入到test代码目录,执行 go test

=== RUN   TestNew

  start test new
have error ✔
stu is nil ✔ 2 total assertions --- PASS: TestNew (0.00s)
=== RUN TestScore if error
have error ✔ 3 total assertions normal
have error ✘
score > 60 ✔ Failures: * /data/www/go/src/test/student_test.go
Line 35:

其实命令行显示的是有颜色标识的。期望出现的结果都会打上对勾,如果期望出现而没有出现的都会打上叉。

还有更好玩的WEB界面。进入的test代码的目录,然后执行 goconvey 会打开一个WEB界面,更加友好的标识出了测试的结果,测试了多少次,有几个通过,几个失败,一目了然。



其实使用特别简单

引入类库,启动Convey函数,剩下的就是调用So各种断言各种比较

import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
Convey("desc", t, func() {
So(var, function)
})

基本平常开发中的比较函数基本都有,看下比较的函数列表,看着貌似都涵盖了。

Convey("Equality assertions should be accessible", t, func() {
thing1a := thing{a: "asdf"}
thing1b := thing{a: "asdf"}
thing2 := thing{a: "qwer"} So(1, ShouldEqual, 1)
So(1, ShouldNotEqual, 2)
So(1, ShouldAlmostEqual, 1.000000000000001)
So(1, ShouldNotAlmostEqual, 2, 0.5)
So(thing1a, ShouldResemble, thing1b)
So(thing1a, ShouldNotResemble, thing2)
So(&thing1a, ShouldPointTo, &thing1a)
So(&thing1a, ShouldNotPointTo, &thing1b)
So(nil, ShouldBeNil)
So(1, ShouldNotBeNil)
So(true, ShouldBeTrue)
So(false, ShouldBeFalse)
So(0, ShouldBeZeroValue)
So(1, ShouldNotBeZeroValue)
}) Convey("Numeric comparison assertions should be accessible", t, func() {
So(1, ShouldBeGreaterThan, 0)
So(1, ShouldBeGreaterThanOrEqualTo, 1)
So(1, ShouldBeLessThan, 2)
So(1, ShouldBeLessThanOrEqualTo, 1)
So(1, ShouldBeBetween, 0, 2)
So(1, ShouldNotBeBetween, 2, 4)
So(1, ShouldBeBetweenOrEqual, 1, 2)
So(1, ShouldNotBeBetweenOrEqual, 2, 4)
}) Convey("Container assertions should be accessible", t, func() {
So([]int{1, 2, 3}, ShouldContain, 2)
So([]int{1, 2, 3}, ShouldNotContain, 4)
So(map[int]int{1: 1, 2: 2, 3: 3}, ShouldContainKey, 2)
So(map[int]int{1: 1, 2: 2, 3: 3}, ShouldNotContainKey, 4)
So(1, ShouldBeIn, []int{1, 2, 3})
So(4, ShouldNotBeIn, []int{1, 2, 3})
So([]int{}, ShouldBeEmpty)
So([]int{1}, ShouldNotBeEmpty)
So([]int{1, 2}, ShouldHaveLength, 2)
}) Convey("String assertions should be accessible", t, func() {
So("asdf", ShouldStartWith, "a")
So("asdf", ShouldNotStartWith, "z")
So("asdf", ShouldEndWith, "df")
So("asdf", ShouldNotEndWith, "as")
So("", ShouldBeBlank)
So("asdf", ShouldNotBeBlank)
So("asdf", ShouldContainSubstring, "sd")
So("asdf", ShouldNotContainSubstring, "af")
}) Convey("Panic recovery assertions should be accessible", t, func() {
So(panics, ShouldPanic)
So(func() {}, ShouldNotPanic)
So(panics, ShouldPanicWith, "Goofy Gophers!")
So(panics, ShouldNotPanicWith, "Guileless Gophers!")
}) Convey("Type-checking assertions should be accessible", t, func() { // NOTE: Values or pointers may be checked. If a value is passed,
// it will be cast as a pointer to the value to avoid cases where
// the struct being tested takes pointer receivers. Go allows values
// or pointers to be passed as receivers on methods with a value
// receiver, but only pointers on methods with pointer receivers.
// See:
// http://golang.org/doc/effective_go.html#pointers_vs_values
// http://golang.org/doc/effective_go.html#blank_implements
// http://blog.golang.org/laws-of-reflection So(1, ShouldHaveSameTypeAs, 0)
So(1, ShouldNotHaveSameTypeAs, "1") So(bytes.NewBufferString(""), ShouldImplement, (*io.Reader)(nil))
So("string", ShouldNotImplement, (*io.Reader)(nil))
}) Convey("Time assertions should be accessible", t, func() {
january1, _ := time.Parse(timeLayout, "2013-01-01 00:00")
january2, _ := time.Parse(timeLayout, "2013-01-02 00:00")
january3, _ := time.Parse(timeLayout, "2013-01-03 00:00")
january4, _ := time.Parse(timeLayout, "2013-01-04 00:00")
january5, _ := time.Parse(timeLayout, "2013-01-05 00:00")
oneDay, _ := time.ParseDuration("24h0m0s") So(january1, ShouldHappenBefore, january4)
So(january1, ShouldHappenOnOrBefore, january1)
So(january2, ShouldHappenAfter, january1)
So(january2, ShouldHappenOnOrAfter, january2)
So(january3, ShouldHappenBetween, january2, january5)
So(january3, ShouldHappenOnOrBetween, january3, january5)
So(january1, ShouldNotHappenOnOrBetween, january2, january5)
So(january2, ShouldHappenWithin, oneDay, january3)
So(january5, ShouldNotHappenWithin, oneDay, january1)
So([]time.Time{january1, january2}, ShouldBeChronological)
})

特别实用的一个测试类库,养成写完代码使用goconvey做测试的好习惯,也顺便覆盖下使用方法和案例,定能让开发事半功倍,减少Bug率。

golang开发:类库篇(五)go测试工具goconvey的使用的更多相关文章

  1. golang开发:类库篇(三)命令行工具cli的使用

    为什么要使用命令行 觉得这个问题不应该列出来,又觉得如果初次进行WEB开发的话,可能会觉得所有的东西都可以使用API去做,会觉得命令行没有必要. 其实,一个生产的项目命令行是绕不过去的.比如运营需要导 ...

  2. golang开发:环境篇(五)实时加载工具gin的使用

    gin 工具是golang开发中非常有用且有效的工具,有效的提高了开发调试go程序的效率. 为什么要使用gin 我们知道golang是编译型语言,这就表示go程序的每次改动,如果需要查看改动结果都必须 ...

  3. golang开发:类库篇(一) Zap高性能日志类库的使用

    为什么要用zap来写日志 原来是写PHP的,一直用的error_log,第一次写Go项目的时候,还真不知道该怎么写日志,后来就按照PHP的写法自己不成规范的捣鼓写.去了新公司之后,发现用的是zap.后 ...

  4. golang开发:类库篇(二) Redis连接池的使用

    为什么要使用连接池 一个数据库服务器只拥有有限的连接资源,一旦所有的连接资源都在使用,那么其它需要连接的资源就只能等待释放连接资源.所以,在连接资源有限的情况下,提高单位时间的连接的使用效率,缩短连接 ...

  5. golang开发:类库篇(四)配置文件解析器goconfig的使用

    为什么要使用goconfig解析配置文件 目前各语言框架对配置文件书写基本都差不多,基本都是首先配置一些基础变量,基本变量里面有环境的配置,然后通过环境变量去获取该环境下的变量.例如,生产环境跟测试环 ...

  6. modern.IE – Web 开发必备的 IE 浏览器测试工具

    modern.IE 是微软推出的一个开发人员中心,提供免费的工具和资源,旨在使您能够花更少的时间来测试各种版本的 Internet Explorer,并留出更多时间在现代 Web 上构建重要的内容.m ...

  7. Golang之框架篇-Windows环境bee工具运行beego

    bee工具简介及好处     bee 工具是一个为了协助快速开发 beego 项目而创建的项目,通过 bee 你可以很容易的进行 beego 项目的创建.热编译.开发.测试.和部署. 强烈推荐新手或J ...

  8. golang开发:环境篇(六) Go运行监控Supervisord的使用

    为什么要使用Supervisord 17年第一次写Go项目的时候,用Go开发项目倒没没费多大劲,很快就开发完成了.到了在测试环境部署的时候,由于不知道有 Supervisord 这个软件,着实花了些功 ...

  9. golang开发:环境篇(四)包管理器 glide的使用

    glide 是golang项目开发中是特别重要的软件,没有它,golang的项目可能都无法发布. 为什么要使用glide 平时我们开发Go项目的时候,使用第三方的包的时候都直接使用go get 去获取 ...

随机推荐

  1. 转载 | float 清除浮动的7种方法

    什么叫浮动:浮动会使当前标签脱离文档流,产生上浮的效果,同时还会影响周边元素(前后标签)及父级元素的位置和width,height属性.下面用一个小例子来看一看浮动的全过程:1.首先我们新建一个网页, ...

  2. AutoCAD C#二次开发

    https://www.cnblogs.com/gisoracle/archive/2012/02/19/2357925.html using System; using System.Collect ...

  3. PHP 数组转字符串后仍保留数组格式

    写此方法的目的是,我想把一个PHP数组配置文件读进程序,添加些配置,然后在写入文件: var_export 方法会把原来的配置打乱(比如数组序号我没有加,他自动给我加上 0,1,2,3...),而且格 ...

  4. F#周报2019年第34期

    新闻 高效的F#,提示与技巧 Fable 社区资源 Visual Studio提示与技巧:为.NET增加生产力 无风险地尝试Compositional IT的培训包--如果没有增加任何价值,可以得到完 ...

  5. Spring学习之旅(八)--SpringMVC请求参数

    现在我们已经完成了一个无参的接口了,但是应用中有很多需要携带参数的场景,我们来看看 ** SpringMVC** 对它的支持. 参数绑定 SpringMVC 提供了一种绑定机制,通过这个机制可以从请求 ...

  6. .NET Core 使用 K8S ConfigMap的正确姿势

    背景 ASP.NET Core默认的配置文件定义在appsetings.json和appsettings.{Environment}.json文件中. 这里面有一个问题就是,在使用容器部署时,每次修改 ...

  7. javascript简单实现深浅拷贝

    深浅拷贝知识在我们的日常开发中还算是用的比较多,但是之前的状态一直都是只曾听闻,未曾使用(其实用了只是自己没有意识到),所以今天来跟大家聊一聊js的深浅拷贝: 首先我们来了解一下javascript的 ...

  8. java设计模式5.组合模式、门面模式、享元模式、桥接模式

    组合模式 在面向对象的语言中,树结构有着巨大的威力,一个基于继承的类型的等级结构便是一个数结构,一个基于合成的对象结构也是一个数结构.组合模式将部分与整体的关系用树结构表示出来,使得客户端把一个个单独 ...

  9. 深度学习常见的优化方法(Optimizer)总结:Adam,SGD,Momentum,AdaGard等

    机器学习的常见优化方法在最近的学习中经常遇到,但是还是不够精通.将自己的学习记录下来,以备不时之需 基础知识: 机器学习几乎所有的算法都要利用损失函数 lossfunction 来检验算法模型的优劣, ...

  10. iOS中的几个重要方法

    iOS开发中几个重要的方法: 加载类到内存,程序刚启动的时候调用,调用在main函数之前 1.+(void)load{ } 初始化类,类第一次使用的时候调用一次 2.+(void)initialize ...