Adding coverage reports in Jenkins to GoogleTest with gcovr

kinow @ Oct 16, 2012 16:09:33

After the last post about GoogleTest and TestLink using Jenkins TestLink Plug-in, I received an e-mail asking about coverage with GoogleTest and Jenkins. I’ve just updated the Makefile in the samples directory, of the GoogleTest TAP listener project, to output coverage data.

Basically, you have to add the compiler flags -fprofile-arcs -ftest-coverage and link the executable with -lgcov. Take a look at the project’s Makefile and you’ll notice how simple it is. In order to have Jenkins interpreting your coverage report, you’ll have to convert it to cobertura XML. There is a Python utility that can be used for this: gcovr. Download it and copy it to somewhere where Jenkins can execute it (e.g.: /usr/local/bin).

Now, if you’ve followed the instructions from the previous post, you should have a job that reports your GoogleTest tests from Jenkins to TestLink using the plug-in, and is downloading the source code from GitHub. Add an extra build step (Shell) to execute gcovr.

gcovr -x -r samples/src > coverage.xml

This should produce a file coverage.xml in your build workspace. The last step is add cobertura post build step. Of course you’ll need Jenkins Cobertura Plug-in (quick tip: if you are using the newest Jenkins, you can install it without restarting, it works well). Simply add the cobertura post build step and make sure to use an include pattern that matches your output file from gcovr (e.g.: **/coverage.xml).

Voilà , you should now have coverage reports in Jenkins that looks like the image below.

A final note, is that we used an example project bundled with GoogleTest TAP Listener, and is not very useful, as it has no other classes being tested. In a real world project, you’ll have many classes that you want to coverage, while other you’d prefer to omit from the report. You can do so by using the -e gcovr flag. For more, try gcovr —help.

Hope that helps, have fun!