加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

GoConvey断言err和bool的方法

发布时间:2020-12-16 18:36:27 所属栏目:大数据 来源:网络整理
导读:最近在写单元测试的时候GoConvey.So()断言err是否nil和bool是否为true的时候不知道用哪个,于是找了这份官方文档,以后也可以参考。 GoConvey comes with a lot of standard assertions you can use with So(). General Equality So(thing1,ShouldEqual,th

最近在写单元测试的时候GoConvey.So()断言err是否nil和bool是否为true的时候不知道用哪个,于是找了这份官方文档,以后也可以参考。
GoConvey comes with a lot of standard assertions you can use with So().
General Equality

So(thing1,ShouldEqual,thing2)
So(thing1,ShouldNotEqual,ShouldResemble,thing2) // a deep equals for arrays,slices,maps,and structs
So(thing1,ShouldNotResemble,ShouldPointTo,ShouldNotPointTo,ShouldBeNil)
So(thing1,ShouldNotBeNil)
So(thing1,ShouldBeTrue)
So(thing1,ShouldBeFalse)
So(thing1,ShouldBeZeroValue)

Numeric Quantity comparison

So(1,ShouldBeGreaterThan,0)
So(1,ShouldBeGreaterThanOrEqualTo,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) // tolerance is optional; default 0.0000000001
So(1.0,ShouldNotAlmostEqual,0.9,.0001)

Collections

So([]int{2,4,6},ShouldContain,4)
So([]int{2,ShouldNotContain,5)
So(4,ShouldBeIn,…[]int{2,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) // supports map,slice,chan,and string

Strings

So(“asdf”,ShouldStartWith,“as”)
So(“asdf”,ShouldNotStartWith,“df”)
So(“asdf”,ShouldEndWith,ShouldNotEndWith,ShouldContainSubstring,“sd”) // optional ‘expected occurences’ arguments?
So(“asdf”,ShouldNotContainSubstring,“er”)
So(“adsf”,ShouldBeBlank)
So(“asdf”,ShouldNotBeBlank)

panic

So(func(),ShouldPanic)
So(func(),ShouldNotPanic)
So(func(),ShouldPanicWith,“”) // or errors.New(“something”)
So(func(),ShouldNotPanicWith,“”) // or errors.New(“something”)

Type checking

So(1,ShouldHaveSameTypeAs,ShouldNotHaveSameTypeAs,“asdf”)

time.Time (and time.Duration)

So(time.Now(),ShouldHappenBefore,time.Now())
So(time.Now(),ShouldHappenOnOrBefore,ShouldHappenAfter,ShouldHappenOnOrAfter,ShouldHappenBetween,time.Now(),ShouldHappenOnOrBetween,ShouldNotHappenOnOrBetween,ShouldHappenWithin,duration,ShouldNotHappenWithin,time.Now())

Thanks to github.com/jacobsa for his excellent oglematchers library,which is what many of these methods make use of to do their jobs.
Next

Next up,learn about building your own assertions.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读