Listen to this story
Senior Software Engineer
The code in this story is for educational purposes. The readers are solely responsible for whatever they build with it.
VSCode is my favorite IDE nowadays: simple, lightweight, and versatile.
In this article, I'll show how we can configure some useful user snippets in VSCode to make our life easier.
Since I read this Dave Cheney's article, table-driven tests have become my favorite way to write tests in Golang.
Here's an example for calculating Fibonacci number:
func TestRecursiveFibonacci(t *testing.T) {
testCases := []struct {
name string
n uint
want uint
}{
{
name: "zero",
n: 0,
want: 0,
},
{
name: "one",
n: 1,
want: 1,
},
{
name: "two",
n: 2,
want: 1,
},
{
name: "three",
n: 3,
want: 2,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if got := RecursiveFibonacci(tc.n); got != tc.want {
t.Errorf("got: %d, want: %d", got, tc.want)
}
})
}
}
This is the output:
=== RUN TestRecursiveFibonacci
=== RUN TestRecursiveFibonacci/zero
=== RUN TestRecursiveFibonacci/one
=== RUN TestRecursiveFibonacci/two
=== RUN TestRecursiveFibonacci/three
--- PASS: TestRecursiveFibonacci (0.00s)
--- PASS: TestRecursiveFibonacci/zero (0.00s)
--- PASS: TestRecursiveFibonacci/one (0.00s)
--- PASS: TestRecursiveFibonacci/two (0.00s)
--- PASS: TestRecursiveFibonacci/three (0.00s)
PASS
ok bitbucket.org/tiagoharris/fibonacci/fibo 0.715s
It can be a bit boring writing this test structure every time. That's where predefined user snippets come to help.
Open up VSCode. If you're using macOS, hit command + shift + p and start typing "snippet":
After pressing ENTER, type "go" and select "go.json":
Then, ENTER again and it will open "go.json":
Here's where we'll define a snippet for generating the table-driven test basic structure:
"Table driven test": {
"prefix": "tabletest",
"body": [
"func Test${1:YourFunc}(t *testing.T) {",
"\ttestCases := []struct{",
"\t\tname string",
"\t}{",
"\t\t{",
"\t\t\tname: \"happy path\",",
"\t\t},",
"\t}",
"\tfor _, tc := range testCases {",
"\t\tt.Run(tc.name, func(t *testing.T) {",
"\t\t})",
"\t}",
"}"
],
"description": "Create basic structure for a table driven test"
}
When you start typing "table", VSCode will suggest our snippet. Press ENTER:
Notice that the cursor is around "YourFunc", so you can type your test name immediately.
Here's another snippet that I find useful: it defines a basic structure for a file with the main function:
"Main Func": {
"prefix": "mf",
"body": [
"package main\n",
"import (",
"\t\"fmt\"",
"\t\"os\"",
")\n",
"func run() error {",
"\treturn nil",
"}\n",
"func main() {",
"\tif err := run(); err != nil {",
"\t\tfmt.Println(err)",
"\t\tos.Exit(1)",
"\t}",
"}",
],
"description": "Create basic structure for a script with main function"
}
Press "ENTER":
Then a basic structure for a file with the main function will be written.
Our final "go.json" is the following:
{
"Table driven test": {
"prefix": "tabletest",
"body": [
"func Test${1:YourFunc}(t *testing.T) {",
"\ttestCases := []struct{",
"\t\tname string",
"\t}{",
"\t\t{",
"\t\t\tname: \"happy path\",",
"\t\t},",
"\t}",
"\tfor _, tc := range testCases {",
"\t\tt.Run(tc.name, func(t *testing.T) {",
"\t\t})",
"\t}",
"}"
],
"description": "Create basic structure for a table driven test"
},
"Main Func": {
"prefix": "mf",
"body": [
"package main\n",
"import (",
"\t\"fmt\"",
"\t\"os\"",
")\n",
"func run() error {",
"\treturn nil",
"}\n",
"func main() {",
"\tif err := run(); err != nil {",
"\t\tfmt.Println(err)",
"\t\tos.Exit(1)",
"\t}",
"}",
],
"description": "Create basic structure for a script with main function"
}
}
Cool, isn't it?