GOLANG: UNIT TESTING

Unit Testing made easy in Go

In this article, we will learn about unit testing in Go. Go provide built-in functionality to test your Go code. Hence, we don’t need an expensive setup or 3rd party libraries to create unit tests.

Uday Hiwarale
May 11, 2019 · 13 min read
Image for post
Image for post
(source: pexels.com)
import "testing"func TestAbc(t *testing.T) {
t.Error() // to indicate test failed
}
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
go test
$ greeting go test
PASS
ok greeting 0.006s
Image for post
Image for post
go test with verbose mode
Image for post
Image for post
go test with verbose additional logs
Image for post
Image for post
test failed
Image for post
Image for post
use gotest instead of go test command with all valid options
Image for post
Image for post
run test functions starting with the name TestHelloE
Image for post
Image for post
go test with the selected test files

Test Coverage

Test Coverage is the percentage of your code covered by test suit. In layman’s language, it is the measurement of how many lines of code in your package were executed when you ran your test suit (compared to total lines in your code). Go provide built-in functionality to check your code coverage.

Image for post
Image for post
go test with coverage
Image for post
Image for post
go test with cover profile
Image for post
Image for post
go cover tool
Image for post
Image for post

Working with tests in Go Module

What we have done so far is to create test cases for package located inside $GOPATH. Post Go1.11, we have Go modules which are favored. In the previous tutorial, we have created a module to manipulate numbers. Let’s carry it forward and write test cases for it.

Image for post
Image for post
go test for a package in the module

Multiple Inputs Problem

A unit component is more reliable when tested with more data. So far, we have tested our unit components with one input data, but in reality, we should test them with sufficiently reliable data.

Image for post
Image for post
go test transform package
Image for post
Image for post
go test ./…
  • go test package when package belongs to $GOPATH as long as you are not executing this command from inside a Go Module.
  • go test ./tranform to test package in ./tranform directory.
  • go test ./... to test all the package in the current directory.
Image for post
Image for post
output test binary with -c flag

Separation of concern

When we write test cases inside a package, these test files are exposed to exported and non-exported members of the package (because they are in the same directory of the source files). So we are not sure, not whether or not these unit components are accessible to the end user.

Image for post
Image for post
putting tests in a different package

Test Data

Let’s say that you have a package that performs some operations on CSV or Excel spreadsheet files, and you want to write test cases for it, where would you store the file? You can’t store outside the package because then you would need to ship them separately for anybody who wants to perform tests.

// some_test.gofile, err := os.Open("./testdata/file.go")
if err != nil {
log.Fatal(err)
}

Using Assertions

If you are familiar with tests in other languages like node.js then you probably have used assertion libraries like chai or built-in package assert. Go does not provide any built-in package for assertions.

Go doesn't provide assertions. They are undeniably convenient, but our experience has been that programmers use them as a crutch to avoid thinking about proper error handling and reporting. [more]
Image for post
Image for post

RunGo

A go-to guide for learning Go programming language

Uday Hiwarale

Written by

Senior Software Engineer (JS • Go • Dart • Python) / Available for full time job / thatisuday.com ☯ github.com/thatisuday ☯ thatisuday@gmail.com

RunGo

RunGo

A place to find introductory Go programming language tutorials and learning resources. In this publication, we will learn Go in an incremental manner, starting from beginner lessons with mini examples to more advanced lessons.

Uday Hiwarale

Written by

Senior Software Engineer (JS • Go • Dart • Python) / Available for full time job / thatisuday.com ☯ github.com/thatisuday ☯ thatisuday@gmail.com

RunGo

RunGo

A place to find introductory Go programming language tutorials and learning resources. In this publication, we will learn Go in an incremental manner, starting from beginner lessons with mini examples to more advanced lessons.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store