Golang Testing: The Essential Guide


5 m read


Software Testing Trends, Testing & QA, Testing Methodolo­gies, Unit Testing, Web Application Testing

Spread the love

Testing has always been a crucial part of software development. Some programming languages are created without much focus on that aspect, but then there’s Golang.

The Google Team made sure testing is easily performed for apps and software written on Go. We like that. Dive into the review of Golang testing fundamentals as our QA professionals share a few tips on testing Go code from their personal and practical experience.

Testing in Golang: Why?

Writing thousands of test cases to make sure your software works flawlessly is time-consuming. Due to that, proper and detailed testing is neglected during development and only carried out at the final stages. This may end up as a problem for both the developers and the customer. Thousands of bugs, slow performance, and constant crashes are the most common resulting issues when testing is disregarded. They can lead to a tech debt, and nobody wants that to happen. Avoiding these inconveniences is possible, but definitely requires your time and attention. Now, let’s figure out what can be done with the code you’ve written. Namely, go test!

Go unit testing

Unit Testing (UT) is a fundamental starting point which helps you gain an overall understanding of the processes if you are new to testing. It is probably the most effective way to search for all undesired elements in your code snippets while testing the overall output at the same time. UT is considered time-consuming in general, but everything changed when Golang was introduced the world. Thanks to the package “testing” you can nail it in a couple of minutes.

Let’s get down to business and create a simple unit:

package mishmash
import (
func mishMash(num int) string {
div2 := (num % 2) == 0
div6 := (num % 6) == 0
if div2 && div6 {
return “MishMash”
} else if div2 {
return “Mish”
} else if div6 {
return “Mash”
} else {

In Golang, managing testing files is quite easy. You just create a doc “file_test.go” and locate it in the same folder where the code that needs to be tested already lies. Let’s take a look at our “mishmash_test.go”. We will create several test cases in order to check whether the output corresponds to our expectations.

package mishmash
import (
func TestMishMash_Mish (t *testing.T) {
if mishMash(2) != “Mish” {
t.Error(“Expected returned string to be ‘Mish'”)
func TestMishMash_Mash (t *testing.T) {
if mishMash(6) != “Mash” {
t.Error(“Expected returned string to be ‘Mash'”)
func TestMishMash_MishMash (t *testing.T) {
if mishMash(12) != “MishMash” {
t.Error(“Expected returned string to be ‘MishMash'”)
func TestMishMash_Num (t *testing.T) {
if mishMash(4) != “4” {
t.Error(“Expected returned string to be ‘4’”)

Using the go test -v -race ./… command we will check whether the expected result is validated for each test case. If you need more insights on the -race flag, check out this material on Data races. The command is very useful, it provides the report on conflicts which occur when two goroutines try to access the same variable.

Golang mocks are also a widely-used UT technique to examine the code interaction with cache, more specifically — how values are inputted and outputted from there. You’ve probably already used or at least have heard about mocking in coding. It can be used not only with Go’s built-in “testing” package but in other contexts too.

Go benchmark

Automated testing of packages is nice to learn as well. Benchmark tests provide you with reports on software performance. They will show you how fast the software loads. In case you don’t need to dive into details but want to conduct some simple code speed testing — use this command go test -bench=. To check the memory consumption and allocation you can use a benchmem command.

Yes, Golang allows you just to write the word “Benchmark” before the method name, and it will be included in the testing report: func BenchmarkXxx(*testing.B). For more precise testing, create a benchmark for every function in your code.

Integration testing in Go

With an eye to consistency, you can set-up integration tests in Go which allow you to write code faster without being distracted by separate Unit Tests. Continuous integration checks the same issues as unit testing does but immediately after the code is submitted. This verifies that your code is logically compiled and every function performs like it is supposed to. However, in the face of poor code coverage, this method has a downside though (we discuss this further down).

For complex cross-platform applications that are not limited to Go you can level-up your testing using frameworks like GoConvey. It supports native Golang “testing” package and brings more power to the overall process, allowing you to write behavioral tests in your IDE and check the results live in your browser.

For outer-connected services like DataBases you can opt for (guess what? drumroll..) Docker. Here you should pay attention to the DB migrations and make sure the created Docker containers are pulled up to the test cases.

Skipping in Go

This testing technique is used for quicker and selective testing, in case you are in a hurry. You might disagree and say, “Why not simply creating a unit test for the specific function?” Of course, you can, but the thing is, it takes much more time. When you are setting up testing for further integration — you don’t want to get distracted and create a totally separate doc to quickly run through a peculiar snippet. Instead, you can deliberately omit testing the functions you don’t want to check right now. This can be done with the help of go test -short.

Here’s the example of how you can implement this technique:

package mishmash
import (
func TestMishMash(t *testing.T) {
if testing.Short() {
t.Skip(“skipping test case in short mode”)
// the rest of test

Golang assert

For even easier Golang testing, there is a lesser known technique from the “testify” package. QA engineers tend to use “assert” for more complex projects. This allows them to write shorter test cases, and retrieve the same accurate results as provided by unit tests in Golang. For instance:

package mishmash
import (
func TestMishMash(t *testing.T) {
var Mish string = “5”
var Mash string = “7”

assert.NotEqual(t, Mish, Mash “Outputs should not be the same”)

Testing web-apps in Go

We’ve uncovered the peculiarities of software testing, and now let’s devote a couple of warm words to HTTP testing. Building web apps with Go is even more convenient now. Accessing the documentation is easy (as always). The package path is “net/http/httptest“. You are able to test “new user”, “log-in”, “authentication” requests, and basically any http-request handlers within your web application.

Code coverage in Golang

So you’ve done a couple of unit tests, made sure everything works just fine, and you want to finish. The thing is, it may only appear to you that all of the code was tested. Lame. That’s what code coverage is for. Run it through your code and check the percentage of really tested pieces in comparison to the ones that weren’t.

The most common feature of this testing technique is that we usually write a case template (this applies to integrated testing in particular) and then add something to the code that was not mentioned in this test case, and hence can’t be retrieved. Consequently, code coverage reports may disappoint you the first couple of times. But, in the end, you learn from your mistakes and improve your code coverage.

Go more

If you are feeling a bit lost while writing another test case it can be useful to check what go help test and go help testflag commands offer. Take a look at the “testing/iotest” and “testing/quick” packages.

Go conclude

The Golang Team showed us how testing can and should be done. Many programmers all over the world started to actually enjoy test-driven development thanks to Go. It was made to be simple — there’s barely an excuse not try it. We hope you will enjoy testing software on Golang as much as we do!

If you have an outstanding idea but don’t know how to turn it into a real app — contact us. Our teams of developers and QA experts are willing to provide you with a high-end product and thorough testing services.

Realizing the importance of providing service on agreed terms, we consider all possible risks and provide efficient solutions for all possible risks and provide efficient solutions.

Your information was successfully submitted.


We are glad to have you with us!
You'll receive an email from us shortly. Meanwhile, you can check our super-informative blog to go through the latest updates in the world of software development.

Got it
We use cookies to ensure your best experience. By continuing to browse this site, you accept the use of cookies and "third-party" cookies. For more information or to refuse consent to some cookies, please see our Privacy Policy and Cookie Policy