The traditional Linux and Unix way for finding code coverage is to use gcov to generate the coverage map and lcov to make the output pretty. Code coverage measures if, and if so, how many times, a line of code is executed. This is useful for measuring the efficacy of your test code. In theory, the more lines that are “covered”, the more complete your tests are. However, the link between code coverage and test completeness can be tenuous.
Code Coverage Data
Code coverage data typically comes in two pieces, line coverage and function coverage, both of which are most easily expressed as percentages. These numbers are easily understood for individual unit tests. When testing either an individual function or a method within an object, the total number of functions and lines in the file loaded serves as the denominator for the percentage calculation. So, if you spread your testing across multiple files for a single module, unit test coverage will be low for each individual test. Aggregation of all the coverage numbers from each individual test will give the complete coverage picture for that file.
If you encounter and error as shown below:
lcov: command not found
you may try installing below package as per your choice of distribution.
|OS X||brew install lcov|
|Debian||apt-get install lcov|
|Ubuntu||apt-get install lcov|
|Kali Linux||apt-get install lcov|
|Fedora||dnf install lcov|
|Raspbian||apt-get install lcov|
Generating and viewing code coverage information is crucial for unit testing and important for aggregated integration testing. While code coverage numbers do not tell the whole tale, code coverage information does provide a nice single number to use to track the progress of your tests.