Testing in Go: Stop Leaking Files

No test suite is perfect. Some test suites are missing good helper functions; others are under-configured or over-customize. Some have obsolete packages included and are left unmaintained. Folks that have experience with more mature projects will likely agree that all of the above can be found in the wild. Often, when we test our Go programs, need to create files. Such files can be just fixture files, or whole file trees, to set up the correct environment for the tests to run....

May 22, 2020 · 12 min · Ilija

Testing in Go: HTTP Servers

Go’s a great hammer for a lot of nails, one of the areas where I find it fitting is building HTTP servers. The net/http package of Go’s standard library makes it easy to attach HTTP handlers to any Go program. What I find delightful is that Go’s standard library, also has packages that make testing HTTP servers as easy as building them. Nowadays, it’s widely accepted that test coverage is important and useful....

March 2, 2020 · 9 min · Ilija

Testing in Go: Clean Tests Using t.Cleanup

Go v1.14 ships with improvements across different aspects of the language. Two of them are brand new t.Cleanup, and b.Cleanup methods, added to the testing package. The introduction of these methods will make it easier for our tests to clean up after themselves. This was always achievable through the careful composition of (sub)tests and helper functions, but since Go 1.14, the testing package will ship with one right way to do that....

February 18, 2020 · 10 min · Ilija

Testing in Go: Golden Files

Hardcoding the expected values in an assertion is a straightforward approach in testing. Most of the time, we know the expected output of the unit under test, so simply adding the raw value to the assertion works well. Things can get tricky when we are testing a unit whose output is cumbersome to hardcode. The straightforward remedy is to extract this cumbersome value to a file that we can then read and compare the output of the unit under test to the output of the file....

February 2, 2020 · 14 min · Ilija

Make resilient Go net/http servers using timeouts, deadlines and context cancellation

When it comes to timeouts, there are two types of people: those who know how tricky they can be, and those who are yet to find out. As tricky as they are, timeouts are a reality in the connected world we live in. As I am writing this, on the other side of the table, two persons are typing on their smartphones, probably chatting to people very far from them. All made possible because of networks....

January 5, 2020 · 15 min · Ilija

Testing in Go: Test Doubles by Example

One of the biggest misconceptions is that doubles are a specific implementation of mocks or other testing constructs that we use in testing. Dummies, mocks, stubs, fakes, and spies ARE test doubles. Test double is the category of these test constructs. Over the years, there has been some confusion around this concept. It is my observation that this confusion arises from the naming of testing constructs that the popular testing frameworks use....

December 16, 2019 · 8 min · Ilija

Testing in Go: Dependency Injection

In software engineering, over the years folks have developed many patterns, techniques and refactoring maneuvers. Some have been long forgotten, while others have stood the test of times. Such a long-standing technique is dependency injection. It is a concept and a programming technique where a construct is passed (injected) to another construct that depends on it. In Golang, like in other languages, we use it to simplify our code and make it more testable....

December 1, 2019 · 8 min · Ilija

Simple Golang database seeding abstraction for Gorm

One of the most feature-full ORMs for Go that I have worked with is Gorm. If you would like to learn more about it, I recommend checking out its official website and its documentation. Recenlty I wanted to write a small database seeding abstraction. Database seeding is a process in which an initial set of data is provided to a database when it is being set up or installed....

November 16, 2019 · 3 min · Ilija

Testing in Go: Fixtures

When I was researching the topic of test fixtures, I couldn’t find much about their beginnings. My first search was about the name of the person who coined “test fixtures”. Unfortunately, that was not a fruitful endeavor. The next logical step was to look for the etymology of the phrase “test fixtures”, but the only search result that made sense was a Wikipedia page on the topic. Judging by the Wiki page, it’s clear that Ruby on Rails has heavily popularized test fixtures as a concept....

September 16, 2019 · 12 min · Ilija

Testing in Go: Subtests

Before we begin: The content in this article assumes knowledge of table-driven tests in Go. If you are unfamiliar with the concept, read this article to familiarize yourself. With table-driven tests as the most popular testing approach, there is one annoying problem that every programmer will face: running selective tests. That’s because the traditional method of testing using table-driven tests in a single test function is not decomposable in granular subfunctions....

July 27, 2019 · 9 min · Ilija