C++ Unit Test Coverage Report with Jenkins — OpenCppCoverage

Hanz
2 min readMay 1, 2020

--

Install OpenCppCoverage on your Jenkins server

OpenCppCoverage is an open-source code coverage tool for C++ under Windows.

The main usage is for unit testing coverage, but you can also use it to know the executed lines in a program for debugging purposes.

Prepare your unit test executable and command

After you built the program of your project, you can test it by running this:

OpenCppCoverage --sources <souce code path> -- <YOURUNITTEST.exe>

For Jenkins, OpenCppCoveragecan easily create code coverage report for Jenkins using Cobertura plugin, in the command, simply set export_type to cobertura

OpenCppCoverage --sources <souce code path> --export_type=cobertura -- <YOURUNITTEST.exe>

Generate report on Jenkins

  • Step1: add Cobertura plugin

As I said before, first, Cobertura plugin need to be installed on Jenkins, if it is not already done.

You can go to http://www.server.com:8080/pluginManager/available where www.server.com:8080 is your server name, search for Cobertura plugin and install the plugin.

You must restart Jenkins.

  • Step2: Run your tests

In your Jenkins project, add command in your Execute Windows batch command section

Your tests should be run with OpenCppCoverage and — export_type=cobertura. This flag will create a xml file named XXXCoverage.xml where XXX is the name of your program.

For the example above, the file YourTestCoverage.xml will be created by the previous command.

  • Step3: Post-build Actions

The last step is to add a Post-build Actions to your project and select Publish Cobertura Coverage Report. In the field Cobertura xml report pattern, you must enter the location of your coverage file.

P.S Note that this location is relative to your workspace folder.

You can use *Coverage.xml if you have several tests for example.

  • Step4: View Coverage Report

Here are some outputs. Note that the Conditionals coverage is not supported.

Reference:

--

--