Unit testing in Go with Ginkgo: Part 1

Mark St. Godard
Apr 3, 2019 · 4 min read
Image for post
Image for post

At Bold, many of our back-end services are written in Go. On any given day, over 80,000 eCommerce websites use our software to power their online sales. Any downtime or severe bugs could result in lost sales and revenue for these merchants, which is why it’s important that we focus on delivering high-quality production code.

Test-driven development (TDD) is an important practice in our software development process. It helps give us confidence that the changes we’re introducing won’t cause any unintended side effects.

Go has a fairly capable built-in testing package, however it does have some limitations. This article will explain (with some simple examples) how we use Ginkgo and Gomega at Bold to write more expressive and structured unit tests in Go.

Background

Let’s first take a look at Go’s testing package and toolchain:

For example, I have a package adder which provides an Add function that sums two integers.

Using the testing package, a test could be written like this:

Run the test in your terminal with go test.

Great, a passing unit test, but what if my function has a bug and subtracts instead of adds? What would the output of a go test look like?

This is fine for basic use, but the built-in Go testing package has two major limitations:

  • It only provides a simple XUnit style of tests
  • It does not provide a library for assertions

Enter Ginkgo 🎉

Ginkgo is a Go unit testing framework, created by Onsi Fakhouri, that provides for a BDD style of unit tests (think RSpec, but for Go).

Getting started

To get started with Ginkgo, follow these steps.

Install the latest version of ginkgo with go get.

Ginkgo requires a test suite for each package that your program uses.

This will create a test suite to bootstrap running your tests.

Now that we have a suite, let’s create our first Ginkgo unit test!

This will generate an empty test file, into which you can start adding individual unit tests.

Now, let’s update and add our first test:

To run our unit tests, we run Ginkgo.

Now, let’s say our requirements change and we’re only allowed to add positive numbers. Using the TDD process, let’s start by adding a failing test.

We can use Context to indicate specific pre-conditions, such as when our inputs are valid or not valid:

Now, we’ll re-run our tests.

Okay, so we have a failing test. Time to update our function to get the test to pass.

Now, we’ll re-run them so that we should have two passing tests.

Tip: Using -v provides more of a human-readable description of your unit tests.


Summary

Ginkgo is a powerful unit testing framework, providing a BDD-style DSL that runs on top of the standard Go testing infrastructure.

Ginkgo provides the DSL that allows developers to compose tests in a more expressive way.

Describe blocks define the thing you’re testing.

Context blocks define the when of the test (i.e. “when invalid request is received,” “when service is unavailable”).

It blocks run the code to test and state what you expect to happen.

BeforeEach blocks run before each unit test ( It blocks).

AfterEach blocks run after each unit test.

Next steps 🚀

Ginkgo helps you write more expressive unit tests in Go (like the one we did today) to make your program safer and more dependable.

Our next post will take a deeper dive into Ginkgo, including:

  • More advanced assertion matching using Gomega
  • How to generate “fakes” to mock your dependencies
  • How to write tests with concurrent code
  • Tips and tricks we’ve learned along the way

Boldly Going

Technical insights from the eCommerce experts at Bold…

Thanks to Ryan Hofer and Bold Commerce

Mark St. Godard

Written by

Director of Software Development at Bold. Previous: IBM and CloudFoundry. I ❤️vim. https://markstgodard.dev

Boldly Going

Technical insights from the eCommerce experts at Bold Commerce.

Mark St. Godard

Written by

Director of Software Development at Bold. Previous: IBM and CloudFoundry. I ❤️vim. https://markstgodard.dev

Boldly Going

Technical insights from the eCommerce experts at Bold Commerce.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

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