go语言单元

本文介绍了Go语言的单元测试,包括使用内置的testing包、Goconvey库以及GoStub进行测试和打桩方法。通过示例展示了如何创建测试用例、断言以及自定义诊断,并提供了对函数和库函数打桩的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 用到的组件
    Testing
    Goconvey
    GoStub

  2. 自带的testing
    go test会自动执行xxx_test.go中的Testxxx的函数
    如a_test.go中的
    func Testxxx(t *testing.T) {
    }

  3. Goconvey
    用Convey来创建测试用例
    如Convey(“Test xxxx function”, t, func() {

    })
    在Convey中使用So来做诊断
    如So(var, ShouldEqual, 1)

  4. 标准的assertion
    So(thing1, ShouldEqual, thing2)
    So(thing1, ShouldNotEqual, thing2)
    So(thing1, ShouldResemble, thing2) // a deep equals for arrays, slices, maps, and structs
    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,

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

参考https://github.com/smartystreets/goconvey/wiki/Assertions

  1. 自定义诊断
    func should(actual interface{}, expected …interface{}) string {
    if <some-important-condition-is-met(actual, expected)> {
    return “” // empty string means the assertion passed
    } else {
    return “

Convey(“All caps always makes text more meaningful”, func() {
So(“BOO!”, shouldScareGophersMoreThan, “boo”)
})

  1. 打桩方法
    使用gostub打桩
    对于变量直接使用
    xxxstub := Stub (&xxx, yyy)
    defer xxxstub.Reset()

    对于函数的打桩
    var Exec = func(xxx,xxx)(string, error) {
    }
    execstub := Stub(&Exec, func())(string, error) {})
    execstub.Reset()

    对于库函数的stub
    var Stat = os.Stat
    然后stub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值