Over the last week I was trying to add unit tests to some rather old piece of code in frameworks. Of course I wanted to know how good this test is and looked into how one can generate test coverage for our tests. As I consider this as rather useful I thought to share the steps.
For generating the test coverage I used gcov which is part of gcc. So nothing to do there. As a frontend I decided for lcov as that can generate some nice html views. On debian based systems it is proved by package lcov.
Let’s assume that we have a standard framework with:
The first step to enable test coverage is to modify the CMakeLists.txt. There is the possibility to switch to CMAKE_BUILD_TYPE “Coverage”, but that would require building all of frameworks with it. Interesting for the CI system, but maybe not for just developing one unit test.
In your framework toplevel CMakeLists.txt add the following definition:
And also adjust the linking of each of your targets:
target_link_libraries(targetName -fprofile-arcs -ftest-coverage)
Remember to not commit those changes
Now recompile the framework, go the build directory of your framework and run:
This generates the basic coverage data which you can now process with lcov run from your build directory:
lcov --capture --directory ./ --output-file coverage.info
And last step is to generate the html output:
genhtml coverage.info --output-directory /path/to/generated/pages
Open your favorite web browser and load index.html in the specified path and enjoy.