Just like everything else in this language, test and cover code is just fun and very very simple.
The single condition to get modern go to work is to create a go mod project.
In order to sample tests, i had to run the go mod init command.
Modules when published have to point to the public repo.
Another alternative was to disable go modules but modules ae here to stay so let's get used to them.
To make go understand this folder as a module, i just did:
go mod init 0007-tests
And that's it.
Now although i didn't made a main function to serve as entrypoint, i can run the tests using this command:
go test -v
One nice thing to have when you already have tests is to enable coverage.
Coverage helps to understand what is actually really getting tested, and thus produce new or better testcases.
In order to enable coverage, install the cover tool:
go get golang.org/x/tools/cmd/cover
Now your test command takes this form:
go test -v -coverprofile=coverage.out
go tool cover -html=coverage.out -o coverage.html
Or using your preferred IDE:
And that's it!
You can see more about my go snippets here.