goconvey测试框架的使用

尽管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可以使测试代码更加清晰、可读性更强,提高测试的覆盖率和质量,帮助开发者更快速地发现和解决问题。

  • 17
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用一些工具来自动生成测试脚本,比如GoConvey和Testify等。这些工具可以帮助你编写和运行测试,并生成测试报告。 使用GoConvey,你可以在项目中编写测试代码,并通过浏览器查看测试结果。首先,你需要在项目中安装GoConvey: ``` go get github.com/smartystreets/goconvey ``` 然后,在你的测试文件中导入GoConvey包,并使用`convey`函数创建一个测试套件: ```go package main_test import ( "testing" . "github.com/smartystreets/goconvey/convey" ) func TestMath(t *testing.T) { Convey("Given two numbers", t, func() { a := 2 b := 3 Convey("When adding them together", func() { sum := a + b Convey("The result should be correct", func() { So(sum, ShouldEqual, 5) }) }) }) } ``` 运行测试脚本: ``` goconvey ``` 这将启动一个Web界面,你可以在浏览器中查看测试结果。 另外一个流行的测试工具是Testify。你可以使用Testify编写更传统的单元测试,并生成测试报告。首先,你需要在项目中安装Testify: ``` go get github.com/stretchr/testify ``` 然后,你可以在你的测试文件中导入Testify包,并编写测试代码: ```go package main_test import ( "testing" "github.com/stretchr/testify/assert" ) func TestMath(t *testing.T) { a := 2 b := 3 sum := a + b assert.Equal(t, 5, sum) } ``` 运行测试脚本: ``` go test ``` 这将执行你的测试并生成测试报告。 这些工具都可以帮助你自动生成测试脚本,并且提供了丰富的断言函数和测试辅助工具,使得编写和运行测试更加方便。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值