Requisites
- Vscode, installed Go extesion, of course.
- Install needed go packages: https://github.com/microsoft/vscode-go/wiki/Go-tools-that-the-Go-extension-depends-on
Note: you don't need all packages in above wiki, but everything is good for your Golang development process on vscode, except gometalinter
because it already archived on github
Configuration
Add some configs into User settings, like below:
{
"go.coverOnSave": true,
"go.coverageDecorator": {
"type": "gutter",
"coveredHighlightColor": "rgba(64,128,128,0.5)",
"uncoveredHighlightColor": "rgba(128,64,64,0.25)",
"coveredGutterStyle": "blockgreen",
"uncoveredGutterStyle": "blockred"
},
"go.coverOnSingleTest": true
}
Instead "gutter" on type, you can try with "highlight".
You can take a look on available options vscode-go supports for gutter style here https://github.com/microsoft/vscode-go/blob/master/src/goCover.ts#L39
Example
I'm having this file
// filename: services/person.go
package services
type Person struct {
name string
}
func (person *Person) Name() string {
return person.name
}
func (person *Person) Foo() string {
return person.name
}
Here is my testing code
// filename: services/person_test.go
package services
import (
"testing"
)
func TestPeople_Name(t *testing.T) {
tests := []struct {
name string
person *Person
want string
}{
{"should return a name", &Person{"Vuong"}, "Vuong"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.person.Name(); got != tt.want {
t.Errorf("DecisionMaker.Name() = %v, want %v", got, tt.want)
}
})
}
}
Every time I've done to saving my code, the result of code coverage on services/person.go
will be shown like the below one.
Happy coding!
Top comments (4)
In vs-code
cmnd + shift + p
will open navigatortype
Go: Toggle Test Coverage in current package
for showing the coverageHi vuong, good post!
Which font do you use on this print with coverage?
Regards!
Hi Bernardo, it's Victor Mono!
Great, Vuong! Really thank you!