为什么要使用goconvey测试程序

goconvey 集成go test,go test 无缝接入。管理运行测试用例,而且提供了丰富的函数断言、非常友好的WEB界面,直观的查看测试结果。
如果没有goconvey的话,编写一个测试结果,首先运行被测试函数,然后判断被测试函数的运行结果,各种if判断,各种输出提示信息,而且回归测试也比较麻烦。但是如果使用了goconvey这些都就变得无比的简单。
还是看些使用代码比较简单明了。

怎么使用goconvey测试程序

第一步当然是安装goconvey

go get github.com/smartystreets/goconvey

看下被测试的代码

package mainimport "fmt"type Student struct {Num intName stringChinaese intEnglish intMath 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 = numstu.Name = namereturn stu,nil
}func (this *Student) GetAve() (int,error) {score := this.Chinaese + this.English + this.Mathif score == 0 {return 0,fmt.Errorf("score is 0")}return score/3,nil
}

主要看下goconvey的测试代码

package mainimport ("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 = 60stu.Chinaese = 70stu.English = 80score,err := stu.GetAve()Convey("have error", func() {So(err, ShouldBeError)})Convey("score > 60", func() {So(score, ShouldBeGreaterThan, 60)})})
}

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

=== RUN   TestNewstart test newhave error ✔stu is nil ✔2 total assertions--- PASS: TestNew (0.00s)
=== RUN   TestScoreif errorhave error ✔3 total assertionsnormalhave error ✘score > 60 ✔Failures:* /data/www/go/src/test/student_test.goLine 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-reflectionSo(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开发:类库篇(一) Zap高性能日志类库的使用

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

  2. go之测试工具(goconvey、gostub、gomock...)

    1.gostub 包引用 go get github.com/prashantv/gostub 使用 //函数重构 var stubedFunc=func()//为函数打桩 stubs := Stub ...

  3. web可用性测试_Web开发人员和设计人员的最佳可用性测试工具

    web可用性测试 UX design is incomplete without user testing, which is an integral part of the process. It' ...

  4. 软件外包开发测试工具

    软件测试是软件项目中非常重要的一个环节,在软件项目上线前必须要将问题测出来,否则上线后出现大量问题不但可能引起经济损失,而且也会失去客户的信任.今天和大家分享软件测试中常用的一些工具,希望对大家有所帮 ...

  5. 十大开源Web应用安全测试工具

    点击蓝字关注我们 Web应用安全测试可对Web应用程序执行功能测试,找到尽可能多的安全问题,大大降低黑客入侵几率. 在研究并推荐一些最佳的开源Web应用安全测试工具之前,让我们首先了解一下安全测试的定 ...

  6. oracle 压测工具 ld,ORACLE压力测试工具

    Swingbench for oracleRAC使用方法图解 1 Swingbench 简述 1.1 概述 这是Oracle UK的一个员工在一个被抛弃的项目的基础上开发的.目前稳定版本2.2,最新版 ...

  7. 我保证你一定会喜欢的几个好用的 API 测试工具

    分享一下我调研并使用过的五种测试工具,适合不同的场景使用. 1.Insomia GOKu 旗下的开源工具,很轻量,支持设计.测试 API. 如果您只需要在开发过程中对 API 进行测试,可以试试它. ...

  8. 国标GB28181-2016级联测试工具

    ----------------------------------------更新--2021-10-25-------------------------------------------- 更 ...

  9. 手游测试工具ThreadingTest--对高仿版植物大战僵尸进行测试

    目前,市面上Android手游项目的测试都采用传统的手工测试方法,而手工测试受到诸多方面因素的限制,不利于版本迭代时大规模的回归测试. ThreadingTest(简称"TT")是 ...

最新文章

  1. sci face 补全
  2. nuxt服务端php,nuxt服务端部署指南
  3. http://www.cnblogs.com/Bear-Study-Hard/archive/2008/03/26/1123267.html
  4. python3ide_Python IDE Windows下载3.4.2 安装版
  5. lua cocos 中对FNT字体的使用
  6. spring容器实例化bean的3种方式
  7. SAP Analytics Cloud里看到的SAP C4C的query列表,是从哪里取出来的
  8. Thinkphp5 还有这种操作?
  9. echart饼图标签重叠_怎么让ECharts饼图的label标签不重叠?
  10. java对mysql进行查找替换_Java对MySQL数据库进行连接、查询和修改【转载】
  11. ArcGIS 使用等高线和高程点数据生成DEM栅格数据
  12. 打开IE8总是提示欢迎使用?怎样使它不提示?
  13. Laravel自学第一课:laravel下载与安装
  14. Android 一键加速原理
  15. 3DMAX解决Vray渲染材质溢色问题的三种方法
  16. 【RDMA】RDMA编程实例(IBV Verbs )
  17. 单反相机的传奇—佳能单反50年辉煌之路(连载十四)
  18. C/C++编程刷题分享—常见的经典面试题一
  19. 全媒体运营师胡耀文教你:从0到1搭建直播运营体系
  20. linux/android驱动工程师面试相关内容总结

热门文章

  1. 几组数据的相关性python_Python 数据相关性分析
  2. Silverlight案例之卷轴动画和Tag树
  3. 牛客网错题集---机器学习基础篇
  4. Mybatis 分页插件 Pagehelper 的 PageInfo 字段属性解释
  5. html与css3入门经典 周靖,HTML5与CSS3从入门到精通
  6. FPGA之VGA/LCD数字时钟显示
  7. 山西人在北京IT交流群
  8. git pull 时候报错Your configuration specifies to merge with the ref ‘refs/heads/master‘ from the remote,
  9. pipelines sdk简介
  10. vue-判断设备是手机端还是pc端