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 · 9 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 edeavour. The next logical step was to look for 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 test fixutures as a concept has been heavily popularized by Ruby on Rails....

September 16, 2019 · 14 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: selective running of tests. That’s because the traditional approach of testing using table-driven tests in a single test function is not decomposable in granular subfunctions....

July 27, 2019 · 10 min · Ilija

Testing in Go: Table-Driven Tests

Coming from Ruby, which has excellent testing tools and libraries, the notion of table-driven tests was unusual for me. The popular testing libraries in Ruby, like RSpec, force the programmer to approach testing from a BDD standpoint. Thus, coming to Go and learning about the table-driven test was definitely a new way of looking at tests for me. Looking back, Dave Cheney’s 2013 seminal blog post “Writing table driven-tests in Go” was very likely my gateway to table-driven tests....

July 10, 2019 · 11 min · Ilija

Testing in Go: go test

If you’re going to remember anything from this article, remember this: go test is a command which automates the execution of test files and functions in a Go project. The go test command ships with Go itself, so if you have Go installed there’s nothing to check - it’s available on your machine. go test will recompile each package along with any files with names matching the file pattern *_test.go. These *_test....

July 1, 2019 · 17 min · Ilija

Testing in Go: Naming Conventions

Programming languages and tools often have conventions. These conventions help make our work more straightforward. Just like all tools out there, Go also has some conventions when it comes to testing. Some are defined in the language itself (official), while others are coined by community members (unofficial). In this article we will look at the naming conventions for files, functions and variables separately. File and package naming conventions Go’s testing package comes with an expectation that any test file must have a _test....

June 24, 2019 · 11 min · Ilija

Testing in Go: Writing Practical Failure Messages

All developers appreciate code that works, yet we spend much of our working hours debugging existing code. When fixing existing code, what our test failures communicate is paramount to the debugging experience we have. That’s why in this article we will look at what it means to write a meaningful test failure message. We will look at its structure and how we can use some simple techniques to improve our test failure messages....

June 15, 2019 · 15 min · Ilija

Testing in Go: Failing Tests

In the previous article on testing in Golang, titled Testing in Go: First Principles, we looked at what testing looks like in Golang. In this article, we will do a deeper dive in the testing package. More specifically, we will take a look at the different ways we can fail tests in our test suites and when and how we should use these techniques. Without further ado, let’s take a look under the covers in the testing package....

May 30, 2019 · 16 min · Ilija

Testing in Go: First Principles

If you have any programming experience, whether that’s as a student or a professional, there’s a good chance you have heard about testing. It’s an omnipresent topic, be it on conferences, books or articles. (See what I did there?) Also, it seems like a topic that everyone agrees on - yes, testing is good and we should do it. There are many reasons why folks consider testing good for you code....

May 8, 2019 · 11 min · Ilija