From the course: Go for Developers: Practical Techniques for Effective Coding

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Code coverage

Code coverage

- One of the most important parts of writing a solid application that is robust and maintainable is to ensure that our code is well tested. This is especially important when we are working with large code bases. When we are writing tests, we need to know which branches of code are covered and which are not. We can do this by generating code coverage reports. When we run Go tests by default, we're given no code coverage information. Here we can see the tests and the demo package have passed, but we don't know just how much of the code in the package is actually tested. The Go test command provides a useful dash cover flag that will give us an overall percentage of coverage per package. So here we add the dash cover flag, and as we can see from the test output, only 53.3% of the package is actually tested. I personally add the dash cover flag to my normal testing scripts, just seeing the overall percentage is a huge help in helping me to understand where I'm lacking coverage. When I see…

Contents