I’ve written a ton of tests in golang without properly knowing about the -coverprofile
flag and how it can be used to visualize parts of code that aren’t covered in unit tests.
That being said, I did know about the -cover
flag that helped me get a conventional idea of code coverage in my unit tests (in %). So, I’m documenting how this can be done without any external dependencies.
To visualize coverage reports while running go test for a particular package:
- Run tests, generate a raw coverage report & store it inside a
.out
file, for instance,
go test -coverprofile=toolbox.out ./toolbox
- Generate coverage report in terms of lines of code, for example,
go tool cover -html=toolbox.out
Doing so will generate and open a temporary coverage.html
file with colour-coded code coverage information!
This tool could help programmers gain back a ton of time in dev-loops. Hope you found this helpful!
π βΈ» @TnvMadhav