Member-only story

Testing in Go

Tips, tricks and tools

Martin Stancanelli
4 min readDec 6, 2020

Making your Go service testable requires dedication from the very first minute of its existence. To turn an old service testable, while is not impossible, is a harder task. So be sure to have testing in your mind (always, not just in Go) before you start coding.

gophers with tools

Interfaces everywhere

Testing usually comes with mocking holding its hand. Mocking functions, services and stuff in Go relies heavily on interfaces. We’ll talk about tools later on but at least the two most used rely on implementing the interface you want to test and do checking on it’s parameters and return values.

So, the rule of thumb here is

Make an interface the front door of your structs

interfaces, interfaces everywhere

Expose every logic via an interface. The smaller the better, we like the I in SOLID.

This allows us to expose always what we just need, not everything, and make our code mockable, essential for keeping our tests independent on each other.

Mocking tools

Now that we have our interfaces ready to be mocked we can start writing those mocks. The…

--

--

Martin Stancanelli
Martin Stancanelli

Written by Martin Stancanelli

Engineer with 9+ years working for startups and some of LATAM’s unicorns. Passionate about high quality yet simple software, mentorship and product development

No responses yet