How to Run Go Tests with Coverage Percentage

Go has a brilliant built in testing package, which although it is quite raw, it is very powerful. It also has the ability to show the test coverage as a percentage of the code base. Which is very useful to get perspective of a project and to know perhaps which areas need improvement.

You can see the test coverage by using the -coverprofile parameter on the test command.

go test -coverprofile cp.out
# coverage: 60.2% of statements
# ok      yourpackage  1.372s

This is great, but even better is being able to visualise the test coverage. You can see which code is run during testing and which code is not by loading the cover profile in a browser. To do this, use this command:

go tool cover -html=cp.out

Which will produce something like this (although I’ve cropped it down):

Leave a Reply

Your email address will not be published. Required fields are marked *