Go Test CodeLens - Enhanced
Run and debug Go table-driven tests in VS Code, using CodeLens hints above the table-driven test names.
CodeLens support for table-driven tests
This extension:
- Can handle any structured slice or map.
- Does not require any specific property naming - it will work with variable names or struct field names that you pass to
t.Run
as the first ("name") argument.
Relies on the Go for Visual Studio Code extension.
Run test
| Debug test
- both supported, for individual table-driven test scenarios.
🗺️ Maps
table := map[string]string{
// run test | debug test <-- CodeLens run/debug "test name one"
"test name one": "whatever",
// run test | debug test <-- CodeLens run/debug "test name two"
"test name two": "whatever",
}
for k, v := range table {
t.Run(k, func(t *testing.T) {
t.Parallel()
fmt.Println(k, v)
})
}
🍞 Slices
table := []struct {
stuff string // <-- Arbitrary test name given as t.Run(v.stuff, ...)
value string
}{
{
// run test | debug test <-- CodeLens run/debug "test name one"
stuff: "test name one",
value: "value1",
},
{
// run test | debug test <-- CodeLens run/debug "test name two"
stuff: "test name two",
value: "value2",
},
}
for _, v := range table {
t.Run(v.stuff, func(t *testing.T) {
t.Parallel()
fmt.Println(v.stuff, v.value)
})
}
🦹 What are you doing, you monster! (but it does work) 🙈
mTable := map[string]string{
// run test | debug test <-- CodeLens run/debug "test name one"
"test name one": "value1",
// run test | debug test <-- CodeLens run/debug "test name two"
"test name two": "value2",
}
sTable := []struct {
name string
value string
}{
{
// run test | debug test <-- CodeLens run/debug "test name one#01"
name: "test name one",
value: "value1",
},
{
// run test | debug test <-- CodeLens run/debug "test name two#01"
name: "test name two",
value: "value2",
},
}
for k, v := range mTable {
t.Run(k, func(t *testing.T) {
t.Parallel()
fmt.Println(k, v)
})
}
for _, v := range sTable {
t.Run(v.name, func(t *testing.T) {
t.Parallel()
fmt.Println(v.name, v.value)
})
}
How you can use it
- Open any Go test file (*_test.go)
- Find any slice- or map-based table-driven tests
- Look for the
run test | debug test
CodeLens above the test name
- Click, and watch the magic unfold 🧙 ✨ 🧪 🎯
Requirements
This extension requires the Go for Visual Studio Code extension to be installed.
Future enhancements
- Should I look at integrating with VS Code's Test Explorer for a more native test running experience?
Installation
You can install from here.
Credits
Loosely based on the work of takaaa220 - thanks!
License
Apache License Version 2.0