Basic Go: Testing and benchmarking using the testing package

I have just getting started playing around using Go. I have been interested in Go for a while, but did not found the time to play with it before now. Using the Christmas calendars to solve each little puzzle using Go feels like a good start. Now I found the Go package testing. When I started reading about it it looks quite interesting. There is a strict convention that need to be follow, but when that convention is follow, the tests can be run by using go test command. Another thing that I found interesting is that the package also includes benchmarking tools.

The convention that need to be followed is that the files need to end with _test.go if they do not the go test command will not find the tests. That test file need to be in the same package as the functionality that should be tested. The naming of the test functions names need to be at the form of TestXxx. Xxx should be replaced with the name of the test and the first character should be upper case. For the benchmark, the file name is the same, but the function name should start with BenchmarkXxx.

The test below is a working test that are testing one function I have in my main file. If the return from my function is not what I expect, it is just to call t.Error("") and then the test will fail.

package main

import "testing"

var (
	palindomerTestCeses       map[int64]bool = make(map[int64]bool)
	hiddenPalindomerTestCeses map[int64]bool = make(map[int64]bool)
)

func init() {
	palindomerTestCeses[123] = false
	palindomerTestCeses[121] = true

	hiddenPalindomerTestCeses[38] = true
}

func TestIsPalindomer(t *testing.T) {
	for key, value := range palindomerTestCeses {
		isPalidrome := isPalidrome(key)
		if isPalidrome != value {
			t.Error("Decision is wrong", key, value)
		}
	}
}

Similar to the test function, the benchmark is quite similar. The difference is that the function that should be benchmarked should be called in a loop that loops through all the b.Nthat can be set using -bench flag. I have just started looking into it myself, but this is something that I want and need to be more familiar with.

package main

import "testing"

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

When running the simplest form of the benchmark command go test -bench . it executes the benchmark test and output a little bit info about the system that is running the benchmark and the time that the execution took. With this default it executed with N=1.

$ go test -bench .
659277075458904
goos: darwin
goarch: amd64
BenchmarkCalculation-4   	       1	101890967890 ns/op
PASS
ok  	_/Users/user1/Projects/KnowItJulekalender2019/Day19	101.897s

After just touching the surface of the testing package, I will explore this package in more details. The benchmarking part has a lot of different flags that can be set and it should be interesting to see what I can get out of this package. I like that the built-in testing functionality looks really interesting and that it is so little that need to be done to get started.

Cheers and happy coding.

Teis Lindemark

Read more posts by this author.