尽管Golang已经内置了功能强大的testing
包,其易用性令人称赞。然而,当我们希望更直观地处理和判断测试结果时,结合使用goconvey
能为我们提供极大的便利。goconvey
不仅为我们提供了丰富的断言函数,这些函数还极大地方便了我们在进行单元测试时对预期结果的验证。通过这些断言函数,我们可以更加精准地定义和验证代码的行为,从而确保软件的质量和稳定性。因此,充分利用goconvey
的断言功能,对于提升单元测试的效果和效率具有重要意义。
接下来,让我们使用起来吧,看看它的便捷性
一、安装goconvey包
go get github.com/smartystreets/goconvey/convey@v1.8.1
二、导入goconvey包
导入时注意一个细节:包前面加上“.”,方便我们在使用时不用再敲入包名
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
三、使用
Convey(string,*testing.T,func{
Convey(string,func{})
Convey(string,func{})
So(实际结果,断言函数变量,预测的结果)
})
goconvey
库提供了Convey
函数,该函数是组织测试用例的关键。每个测试用例都需要使用Convey
函数进行包裹。Convey
函数的第一个参数是一个描述性的字符串,用于说明该测试的目的或场景。第二个参数是*testing.T
类型的测试函数入参,它提供了测试框架所需的基本功能。第三个参数是一个不接受任何参数且没有返回值的函数(通常使用闭包形式),该函数内部包含了具体的测试逻辑,通过So函数完成断言判断。
例子1:加法结果判断
func Add(a, b int) int {
return a + b
}
func TestAdd(t *testing.T) {
Convey("Given two integers", t, func() {
Convey("When adding them together", func() {
result := Add(2, 3)
Convey("The result should be correct", func() {
So(result, ShouldEqual, 5)
})
})
})
}
例子2:判断两个slice是否相等
func TestStringSliceEqual(t *testing.T) {
Convey("TestStringSliceEqual", t, func() {
Convey("should return true when a!=nil and b!=nil", func() {
a := []string{"hello", "goconvey"}
b := []string{"hello", "goconvey"}
So(StringSilceEqual(a, b), ShouldBeTrue)
})
Convey("should return false when a!=nil and b!=nil", func() {
a := []string{"hello", "goconvey"}
b := []string{"hello", "testing"}
So(StringSilceEqual(a, b), ShouldBeFalse)
})
Convey("should return false when a!=nil and b==nil", func() {
a := []string{"hello", "goconvey"}
So(StringSilceEqual(a, nil), ShouldBeFalse)
})
Convey("should return false when a==nil and b==nil", func() {
So(StringSilceEqual(nil, nil), ShouldBeFalse)
})
})
}
func StringSilceEqual(a []string, b []string) bool {
if a == nil || b == nil {
return false
}
return reflect.DeepEqual(a, b)
}
特别注意
如果一个测试用使用多个Convey,Convey的第一个参数不能相同,
否则会报如下错误:
--- FAIL: TestStringSliceEqual (0.00s)
panic: Multiple convey suites with identical names: "should return true when a!=nil and b!=nil" [recovered]
panic: Multiple convey suites with identical names: "should return true when a!=nil and b!=nil" [recovered]
panic: Multiple convey suites with identical names: "should return true when a!=nil and b!=nil"
断言函数枚举
ShouldAlmostEqual = assertions.ShouldAlmostEqual
ShouldBeBetween = assertions.ShouldBeBetween
ShouldBeBetweenOrEqual = assertions.ShouldBeBetweenOrEqual
ShouldBeBlank = assertions.ShouldBeBlank
ShouldBeChronological = assertions.ShouldBeChronological
ShouldBeEmpty = assertions.ShouldBeEmpty
ShouldBeError = assertions.ShouldBeError
ShouldBeFalse = assertions.ShouldBeFalse
ShouldBeGreaterThan = assertions.ShouldBeGreaterThan
ShouldBeGreaterThanOrEqualTo = assertions.ShouldBeGreaterThanOrEqualTo
ShouldBeIn = assertions.ShouldBeIn
ShouldBeLessThan = assertions.ShouldBeLessThan
ShouldBeLessThanOrEqualTo = assertions.ShouldBeLessThanOrEqualTo
ShouldBeNil = assertions.ShouldBeNil
ShouldBeTrue = assertions.ShouldBeTrue
ShouldBeZeroValue = assertions.ShouldBeZeroValue
ShouldContain = assertions.ShouldContain
ShouldContainKey = assertions.ShouldContainKey
ShouldContainSubstring = assertions.ShouldContainSubstring
ShouldEndWith = assertions.ShouldEndWith
ShouldEqual = assertions.ShouldEqual
ShouldEqualJSON = assertions.ShouldEqualJSON
ShouldEqualTrimSpace = assertions.ShouldEqualTrimSpace
ShouldEqualWithout = assertions.ShouldEqualWithout
ShouldHappenAfter = assertions.ShouldHappenAfter
ShouldHappenBefore = assertions.ShouldHappenBefore
ShouldHappenBetween = assertions.ShouldHappenBetween
ShouldHappenOnOrAfter = assertions.ShouldHappenOnOrAfter
ShouldHappenOnOrBefore = assertions.ShouldHappenOnOrBefore
ShouldHappenOnOrBetween = assertions.ShouldHappenOnOrBetween
ShouldHappenWithin = assertions.ShouldHappenWithin
ShouldHaveLength = assertions.ShouldHaveLength
ShouldHaveSameTypeAs = assertions.ShouldHaveSameTypeAs
ShouldImplement = assertions.ShouldImplement
ShouldNotAlmostEqual = assertions.ShouldNotAlmostEqual
ShouldNotBeBetween = assertions.ShouldNotBeBetween
ShouldNotBeBetweenOrEqual = assertions.ShouldNotBeBetweenOrEqual
ShouldNotBeBlank = assertions.ShouldNotBeBlank
ShouldNotBeChronological = assertions.ShouldNotBeChronological
ShouldNotBeEmpty = assertions.ShouldNotBeEmpty
ShouldNotBeIn = assertions.ShouldNotBeIn
ShouldNotBeNil = assertions.ShouldNotBeNil
ShouldNotBeZeroValue = assertions.ShouldNotBeZeroValue
ShouldNotContain = assertions.ShouldNotContain
ShouldNotContainKey = assertions.ShouldNotContainKey
ShouldNotContainSubstring = assertions.ShouldNotContainSubstring
ShouldNotEndWith = assertions.ShouldNotEndWith
ShouldNotEqual = assertions.ShouldNotEqual
ShouldNotHappenOnOrBetween = assertions.ShouldNotHappenOnOrBetween
ShouldNotHappenWithin = assertions.ShouldNotHappenWithin
ShouldNotHaveSameTypeAs = assertions.ShouldNotHaveSameTypeAs
ShouldNotImplement = assertions.ShouldNotImplement
ShouldNotPanic = assertions.ShouldNotPanic
ShouldNotPanicWith = assertions.ShouldNotPanicWith
ShouldNotPointTo = assertions.ShouldNotPointTo
ShouldNotResemble = assertions.ShouldNotResemble
ShouldNotStartWith = assertions.ShouldNotStartWith
ShouldPanic = assertions.ShouldPanic
ShouldPanicWith = assertions.ShouldPanicWith
ShouldPointTo = assertions.ShouldPointTo
ShouldResemble = assertions.ShouldResemble
ShouldStartWith = assertions.ShouldStartWith
ShouldWrap = assertions.ShouldWrap
自定义断言函数
如果你觉得以上的断言都不能满足你的业务需求,那么可以自己定义断言函数
func shouldXXX(actual interface{}, expected ...interface{}) string {
if <some-important-condition-is-met(actual, expected)> {
return "" // 返回空字符串表示断言通过
}
return "<一些描述性消息详细说明断言失败的原因...>"
}
其实就是写一个函数来处理你的按断,返回空字符串表示断言通过,否则不通过给出原因。
四、WebUI
goconvey提供了WEB服务,可以通过WEB页面来执行单元测试和查看测试结果
如果要使用带WEB的goconvey需要安装goconvey包
go install github.com/smartystreets/goconvey@v1.8.1
cd到你要运行的测试包下执行 goconvey命令即可
cd /home/myPorject/goProject/test-all/
goconvey
你就会看到如下的页面
五、总结
Golang的testing包是Go语言自带的测试框架,提供了基本的测试功能,可以编写和运行单元测试、性能测试和示例测试等。而GoConvey是一个基于BDD(行为驱动开发)的测试框架,可以更加直观和易读地编写测试用例,并提供了更丰富的断言和报告功能。
结合Golang的testing包和GoConvey的好处包括:
1. 更直观的测试代码:GoConvey使用自然语言风格的描述和断言,使得测试用例更易读易懂,可以更清晰地表达测试的意图。
2. 自动化测试报告:GoConvey会生成可视化的测试报告,展示测试用例的执行结果和覆盖率等信息,帮助开发者更好地理解测试结果。
3. 实时测试监控:GoConvey可以实时监控代码的变化,并自动运行相关的测试用例,帮助开发者及时发现代码变更对测试的影响。
4. 更丰富的断言功能:GoConvey提供了丰富的断言函数,可以方便地进行各种测试断言,包括相等、包含、大于等比较操作。
总的来说,结合Golang的testing包和GoConvey可以使测试代码更加清晰、可读性更强,提高测试的覆盖率和质量,帮助开发者更快速地发现和解决问题。