美文网首页
【Go】单元测试

【Go】单元测试

作者: 如雨随行2020 | 来源:发表于2022-11-19 16:47 被阅读0次

基本用法

  • 测试文件名以_test结尾
  • 函数名以Test开始

待测试代码

// split.go
package split

import "strings"

func Split(str string, sep string) []string {
    var ret []string
    index := strings.Index(str, sep)
    for index >= 0 {
        ret = append(ret, str[:index])
        str = str[index+len(sep):]
        index  = strings.Index(str, sep)
    }
    ret = append(ret, str)
    return ret
}

测试代码

// split_test.go
package split

import (
    "reflect"
    "testing"
)

func TestSplit(t *testing.T) {
    got := Split("a:b:c", ":")
    want := []string{"a", "b", "c"}
    if !reflect.DeepEqual(got, want) {
        t.Errorf("expected: %v, got: %v\n", want, got)
    }
}

到所在目录执行

go test -v

测试组

优化多个测试用例的代码

func TestSplitByGroup(t *testing.T) {
    type testCase struct {
        str string
        sep string
        want []string
    }

    testGroup := []testCase {
        {"babcbef", "b", []string{"", "a", "c", "ef"}},
        { "a:b:c", ":", []string{"a", "b", "c"}},
        {"abcef", "bc", []string{"a", "ef"}},
    }

    for _, tg := range testGroup {
        got := Split(tg.str, tg.sep)
        if !reflect.DeepEqual(got, tg.want) {
            t.Errorf("expected: %v, got: %v\n", tg.want, got)
        }
    }
}

子测试

用于区分测试组中,具体执行了哪个测试用例

func TestSplit3(t *testing.T) {
    type testCase struct {
        str  string
        sep  string
        want []string
    }

    testGroup := map[string]testCase{
        "case1": {str: "babcbef", sep: "b", want: []string{"", "a", "c", "ef"}},
        "case2": {str: "a:b:c", sep: ":", want: []string{"a", "b", "c"}},
        "case3": {str: "abcef", sep: "bc", want: []string{"a", "ef1"}},
    }

    for name, tg := range testGroup {
        t.Run(name, func(t *testing.T) {
            got := Split(tg.str, tg.sep)
            if !reflect.DeepEqual(got, tg.want) {
                t.Errorf("expected: %#v, got: %#v\n", tg.want, got)
            }
        })
    }
}

测试结果可以看到是case3结果出错了

image-20221119000527171

如果想跑某一个测试用例

go test -run=Split3/case1
go test -run=Split
// 如果run参数错了,找不到测试用例,会warning
// testing: warning: no tests to run

测试覆盖率

  • 函数覆盖率100%
  • 代码覆盖着60%

简单使用

go test -cover
image-20221119001017029

使用工具

go test -cover -coverprofile=cover.out
go tool cover -html=cover.out
image-20221119001337342

基准测试

  • 函数名以Benchmark开始
  • 必须执行b.N
func BenchmarkSplit(b *testing.B) {
    for i := 0; i < b.N; i++ {
        Split("a:b:c", ":")
    }
}

运行

go test -bench=Split
go test -bench .
image-20221119141057822

看内存信息

go test -bench=Split -benchmem
image-20221119141221966

每次操作使用了三次内存分配

还可以重置时间

b.ResetTimer()

Test Main

当测试文件中有TestMain函数,执行go test就会会调用TestMain,否则会创建一个默认的TestMain;我们自定义TestMain时,需要手动调用m.Run()否则测试函数不会执行

func TestMain(m *testing.M) {
    fmt.Println("before code...")
    retCode := m.Run()
    fmt.Println("retCode", retCode)
    fmt.Println("after code...")
    os.Exit(retCode)
}

相关文章

  • go 单元测试

    单元测试 Go 语言测试框架可以让我们很容易地进行单元测试,但是需要遵循五点规则: 含有单元测试代码的 go 文件...

  • golang 单元测试(gotests、mockery自动生成)

    golang 单元测试 文件格式:go单元测试,有固定的名称格式,所有以_test.go为后缀名的源文件在执行go...

  • go test 单元测试

    go test 单元测试 文件格式:go单元测试,有固定的名称格式,所有以_test.go为后缀名的源文件在执行g...

  • gotests

    Go单元测试 go 程序中一般使用官方的go test 做测试,面对一些复杂情况和紧急需求写单元测试就变得有些仓促...

  • Go单元测试框架简单使用

    约束: 使用go自身的单元测试框架testing包来写单元测试有如下约束: 单元测试,要导入 testing 包;...

  • gotests 单元测试 快速生成

    gotests 单元测试 快速生成 go test单元测试简介 gotests安装 源码地址:https://gi...

  • Go单元测试(一):基本用法

    来自公众号:灰子学技术 原文链接 一、单元测试的基本规则介绍 Go的单元测试比较容易实现,因为Go语言为我们提供了...

  • Go 语言 Unit Testing 单元测试

    关于 Go 的基本语法,参见:半天时间 Go 语言的基本实践 单元测试 Go 中提供了 testing 这个 pa...

  • 单元测试&基准测试&样本测试&测试覆盖率

    1.单元测试 1.1.go test 目录 1.2.go test 测试源码文件 测试的源码文件 1.3.go t...

  • go 用testify搭建完整易用的测试环境

    单元测试 单元测试在大型应用开发中是非常重要的一环。go 自身提供了单元测试框架,但是原生单元测试框架提供的功能太...

网友评论

      本文标题:【Go】单元测试

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