Go Common Test

简介: The test methods in Go mainly include three types: unit test, benchmark test, and example test.

This article is also posted on my blog, feel free to check the latest revision: Go Common Test

The test methods in Go mainly include three types: unit test, benchmark test, and example test.

First of all:

  1. You should import the testing package first.
  2. In the package directory, all source code files with the suffix *_test.go are part of the go test test, and will not be compiled into the final executable file by go build.

Unit Test

The basic format/signature is:

func TestXxx(t *testing.T) {
   
    // ...
}

Put the _test.go and .go in the same dir, and run go test -v to execute the test detailed, the go test -cover to check the coverage of the test.

Benchmark test

The benchmark test will not be excuted by default, you need to use go test -bench=Xxx to execute the benchmark test.

The basic format/signature is:

func BenchmarkXxx(b *testing.B) {
   
    for i := 0; i < b.N; i++ {
   
        // ...
    }
}

The b.N is the number of times the test is run, and the b.N is automatically adjusted by the go test tool.

    BenchmarkSplit-8        10000000               215 ns/op
  • The -8 means the test is run on the 8 GOMAXPROCS
  • The 10000000 means the test is run 10000000 times.
  • The 215 ns/op means the test takes 215 nanoseconds average per operation.

go test -bench=Xxx -benchmem can check the memory allocation and the number of allocations per operation.

    BenchmarkSplit-8        10000000               215 ns/op             112 B/op          3 allocs/op
  • The 112 B/op means the test allocates 112 bytes per operation.
  • The 3 allocs/op means the test allocates 3 times per operation.

Performance comparison

// note: the n is the parameter of Fib not the b.N, the b.N is the number of times the test is run. it will be automatically adjusted by the go test tool.
func benchmarkFib(b *testing.B, n int) {
   
    for i := 0; i < b.N; i++ {
   
        Fib(n)
    }
}

func BenchmarkFib1(b *testing.B)  {
    benchmarkFib(b, 1) }
func BenchmarkFib2(b *testing.B)  {
    benchmarkFib(b, 2) }
func BenchmarkFib3(b *testing.B)  {
    benchmarkFib(b, 3) }

go test -bench=. will execute the benchmark test for all functions with the prefix Benchmark.

func BenchmarkSplit(b *testing.B) {
   
    time.Sleep(5 * time.Second) // some time-consuming operations
    b.ResetTimer()              // reset the timer(ignore the time above)
    for i := 0; i < b.N; i++ {
   
        // some time-consuming operations
    }
}

// you can also use this method

func BenchmarkSplit(b *testing.B) {
   
    b.StopTimer()
    // some time-consuming operations
    b.StartTimer()
    for i := 0; i < b.N; i++ {
   
        // some time-consuming operations
    }
}

Parallel test

Signature:

func BenchmarkSplitParallel(b *testing.B) {
   
    // b.SetParallelism(1) // set the number of CPU to use
    b.RunParallel(func(pb *testing.PB) {
   
        for pb.Next() {
   
            // some time-consuming operations
        }
    })
}

Example test

Signature:

func ExampleName() {
   
    // No parameters and no return
}
目录
相关文章
|
10月前
|
测试技术 Go 开发者
掌握 go test 命令,写出可信赖的代码
掌握 go test 命令,写出可信赖的代码
187 0
|
测试技术 Go Cloud Native
Go test 单元测试用起来
单元测试,是指对软件中的最小可测试单元进行检查和验证
|
测试技术 API Go
|
Go
GO语言学习笔记(四) - 测试 go test
GO语言学习笔记(四) - 测试 go test 测试程序规则 测试程序文件必须以_test.go结尾规则是被测试文件的文件名+_test.go例:被测试文件是 bs.go,那么测试文件名应该是bs_test.
1150 0
|
关系型数据库 测试技术 Go
|
测试技术 Go 应用服务中间件
|
20天前
|
运维 监控 算法
监控局域网其他电脑:Go 语言迪杰斯特拉算法的高效应用
在信息化时代,监控局域网成为网络管理与安全防护的关键需求。本文探讨了迪杰斯特拉(Dijkstra)算法在监控局域网中的应用,通过计算最短路径优化数据传输和故障检测。文中提供了使用Go语言实现的代码例程,展示了如何高效地进行网络监控,确保局域网的稳定运行和数据安全。迪杰斯特拉算法能减少传输延迟和带宽消耗,及时发现并处理网络故障,适用于复杂网络环境下的管理和维护。
|
21天前
|
编译器 Go
揭秘 Go 语言中空结构体的强大用法
Go 语言中的空结构体 `struct{}` 不包含任何字段,不占用内存空间。它在实际编程中有多种典型用法:1) 结合 map 实现集合(set)类型;2) 与 channel 搭配用于信号通知;3) 申请超大容量的 Slice 和 Array 以节省内存;4) 作为接口实现时明确表示不关注值。此外,需要注意的是,空结构体作为字段时可能会因内存对齐原因占用额外空间。建议将空结构体放在外层结构体的第一个字段以优化内存使用。
|
26天前
|
存储 Go
Go 语言入门指南:切片
Golang中的切片(Slice)是基于数组的动态序列,支持变长操作。它由指针、长度和容量三部分组成,底层引用一个连续的数组片段。切片提供灵活的增减元素功能,语法形式为`[]T`,其中T为元素类型。相比固定长度的数组,切片更常用,允许动态调整大小,并且多个切片可以共享同一底层数组。通过内置的`make`函数可创建指定长度和容量的切片。需要注意的是,切片不能直接比较,只能与`nil`比较,且空切片的长度为0。
Go 语言入门指南:切片
|
1月前
|
算法 安全 Go
公司局域网管理系统里的 Go 语言 Bloom Filter 算法,太值得深挖了
本文探讨了如何利用 Go 语言中的 Bloom Filter 算法提升公司局域网管理系统的性能。Bloom Filter 是一种高效的空间节省型数据结构,适用于快速判断元素是否存在于集合中。文中通过具体代码示例展示了如何在 Go 中实现 Bloom Filter,并应用于局域网的 IP 访问控制,显著提高系统响应速度和安全性。随着网络规模扩大和技术进步,持续优化算法和结合其他安全技术将是企业维持网络竞争力的关键。
50 2
公司局域网管理系统里的 Go 语言 Bloom Filter 算法,太值得深挖了