Skip to the content.

Test your code in Go

In this chapter, we will look at creating and running unit tests.

Introduction

This chapter will cover:

Why we test

It’s good to test your code to ensure it works as intended. In this chapter, we’re looking at unit tests specifically.

What Go provides

Go has a package testing that gives us two things to start out with:

Here’s an example test function:

func TestAdd(t *testing.T) {
 total := Add(2, 2)
 if total != 4 {
  t.Errorf("Sum was incorrect, Actual: %d, Expected: %d", total, 4)
 }
 t.Log("running TestAdd")
}

Your first test

Make sure you’ve created a project with go mod init. Then create a file structure like so:

main.go
math/
  math.go

What you want to do next is to create a test file. You want to keep the test file as close to the code you want to test as possible. Because you want to test math.go you create math_test.go file in the math/ directory like so:

main.go
math/
  math.go
  math_test.go

Authoring and running your first test

Now that you have the file structure above, ensure the math_test.go file has the following content:

package math

import (
 "testing"
)

func TestAdd(t *testing.T) {
  total := Add(2, 2)
  if total != 4 {
    t.Errorf("Sum was incorrect, Actual: %d, Expected: %d", total, 4)
  }
  t.Log("running TestAdd")
}

To run a test, you invoke the go test command. Here are different ways to run your tests:

Control the test run

There are ways to control how many tests are run. Here are some ways:

Coverage

There’s a built-in tool for dealing with coverage. To learn more about the tool, you can type:

go tool cover -help

it will list a set of commands.

The tool is centred on the concept of having an out file. The out file contains instructions on where your code is covered by tests and where it isn’t. An out file can look something like this:

mode: set
test-example/math/math.go:3.32,5.2 1 1
test-example/math/math.go:7.37,9.2 1 1
test-example/math/math.go:11.47,13.2 1 0

This is a format readable by the tool.

It’s a prerequisite to generate said “out file” before you can view your code’s coverage. Place yourself in the directory you mean to measure coverage on and run this command:

go test -coverprofile=c.out

Now you are ready to run a command that shows the result in a browser:

go tool cover -html=c.out

The above command spins up a browser and the output look something like so:

coverage

The coverage report tells us that the the green portions are covered by tests whereas the red portions should have tests covering it.

Learn more

There’s a lot more to learn on testing with Go, have a look at package documentation, docs

Challenge

Create a test for a piece of code you wrote. Run the test. See if you can produce a coverage report and implement any gaps pointed out by the report.