Go – Define Test Coverage in Golang – With Picture and Coverprofile – With YouTube Video

After ChatGPT showed me how to write tests in Golang (seriously!), I learned that there is a built-in feature, that actually shows the test coverage like this:

The greatest test coverage ever!

The green lines show you where the code has run and was tested and the red lines show where the code was still not covered by the tests. The magical way to get this is calling following line in the command line:

go test -coverprofile=cover.out & go tool cover -html=cover.out

Yes. This beauty works. I even made a YouTube video about it here, so I will not explain a lot to give you some insentives to watch it and become one of my subscribers. The code is below the video 🙂

main_test.go

main.go

Calling the tests is with one of the three options:

go test  – simply runs the tests

go test -v – one step further, gives information about the tests that are run;

go test -coverprofile true  – actually calculates the test coverage;

Enjoy!

Tagged with: , , , , , ,