Better Programming

Advice for programmers.

Follow publication

Member-only story

A Quick Way to Generate Go Tests in Visual Studio Code

Juan De Dios Santos
Better Programming
Published in
4 min readSep 3, 2022
tl;dr: click here.

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…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Juan De Dios Santos
Juan De Dios Santos

Written by Juan De Dios Santos

Data storyteller, Trust and Safety Software Engineer, and fan of quantifying my life. Also, I like Pokemon. https://juandes.com, @jdiossantos.

Write a response