DEV Community

林子篆
林子篆

Posted on • Originally published at dannypsnl.github.io on

Testing in Go

Just list some testing way in Go.

Basically, we use testing this built-in lib to testing

To start your first test with Go is an easy task.

Example(we would test the following function under directory add)

package add

func Add(x, y int) int { return x + y }
Enter fullscreen mode Exit fullscreen mode
  1. create a file contains suffix _test, it would be a test file, e.g. add_test.go
  2. create a function in test file has prefix Test, use t *testing.T as it’s parameter
package add

import "testing"
 func TestAdd(t *testing.T) {
     if Add(1, 2) != 3 {
         t.Errorf("Add(1, 2) should be 3 but: %d", Add(1, 2))
     }
 }
Enter fullscreen mode Exit fullscreen mode
  1. type go test & execute it in terminal

Ok, now we got a test, if you see the error message then must something wrong about your implementation of Add

p.s. Usually, we won’t use go test but go test ./... because we would have a lot of package under a project, ./... would find out every sub directory(those can be a go package) & run test

We have func (*testing.T) Run(subTestName string, subTest func(t *testing.T)) this function, we can use it to create a new sub test.

func TestCarFactory(t *testing.T) {
    factory := car.NewFactory()
    t.Run("Toyota", func(t *testing.T) {
        toyota := factory.Build(car.Toyota)
        // test toyota
    })
    t.Run("Mazda", func(t *testing.T) {
        mazda := factory.Build(car.Mazda)
        // test mazda
    })
}
Enter fullscreen mode Exit fullscreen mode

Basically, you can see sub test means we want to reuse the same context for different tests, or like me, just use it represents the test structure.

A practical problem is sometimes we extract a test helper out of the test function.

For example:

func assertNoError(t *testing.T, err error) {
    if err != nil {
        t.Errorf("assert no error but: %s", err)
    }
}
Enter fullscreen mode Exit fullscreen mode

You will find all error say it happened at t.Errorf that line, but not the error actually happened place!

To solve this problem, you have to add t.Helper() this function call, according document:

Helper marks the calling function as a test helper function. When printing file and line information, that function will be skipped. Helper may be called simultaneously from multiple goroutines.

I recommend https://github.com/stretchr/testify for the assertion, Don’t Reinvent The Wheel! (Something I always violate it)

And https://github.com/gavv/httpexpect is an awesome lib for web API testing.

A nice fact is, Go also help you create benchmark easy.

Still in the test file, but use Benchmark as a prefix of a test.

package add

func BenchmarkAdd(b *testing.B) {
    for i := 0; i < b.N; i++ {
        Add(1, 2)
    }
}
Enter fullscreen mode Exit fullscreen mode

To run the benchmark needs argument -bench, it would like go test -bench .

Output:

goos: darwin
goarch: amd64
pkg: test
BenchmarkAdd-4 2000000000 0.61 ns/op
PASS
ok test 1.285s
Enter fullscreen mode Exit fullscreen mode

As t.Run, you can have b.Run in the benchmark.

To get a nice analysis of the program, you can use go test -bench . -cpuprofile cpu.out -memprofile mem.out to generate some profiles

Then use go tool pprof -http=127.0.0.1:5000 cpu.out to see the result on the browser(if you are familiar with CLI mode, you can remove -http flag)

You can see something like:

----------------------------------------------------------+-------------
                                            1130ms 100% | testing.(*B).launch /usr/local/Cellar/go/1.11.2/libexec/src/testing/benchmark.go:290
         0 0% 100% 1130ms 98.26% | testing.(*B).runN /usr/local/Cellar/go/1.11.2/libexec/src/testing/benchmark.go:141
                                            1130ms 100% | test.BenchmarkAdd /Users/dannypsnl/code/go/src/test/add_test.go:8
----------------------------------------------------------+-------------
Enter fullscreen mode Exit fullscreen mode

At here example is too easy so nothing to show, in a real-world code it would be pretty useful to know the hot point of the program.

p.s. At profile example, -bench can’t be omitted, because we want something to run lots of time to detect it’s real performance.

If you want to get the performance under real usage, you can import pprof into the program:

import (
    _ "net/http/pprof"
)
Enter fullscreen mode Exit fullscreen mode

If your program is not an HTTP server, then you have to start one like:

go func() {
    log.Println(http.ListenAndServe("0.0.0.0:6060", nil))
}()
Enter fullscreen mode Exit fullscreen mode

The reason of 0.0.0.0 can reference to https://stackoverflow.com/questions/20778771/what-is-the-difference-between-0-0-0-0-127-0-0-1-and-localhost

After these, you can run your program up then see your profile like go tool pprof http://127.0.0.1:6060/debug/pprof/profile

To get more info, you can reference:

Thanks for reading

Top comments (4)

Collapse
 
chenge profile image
chenge

Seems need to write package name and put under src dir.

Collapse
 
dannypsnl profile image
林子篆

My fault, should write more complete info XD

Collapse
 
chenge profile image
chenge

Thanks for share, hope to see more awesome posts in Go from you.

Collapse
 
david_j_eddy profile image
David J Eddy

Thank you for this. I love testing and Golang; your examples are nice and simple to get started with.