Show code coverage with a source code in Jenkins wiht Cobertura (run result from other machine) Show code coverage with a source code in Jenkins wiht Cobertura (run result from other machine) jenkins jenkins

Show code coverage with a source code in Jenkins wiht Cobertura (run result from other machine)


I encountered a very similar issue when I had to develop a CI pipeline for a very huge C++ client. I had the best results if I avoided the Cobertura Plugin and instead used the HTML Publisher Plugin. The main issue I had was also finding the source files.

  1. Convert OpenCppCoverage result to HTML

This step is quite easy. You have to add the parameter --export_type=html:<outputPath> (see Commandline-reference) to the OpenCppCoverage call.

mkdir CodeCoverageOpenCppCoverage.exe --export_type=html:CodeCoverage <GoogleTest.exe>

The commands above should result in a html-file in the directory <jenkins_workspace>/CodeCoverage/index.html

  1. Publish the OpenCppCoverage result

To do this we use the HTML Publisher Plugin as I mentioned above. reportDir is the directory created in step one and which contains our html-file. Its path is relative to the Jenkins workspace.

    publishHTML target: [      allowMissing: false,      alwaysLinkToLastBuild: true,      keepAll: true,      reportDir: 'CodeCoverage',      reportFiles: 'index.html',      reportName: 'Code Coverage'      ]

and to be sure that everyone can download and check the result locally we archieve the result of OpenCppCoverage:

   archiveArtifacts artifacts: 'CodeCoverage/*.*'

You can see the result now in the sidebar of your pipeline under Code Coverage and the result will look like the following:

enter image description hereenter image description here

This is the solution that worked for me.

I hope this helps at least a bit. I can only advice do avoid the Cobertura Plugin. I wasted so much time try to fix it and recognize my sources...


Ok I've found reasons why I had a problems with this plugin.

  1. xml from openCppCoverage is just correct. No changes are needed here to make it work (as far as sources are there where pdb file points to). Sources outside Jenkins workspace are not the problem here. When I copied executable from build machine to test machine, then run tests with openCppCoverage and copied result back to build machine it is just fine.

  2. In job configuration any user which supposed to view code coverage has to have access to Job/workspace in security section. In my case I've enabled this for all logged in users. enter image description here This covers last bullet point of error message.

  3. Most important thing: build must be successful. I mean form beginning to the end. Doesn't meter if step containing call to cobertura plugin was successful. If any step (even in the future step) fails then cobertura will not show code for this coverage run. In my case build job was failing since one of tests was timing out. This was caused by openCppCoverage overhead which slows down tests by factor 3. My script was detecting timeout and killing one of tests.

I discovered that not successful build was a problem by accident. During experiments I noticed two cases when cobertura has shown source code:

  • I've rerun job and removed all steps but one responsible for publishing coloratura results
  • I run whole job such way it run a single test case which passed

Not sowing coverage if build is not successful is reasonable (if test failed then most probably wrong branch of code has been taken), but UI should indicate that in different way.

Conclusion

This is great example how it is important to report errors to user with precise details what went wrong and why. I wasted at least whole weak to figure out what is actually wrong which bullet point of error message is actually my case. In fact error message from plugin doesn't cover all reasons of not showing the code.

I will file report that plugin should give better explanation what went wrong.