• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Recommended Posts

Go单测—goconvey的使用

本文介绍了如何使用goconvey更好地编写单元测试,使单元测试的结果更加直观、生动。在本文中,我们将介绍一个人性化的单元测试工具——goconvey。源代码

一、GoConvey介绍

GoConvey是一个非常非常好用的go测试框架。它直接与Go Test集成,提供许多丰富的断言功能,可以在终端输出可读的颜色测试结果,还支持全自动Web UI。

二、安装

去找github.com/smartystreets/goconvey

三、使用示例

我们使用goconvey为最初的基本示例中的Split函数编写单元测试。拆分功能如下:

//split.go

包goconvey_demo

导入“字符串”

/*

@作者RandySun

@create 2022-05-01-19:19

*/

//

//拆分

//@Description:根据给定的分隔符sep分割字符串S并返回字符串片

//@param s

//@参数sep

//@返回结果

//

func Split(s,sep字符串)(result []string) {

结果=make([]字符串,0,字符串。计数(月)1)

i :=字符串。指数,9月

对于i -1 {

result=append(结果,s[:i])

S=s[i len(sep):] //使用len(sep)得到sep的长度。

i=字符串。指数,9月

}

result=append(结果,s)

返回

}

单元测试文件的内容如下:

//split_test.go

导入(

'测试'

' c ' github.com/smartystreets/goconvey/convey'//alias进口

)

func test split(t *测试。T) {

C.传达('基本用例',t,func() {

风险值(

s='a:b:c '

sep=': '

expect=[]string{'a ',' b ',' c'}

)

得到:=拆分(s,sep)

C.so (got,c .应重分发,expect)//断言

})

C.Convey('没有分隔符的用例',t,func() {

风险值(

s='a:b:c '

sep='| '

expect=[]string { ' a : b : c ' }

)

得到:=拆分(s,sep)

C.so (got,c .应重分发,expect)//断言

})

}

执行命令行单元测试将在终端输出非常易读的颜色结果。

goconvey_demo go测试-v

===运行测试拆分

基本用例。

1总断言

没有分隔符的用例。

2个总断言

- PASS:测试分割(0.01秒)

及格

ok golang-unit-test-example/09go convey _ demo 0.243s

image-20220501200417110

Goconvey还根据需要支持单元测试中的嵌套调用,比如:

//

//TestChildrenSplit

//@Description:嵌套调用

//@param

t // func TestChildrenSplit(t *testing.T) { // 只需要在顶层的Convey调用时传入t c.Convey("分隔符在开头或者结尾用例", t, func() { tt := []struct { name string s string sep string expect []string }{ {"分隔符在开头", "1*2*3", "*", []string{"", "1", "2", "3"}}, {"分隔符在结尾", "1+2+3+", "+", []string{"1", "2", "3", ""}}, } for _, tc := range tt { c.Convey(tc.name, func() { // 嵌套调用Convery got := Split(tc.s, tc.sep) c.So(got, c.ShouldResemble, tc.expect) }) } }) }

这样输出最终的测试结果时也会分层级显示。

goconvey_demo> go test -v -run Ch
=== RUN   TestChildrenSplit
  分隔符在开头或者结尾用例
    分隔符在开头 .
    分隔符在结尾 .
2 total assertions
--- PASS: TestChildrenSplit (0.00s)
PASS
ok      golang-unit-test-example/09goconvey_demo        0.303s

image-20220501201640794

四、断言方法

GoConvey为我们提供了很多种类断言方法在So()函数中使用。

一般相等类
So(thing1, ShouldEqual, thing2)
So(thing1, ShouldNotEqual, thing2)
So(thing1, ShouldResemble, thing2)		// 用于数组、切片、map和结构体相等
So(thing1, ShouldNotResemble, thing2)
So(thing1, ShouldPointTo, thing2)
So(thing1, ShouldNotPointTo, thing2)
So(thing1, ShouldBeNil)
So(thing1, ShouldNotBeNil)
So(thing1, ShouldBeTrue)
So(thing1, ShouldBeFalse)
So(thing1, ShouldBeZeroValue)
数字数量比较类
So(1, ShouldBeGreaterThan, 0)
So(1, ShouldBeGreaterThanOrEqualTo, 0)
So(1, 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)
包含类
So([]int{2, 4, 6}, ShouldContain, 4)
So([]int{2, 4, 6}, ShouldNotContain, 5)
So(4, ShouldBeIn, ...[]int{2, 4, 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
字符串类
So("asdf", ShouldStartWith, "as")
So("asdf", ShouldNotStartWith, "df")
So("asdf", ShouldEndWith, "df")
So("asdf", ShouldNotEndWith, "df")
So("asdf", ShouldContainSubstring, "稍等一下")		// 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")
类型检查类
So(1, ShouldHaveSameTypeAs, 0)
So(1, ShouldNotHaveSameTypeAs, "asdf")
时间和时间间隔类
So(time.Now(), ShouldHappenBefore, time.Now())
So(time.Now(), ShouldHappenOnOrBefore, time.Now())
So(time.Now(), ShouldHappenAfter, time.Now())
So(time.Now(), ShouldHappenOnOrAfter, time.Now())
So(time.Now(), ShouldHappenBetween, time.Now(), time.Now())
So(time.Now(), ShouldHappenOnOrBetween, time.Now(), time.Now())
So(time.Now(), ShouldNotHappenOnOrBetween, time.Now(), time.Now())
So(time.Now(), ShouldHappenWithin, duration, time.Now())
So(time.Now(), ShouldNotHappenWithin, duration, time.Now())
自定义断言方法

如果上面列出来的断言方法都不能满足你的需要,那么你还可以按照下面的格式自定义一个断言方法。

注意:<>中的内容是你需要按照实际需求替换的内容。

func should<do-something>(actual interface{}, expected ...interface{}) string {
    if <some-important-condition-is-met(actual, expected)> {
        return ""   // 返回空字符串表示断言通过
    }
    return "<一些描述性消息详细说明断言失败的原因...>"
}

五、WebUI

goconvey提供全自动的WebUI,只需要在项目目录下执行以下命令。

goconvey

默认就会在本机的8080端口提供WebUI界面,十分清晰地展现当前项目的单元测试数据。

image-20220501202116246

image-20220501202315903

六、总结

本文通过一个完整的单元测试示例,介绍了如何使用goconvey工具编写测试用例、管理测试用例、断言测试结果,同时也介绍了goconvey丰富多样的测试结果输出形式。

Link to comment
Share on other sites