GoConvey_初步认识

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() {
			//该断言代表第一个时间点是否早于第二个时间点 如果是返回pass
			So(time.Now(), ShouldHappenBefore, time.Now())
		})

	Convey("ShouldHappenOnOrBefore", t, func() {
			ti := time.Now()
			//该断言代表第一个参数时间早于或等于第二个时间参数 如果是则pass
			So(ti, ShouldHappenOnOrBefore, ti)
		})
	Convey("ShouldNotContain", t, func() {
			//该断言判断 第一个参数不包含第三个参数 如果是pass
			So([]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 report
			SkipSo(1, ShouldEqual, 2)

		})
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值