• GoConvey使用与总结


    GoConvey是一款针对Golang的测试框架,可以管理和运行测试用例,同时提供了丰富的断言函数,并支持很多 Web 界面特性。
    Golang虽然自带了单元测试功能,并且在GoConvey框架诞生之前也出现了许多第三方测试框架,但没有一个测试框架像GoConvey一样能够让程序员如此简洁优雅的编写测试代码

    总结:

    1. import goconvey包时,前面加点号".",以减少冗余的代码。凡是在测试代码中看到Convey和So两个方法,肯定是convey包的,不要在产品代码中定义相同的函数名
    2. 测试函数的名字必须以Test开头,而且参数类型必须为*testing.T
    3. Convey(string,testing.T,func{
         Convey(string,func{})
         Convey(string,func{})
         So(实际结果,断言函数变量,预测的结果)
      })
    4. 每个测试用例必须使用Convey函数包裹起来,它的第一个参数为string类型的测试描述,第二个参数为测试函数的入参(类型为*testing.T),第三个参数为不接收任何参数也不返回任何值的函数(习惯使用闭包)
    5. Convey函数的第三个参数闭包的实现中通过So函数完成断言判断,它的第一个参数为实际值,第二个参数为断言函数变量,第三个参数或者没有(当第二个参数为类ShouldBeTrue形式的函数变量)或者有(当第二个函数为类ShouldEqual形式的函数变量)
    import (
        "testing"
        . "github.com/smartystreets/goconvey/convey"
    )
    
    func TestStringSliceEqual(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)
            })
        })
    }
    

      

    重点关注So断言的使用和Convey嵌套使用。

  • 相关阅读:
    Java进阶之并发初探
    Java进阶之HashMap剖析
    Java进阶之反射
    Linux常用命令
    海量数据处理算法与面试题
    一些刷题时总结的重要知识点
    一些Java刷题时的小知识点
    九章算法知识点与题目总结(不定时更新...)
    c++设计模式之状态模式
    c++设计模式之抽象工厂模式
  • 原文地址:https://www.cnblogs.com/richxan/p/15942556.html
Copyright © 2020-2023  润新知