Member-only story
Testing in Go
Tips, tricks and tools
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.
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
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…