I wanted to get some code coverage metrics into our Jenkins environment, so that as well as unit testing, and running FxCop and StyleCop I could see which bits of code our unit tests were exercising and make sure that our code is being tested automatically as much as possible.

It took a bit of working out, but here’s what I used and where I’ve got it - documented here because it’s taken me half a day to assemble this house of cards:

  • OpenCover - being used as the coverage checker
  • OpenCoverToCoverturaConverter - a tool to convert the xml output from OpenCover into a format recognised by Cobertura (for which there is a Jenkins plugin)
  • Cobertura plugin - used to give a nice progress chart showing (hopefully) increases in coverage over time
  • ReportGenerator - a tool to convert the xml output from OpenCover into clickable HTML showing line by line usage - used to show the current build’s usage in an informative manner
  • HTML Publisher plugin - used to publish the HTML output from ReportGenerator into a way that can be accessed via the Jenkins dashboard

Listed below are the, slightly anonymised, jenkins instructions:

Windows batch commands

OpenCover.Console.exe -target:"C:\Program Files\NUnit 2.6\bin\nunit-console.exe" -targetargs:"/nologo /noshadow %JOB_NAME%.nUnit" -filter:"+[%JOB_NAME%]* -[*Core*]* -[nunit-*]* -[*]*.*WS*.*" -register:user -output:outputCoverage.xml -hideskipped:Filter

  • this uses the nUnit file named after the Jenkins job as it’s input, filters to include the necessary dll, exclude any of the Core, nUnit or WebService generated code. The output will be stored at outputCoverage.xml

OpenCoverToCoberturaConverter\OpenCoverToCoberturaConverter.exe -input:outputCoverage.xml -output:outputCobertura.xml -sources:%WORKSPACE%

  • taking the outputCoverage.xml file generated by OpenCover and convert it into outputCobertura.xml using the source code in the %WORKSPACE% folder

ReportGenerator.exe -reports:outputCoverage.xml -targetDir:CodeCoverageHTML

  • take the outputCoverage.xml file and convert it into HTML files and store them into a folder CodeCoverageHTML - the directory needs to exist first

Plugins

In the “Publish Cobertura Coverage Report” plugin, I’ve set xml report pattern to be outputCobertura.xml and have then adjusted all the metrics as seemed appropriate

In the “Publish HTML reports” plugin, I’ve set HTML directory to archive to be CodeCoverageHTML, given it an “index.htm” index page and a Report Title of “Code Coverage - including source code”

I appreciate that this is as dull as dishwater to most people, but I’ve relied on blog posts like this, this and this stack overflow question to get this far, and thought I’d share my steps as well for future explorers