Writing unit tests in Golang Part 1: Introducing Testify
By: Sira K.
Tags: Golang, Unit Testing, Programming

Unit testing is a way of writing tests for the individual components (aka, the smallest part) of a program. The purpose of it is to validate that any piece of code is always working as expected.

Moreover, unit testing has a lot of advantages such as improving the quality of code, providing documentation, also the code can be tested individually and doesn’t require another module in order for it to work, etc.

I’m not going to explain what is unit testing because it will be out of this article’s context. I’ll list some useful sources for you for further readings about unit testing at the end of this article.

Why should you always write unit tests?

Simple! To make sure that nothing breaks when you alter the code. Unit testing ensures that everything works as expected and also could prevent or detect bugs before they slip into production.

I’ve seen a lot of programmers who don’t write tests especially unit tests. The lack of tests could introduce bugs in your application that could affect user experience or worse, breaking the app…Yikes!

A package called “Testify”

Testify is …

Go code (golang) set of packages that provide many tools for testifying that your code will behave as you intend.

src: https://github.com/stretchr/testify

Woah, wait a second. But Go already has a standard test package! Why would we need another library for writing tests?

Good question! Go standard test package does a decent job in testing but it still lacks some useful functionality like assertion, mocking, and test suite. Testify is like an extension for the Go standard test package to do all those things I have mentioned (yes, you need the standard test package in order to use Testify).

Examples

In this section, I’ll show you some examples of writing unit testing in Go. Before we begin, I’ll have some tips for you about writing tests.

Tips for writing tests.

Keep your test files in the same directory as the function to be tested. A test function name should begin with the word Test.

Unit Test Example (Easy)

Let’s begin with a simple Add function. This function takes two arguments and returns their summation.

Its test function is shown below.

Looking closely at line 21, you’ll see that we call the function assert.Equal to assert the value of the actual and the expected result. The test passes if they are equal. Otherwise, the test fails.

Unit Test Example (Test Table)

In the next example, I’ll show you how to create a test table for unit testing.

Here we define a function IsAllLowerCase . It returns true if the given input contains only lowercase characters. Otherwise, returns false .

Its test function is shown below.

First, defining testCase struct that has the following fields; name (as the test case name), input (as the function input), expectedResult (as the expected result from the test), hasError (as a flag that the result expects an error).

Define a test table from using a slice of testCase . You can define as many tests as you like. It could be as simple or bizarre as you can think of.

Beginning the test by for…looping through the test table. In this example, we use the functions assert.Nil and assert.NotNil to assert that the current test case does not or does expect an error respectively.

And that’s it for this article. There are still more topics we haven’t discussed such as mocking and test suite. I’ll show you more about them in the next part of this series. See you again and always write tests!

Read my other articles

References and further readings