Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Testing Code in Go Language

Testing code from the start keeps your product from being riddled with bugs in the end. Let's get started with testing in Go using the built-in testing package.

Oct 2, 2020 • 6 Minute Read

Introduction

Have you ever used an app or some code from GitHub, received errors, and had no idea why? Maybe you had to reach out to the vendor or the maintainer of the GitHub repository to ask for help. These types of situations can be mitigated with proper testing.

When it comes to code quality, one of the first things you should think about is testing code. Testing code from the start is the difference between shipping a product that is riddled with bugs and one that is consumer-ready.

In this guide, you'll learn the first steps toward getting started with testing in Go using the built-in testing package.

The Go Testing Package

When you're testing code in other languages besides Go, one of the first questions to come to mind is, What library or framework am I going to use? Luckily, with Go, there is a testing package built in. That means the testing package is part of the standard library and you don't have to use an external framework.

The package provides support for ad-hoc and automated testing of Go packages. Once you write the test, you can run it using the go test command.

The go test command will look at all Go files in the current directory where you're running the command form. go test will look for two keywords:

  1. Any Go file that ends with _test.go
  2. All function names that start with Test

Although not mandatory, the function name should start with a capital T. Otherwise, linters like Golint will throw errors to the console.

Some popular ways to use the Go testing package include:

  • Running tests directly from the command-line in an ad-hoc fashion.
  • Running automated tests
  • Running tests in pipelines. For example, you can use a GitHub Actions workflow to kick off a Go test. That means the test will run before the code even attempts to build, so you know if issues occur from the start.

Benchmark Tests

A cool feature included in the Go testing package is for benchmark tests,—that is, to see the speed of the code. If fast code and deploying quickly is important to you, you may find yourself testing solutions in different languages. An example is testing a function in Python vs. Go and seeing which one is faster.

With the benchmark feature, you can easily clock the code.

Testing Example

There are many types of testing. Some of the most popular include:

  • Unit testing
  • Integration testing
  • Mock testing
  • Smoke testing
  • Regression testing
  • User acceptance testing

And there are many others.

In fact, the different types of testing can fill a whole book (and there are books to prove it).

The key type of test this guide will focus on is unit testing, which is a test that ensures each part of your code delivers the desired output, that is, what you're expecting.

Below is an example of a Go test.

      package main

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestAddition(t *testing.T) {
	x := 2
	y := 2

	assert.Equal(t, x, y, "x and y should be the same")
}
    

Let's go over it section by section.

The first section is to create a main package.

      package main
    

Next, specify what packages you want to import. The first is the standard test package and the second is an assert package. Assert provides a list of testing tools, for example, to see whether two values equal each other in a unit test.

      import (
	"testing"

	"github.com/stretchr/testify/assert"
)
    

Finally, you have the function. The function is comprised of a few key aspects that are a must for testing:

  • First, the function name starts with Test, which tells Go that this function isn't just a standard function, but a testing function.
  • The argument t is of type *testing.T. *testing is calling the value of type testing and type .T is for managing test state and formatted test logs.

The function itself returns a simple test. It tests whether the x and y variable are both of an int of 2. To test that the two variables are equal, use the assert package.

The assert package contains:

  • The t argument to let assert know that you're testing against the function
  • The two variables, x and y, which are passed in because those are the variables you're testing
  • The string "x and why should be the same", which tells the Equal() function what the desired outcome should be
      func TestAddition(t *testing.T) {
	x := 2
	y := 2

	assert.Equal(t, x, y, "x and y should be the same")
}
    

Save the code into a Go file. Remember, the Go file needs to end with _test.go. For simplicity's sake, save the Go file as main_test.go.

To run the test, run the following command:

      go test main_test.go
    

The output should look similar to the screenshot below.

Congrats! You have successfully run a unit test in Go.

Conclusion

Quality code is important in all scenarios, whether it's code going out to a customer or code staying internal for your fellow teammates to use. One thing holds true: the code must work. Code that you write can work at that exact moment, but once it's committed to source control, you have zero idea whether it'll work again unless you're manually testing it every five minutes.

With tests, you can remove that manual process of having to test code and automate the testing process. It saves you time and helps everyone by ensuring the code they're interacting with works as expected.