美文网首页
Go 测试(五)

Go 测试(五)

作者: LL大L | 来源:发表于2018-06-21 10:19 被阅读18次

欢迎来我的博客

表格驱动测试

表格驱动测试的优势

  • 分离的测试数据和测试逻辑
  • 明确的出错信息
  • 可以部分失败
  • go 语言的语法使得我们更易实践表格驱动测试
func TestTriangle(t *testing.T) {

    tests := []struct{ a, b, c int} {

        {3, 4, 5},
        {5, 12, 13},
        {8, 15, 17},
        {12, 35, 0},
        {30000, 40000, 50000},
    }

    for _, tt := range tests {

        if actual := calcTriangle(tt.a, tt.b); actual != tt.c {
            t.Errorf("calcTriangle(%d, %d); " + "got %d; expected %d", tt.a, tt.b, actual, tt.c)
        }
    }
}

func calcTriangle(a, b int) int {

    var c int
    c = int(math.Sqrt(float64(a * a + b * b)))
    return c
}

benchmark 示例

这里我们不需要指定一个循环的循环次数,go 会帮我们做好的

func BenchmarkSubstr(b *testing.B) {

    s := "黑化肥挥发发灰会花飞灰化肥挥发发黑会飞花"
    ans := 8

    for i := 0; i < b.N; i++ {

        actual := lengthOfNonRepeatingSubStr(s)
        if actual != ans {

            b.Errorf("got %d for input  %s;"+"expected %d", actual, s, ans)
        }
    }
}

命令行测试常用指令

运行测试文件

go test .

查看代码覆盖率

go tool cover -html=c.out

运行benchmark

 go test -bench .

查看cpu占用,来优化代码

go test -bench . -cpuprofile cpu.out
go tool pprof cpu.out
> web

查看文档

godoc -http: 6060

相关文章

  • go 单元测试

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

  • Go 测试(五)

    欢迎来我的博客 表格驱动测试 表格驱动测试的优势 分离的测试数据和测试逻辑 明确的出错信息 可以部分失败 go 语...

  • go语言函数测试

    go语言测试方法 测试split 编写的测试函数 go 测试使用的命令

  • GO学习笔记(17) -测试

    测试工具 —go test go test命令用于对Go语言编写的程序进行测试。 测试规范 测试文件必须为"_te...

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

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

  • go语言测试框架

    go语言内置的测试框架能够完成基本的功能测试,基准测试,和样本测试。 测试框架 go语言测试单元以包为单位组织,包...

  • Golang压力测试

    Go Test工具 Go语言中的测试依赖go test命令。编写测试代码和编写普通的Go代码过程是类似的,并不需要...

  • Golang快速排序(分治-填充)

    go快速排序算法,Go没有while循环,使用for 测试

  • 用golang程序跑满cpu

    test.go hello.go 下图是测试的

  • 15. Go极简教程 编写测试

    Go拥有一个轻量级的测试框架,它由 go test 命令和 testing 包构成 hello.go 编写待测试的...

网友评论

      本文标题:Go 测试(五)

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