前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >Go Common Test

Go Common Test

原创
作者头像
timerring
发布2025-02-13 11:17:34
发布2025-02-13 11:17:34
380
举报

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:

代码语言:go
复制
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:

代码语言:go
复制
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.

代码语言:go
复制
    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.

代码语言:go
复制
    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

代码语言:go
复制
// 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.

代码语言:go
复制
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:

代码语言:go
复制
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:

代码语言:go
复制
func ExampleName() {
    // No parameters and no return
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Unit Test
  • Benchmark test
    • Performance comparison
    • Parallel test
  • Example test
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档