Testing Code Coverage

Let’s learn how to test code coverage.

We'll cover the following

In this lesson, we are going to learn how to find information about the code coverage of our programs to discover blocks of code or single code statements that are not being executed by testing functions.

Among other things, seeing the code coverage of programs can reveal issues and bugs in the code, so do not underestimate its usefulness. However, the code coverage test complements unit testing without replacing it. The only thing to remember is that we should make sure that the testing functions try to cover all cases and, therefore, try to run all available code. If the testing functions do not try to cover all cases, then the issue might be with them, not the code that is being tested.

Coding example

The code of coverage.go, which has some intentional issues in order to show how unreachable code is identified, is as follows:

Get hands-on with 1200+ tech skills courses.