Member-only story
A Quick Way to Generate Go Tests in Visual Studio Code
Click here to generate unit tests

I just learned of a quick way to write Go tests in Visual Studio Code (VSC). While programming with a colleague, I noticed he generated a test’s boilerplate code after right-clicking somewhere in the editor. That got my attention since it was my first time seeing that magic. So, after a bit of Google, I found out how he did it. In this short piece, I’ll describe the process.
The tool that creates this boilerplate code is called gotests
. It generates Go's table-driven tests that cover the functions present in a Go file or generates them from a method's signature. To use it, you could either install it and execute it from the command line or directly from VSC since it is included in VSC's Go plugin. Let's see how.
Suppose you have a Go source file like this:

It has a struct Superhero
, a map nameToSuperheroName
to translate a superhero's real name to its alias, and a function that creates a new Superhero
and checks if the map contains its alias. To generate its boilerplate test code, right-click on the function's signature (where it says NewSuperhero
) and…