GoConvey_初步认识

来源:互联网 发布:java drawimage 厘米 编辑:程序博客网 时间:2024/06/07 00:31

GoConvey 是一款针对Go语言的testing框架 编写文件保存自动运行testcase,web ui实时显示结果

先展示下效果图


Very Cool

此处是Github地址

https://github.com/smartystreets/goconvey

安装步骤就不介绍了

GoConvey 有两个比较重要的方法一个是 Convey  和 So

Convey用来定义一些scope/context/behavior/ideas

so用来进行断言

以下是例子

//该测试方法的测试结果都包含在上传的图片中func Test3(t *testing.T) {//第一个参数可以理解为Convey的名字用于在web ui中查看//必须要降testing.T传入到Convey中//第三个参数则是需要执行测试的函数Convey("3 shouldEqual 3", t, func() {//使用So来进行断言//该断言代表3和3是否相等So(3, ShouldEqual, 3)})Convey("4 shouldEqual 4", t, func() {So(4, ShouldEqual, 4)})Convey("5 ShouldNotEqual 5", t, func() {So(5, ShouldNotEqual, 6)})Convey("ShouldHappenBefore", t, func() {//该断言代表第一个时间点是否早于第二个时间点 如果是返回passSo(time.Now(), ShouldHappenBefore, time.Now())})Convey("ShouldHappenOnOrBefore", t, func() {ti := time.Now()//该断言代表第一个参数时间早于或等于第二个时间参数 如果是则passSo(ti, ShouldHappenOnOrBefore, ti)})Convey("ShouldNotContain", t, func() {//该断言判断 第一个参数不包含第三个参数 如果是passSo([]int{2, 4, 6}, ShouldNotContain, 5)})//这个是用来表明暂时还没有实现的测试方法 在webUI中有不一样的图标显示 Convey("This isn't yet implemented", t, nil)}

GoConvey也支持自己定义一个断言函数 So的函数原型如下

func So(actual interface{}, assert assertion, expected ...interface{}) 
可以看到第二个参数是assertion 查看下assertion

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

可以看出只要传入一个符合assertion格式的函数就可以了 我自己写了个断言函数如下

func Test4(t *testing.T) {Convey("BrainWu is handsome!!!", t, func() {So("BrainWu", ShouldBrainWuHandsome, "Handsome")})}//符合assertion类型规范的函数 如果返回字符串为空值 那么将会被pass//如果有内容 那么将不通过 且返回内容会被输出func ShouldBrainWuHandsome(actual interface{}, expected ...interface{}) string {if actual == "BrainWu" && expected[0] == "Handsome" {return ""//pass} else {return "BrainWu is handsome!!!"//not pass and output the return value}}

有的时候我们想要忽略某些断言操作但又不想删掉 GoConvey提过了一种方法可以不去断言

一种是忽略Convey一种是忽略So

示例如下

func Test5(t *testing.T) {SkipConvey("Important stuff", t, func() {          // This func() will not be executed!Convey("More important stuff", func() {So("asdf", ShouldEqual, "asdf")})})Convey("1 Should Equal 2", t, func() {// This assertion will not be executed and will show up as 'skipped' in the reportSkipSo(1, ShouldEqual, 2)})}

0 0
原创粉丝点击