美文网首页
goconvey 单元测试

goconvey 单元测试

作者: Hmcf | 来源:发表于2020-12-14 11:36 被阅读0次
    package main
    
    import (
        . "github.com/smartystreets/goconvey/convey"
        "testing"
    )
    
    func StringSliceEqual(a, b []string) bool {
        if len(a) != len(b) {
            return false
        }
    
        if (a == nil) != (b == nil) {
            return false
        }
    
        for i, v := range a {
            if v != b[i] {
                return false
            }
        }
        return true
    }
    
    
    // 每一个Convey语句对应一个测试用例,那么一个函数的多个测试用例可以通过一个测试函数的多个Convey语句来呈现
    func TestStringSliceEqual(t *testing.T) {
        Convey("TestStringSliceEqual should return true when a != nil  && b != nil", t, func() {
            a := []string{"hello", "goconvey"}
            b := []string{"hello", "goconvey"}
            So(StringSliceEqual(a, b), ShouldBeTrue)
        })
    
        Convey("TestStringSliceEqual should return true when a == nil  && b == nil", t, func() {
            So(StringSliceEqual(nil, nil), ShouldBeTrue)
        })
    
        Convey("TestStringSliceEqual should return false when a == nil  && b != nil", t, func() {
            a := []string(nil)
            b := []string{}
            So(StringSliceEqual(a, b), ShouldBeFalse)
        })
    
        Convey("TestStringSliceEqual should return false when a != nil  && b != nil", t, func() {
            a := []string{"hello", "world"}
            b := []string{"hello", "goconvey"}
            So(StringSliceEqual(a, b), ShouldBeFalse)
        })
    }
    
    // Convey语句可以无限嵌套,以体现测试用例之间的关系。需要注意的是,只有最外层的Convey需要传入*testing.T类型的变量t。
    // 推荐使用这种以测试函数为单位多个测试用例集中显示的形式
    func TestStringSliceEqual1(t *testing.T) {
        Convey("TestStringSliceEqual", t, func() {
            Convey("should return true when a != nil  && b != nil", func() {
                a := []string{"hello", "goconvey"}
                b := []string{"hello", "goconvey"}
                So(StringSliceEqual(a, b), ShouldBeTrue)
            })
    
            Convey("should return true when a == nil  && b == nil", func() {
                So(StringSliceEqual(nil, nil), ShouldBeTrue)
            })
    
            Convey("should return false when a == nil  && b != nil", func() {
                a := []string(nil)
                b := []string{}
                So(StringSliceEqual(a, b), ShouldBeFalse)
            })
    
            Convey("should return false when a != nil  && b != nil", func() {
                a := []string{"hello", "world"}
                b := []string{"hello", "goconvey"}
                So(StringSliceEqual(a, b), ShouldBeFalse)
            })
        })
    }
    

    测试

    ➜  gocs go test -v cf_test.go -run TestStringSliceEqual1
    === RUN   TestStringSliceEqual1
    
      TestStringSliceEqual 
        should return true when a != nil  && b != nil ✔
        should return true when a == nil  && b == nil ✔
        should return false when a == nil  && b != nil ✔
        should return false when a != nil  && b != nil ✔
    
    
    4 total assertions
    
    --- PASS: TestStringSliceEqual1 (0.00s)
    PASS
    ok      command-line-arguments  0.083s
    ➜  gocs 
    

    还有一些其他的断言,有需要的可以看对应文档

    image.png

    相关文章

      网友评论

          本文标题:goconvey 单元测试

          本文链接:https://www.haomeiwen.com/subject/igcvgktx.html