Go   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Go单元测试大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

Go单元测试

简介

单元测试是go语言级别提供的完整功能,测试代码*_test.go命名,单元测试的case以Test开头,性能测试case以Benchmark开头,运行测试命令:go test <@R_197_9214@file_list>

例子

实现排列组合函数以及对应的单元测试和性能测试

创建工程目录结构

目录结构说明参见规范-项目

└── src
    └── hmath
        ├── hmath.go
        └── hmath_test.go

实现排列组合函数

// src/hmath/hmath.go

package hmath

func combination(m,n int) int {
    if n > m-n {
        n = m - n
    }

    c := 1
    for i := 0; i < n; i++ {
        c *= m - i
        c /= i + 1
    }

    return c
}

实现单元测试和性能测试

// src/hmath/hmath_test.go

package hmath

import (
    "math/rand"
    "tesTing"
)

// 单元测试
// 测试全局函数,以TestFunction命名
// 测试类成员函数,以TESTClass_Function命名
func TESTCombination(t *tesTing.T) {
    // 这里定义一个临时的结构体来存储测试case的参数以及期望的返回值
    for _,unit := range []struct {
        m        int
        n        int
        expected int
    }{
        {1,1},{4,1,4},2,6},3,4,{10,10},120},7,} {
        // 调用排列组合函数,与期望的结果比对,如果不一致输出错误
        if actually := combination(unit.m,unit.n); actually != unit.expected {
            t.Errorf("combination: [%v],actually: [%v]",unit,actually)
        }
    }
}

// 性能测试
func BenchmarkCombination(b *tesTing.b) {
    // b.N会根据函数的运行时间取一个合适的值
    for i := 0; i < b.N; i++ {
        combination(i+1,rand.Intn(i+1))
    }
}

// 并发测试
func BenchmarkCombinationParallel(b *tesTing.b) {
    // 测试一个对象或者函数在多线程的场景下面是否安全
    b.RunParallel(func(pb *tesTing.PB) {
        for pb.Next() {
            m := rand.Intn(100) + 1
            n := rand.Intn(m)
            combination(m,n)
        }
    })
}

运行单元测试和性能测试

export GOPATH=$(pwd)
go test src/hmath/*.go           # 单元测试
go test --cover src/hmath/*.go   # 单元测试覆盖率
go test -bench=. src/hmath/*.go  # 性能测试

上面命令的输出如下:

hatlonely@localhost: ~/hatlonely/github/tmp $ go test src/hmath/hmath*.go
ok      command-line-arguments  0.005s
hatlonely@localhost: ~/hatlonely/github/tmp $ go test --cover src/hmath/hmath*.go
ok      command-line-arguments  0.005s  coverage: 100.0% of statements
hatlonely@localhost: ~/hatlonely/github/tmp $ go test -bench=. src/hmath/*.go
BenchmarkCombination-8                100000        217618 ns/op
BenchmarkCombinationParallel-8       3000000           401 ns/op
PASS
ok      command-line-arguments  23.599s

大佬总结

以上是大佬教程为你收集整理的Go单元测试全部内容,希望文章能够帮你解决Go单元测试所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:单元测试