Golang Testing: The Essential Guide

TestFortExpert by TestFortExpert on 01/17/2018

Golang Testing: The Essential Guide

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.

We Work With

Having one outside team deal with every aspect of quality assurance on your software project saves you time and money on creating an in-house QA department. We have dedicated testing engineers with years of experience, and here is what they can help you with.

Software is everywhere around us, and it’s essential for your testing team to be familiar with all the various types and platforms software can come with. In 21+ years, our QA team has tested every type of software there is, and here are some of their specialties.

There are dozens of different types of testing, but it takes a team of experts to know which ones are relevant to your software project and how to include them in the testing strategy the right way. These are just some of the testing types our QA engineers excel in.

The success of a software project depends, among other things, on whether it’s the right fit for the industry it’s in. And that is true not just for the development stage, but also for QA. Different industry have different software requirements, and our team knows all about them.

Icon Manual Testing

Maximum precision and attention to detail for a spotless result.

Icon Testing Automation

We’ll automate thousands of tests for all-encompassing coverage.

Icon Testing Outsourcing

Outsource your testing needs to a team of experts with relevant skills.

Icon Testing Consulting

Overhaul your QA processes to achieve even more testing efficiency.

Icon QA

Thorough Quality Assurance for a project of any scale or complexity.

Icon API Testing

Verify the correct operation of as many APIs as your project needs.

Icon IoT Testing

Stay ahead of the growing Internet of Things market with timely testing.

Icon Web Testing

Reach out to even more customers with a high-quality web application.

Icon Mobile App Testing

Help users fall in love with your mobile app with our texting expertise.


Make sure your CRM/ERP system meets the needs of the stakeholders.

Icon Desktop Application Testing

We’ll check the stability, compatibility, and more of your desktop solution.

Icon Functional Testing

Is your app doing everything it’s supposed to? We’ll help you find out!

Icon Compatibility

Check how your solution works on different devices, platforms, and more.

Icon Usability

Find out if your software solution provides an engaging user experience.

Icon UI

Make sure your application’s UI logic works for all categories of users.

Icon Regression

We’ll verify the integrity of your application after recent code changes.

Icon Online Streaming & Entertainment

Stay on top of the media industry with a technically flawless solution.

Icon eCommerce & Retail

Does your store meet customer needs? We’ll help you know for sure!

Icon HR & Recruiting

Streamline HR processes with a solution that works like a clock

Icon Healthcare

Test the functionality, stability, scalability of your app and more.

Icon Fintech & Banking

Give your users what they want: a powerful, secure fintech product.

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