Code coverage can be quite a helpful tool in your project assessment toolbox, especially for giving you a view of untested areas of code. When I was tasked with generating coverage for our Outlook plug-in written in C#, I looked around and quickly settled on NCover as the tool to use. I chose NCover because it seemed like the most mature solution, already had integration with our test framework (MbUnit via Gallio), and could generate HTML reports and keep track of trends over time.
Integrating NCover with Continuous Integration
Generating XML or HTML reports was as simple as passing a few arguments to Gallio:
C:\Program Files\Gallio\bin\Gallio.Echo.exe "C:\path\to\yourtest.dll" /runner:ncover3 /runner-property:NCoverArguments="//html coveragedir //at ncover3.trend"
This will run the tests in yourtest.dll, output HTML reports in directory named “coveragedir”, and maintain trends over time in the file ncover3.trend. After doing this we had the first glimpse into the coverage of our unit tests. But just having these reports sitting in a folder on our build machine isn’t as useful as it could be. We wanted the HTML reports to be easily accessible from our continuous integration system, Hudson, so that anyone could easily view the coverage of the latest build. Hudson allows us to have all of our projects built and tested on schedules or on commit, and provides a central dashboard giving us a detailed view into many aspects of our projects, as well as notifies us as soon as something breaks.
NCover Plugin for Hudson
Unfortunately, there wasn’t an existing solution for integrating NCover and Hudson. However, as Hudson is open-source, has a friendly community, and a flourishing plug-in system, I decided to write an NCover plug-in for Hudson myself. After refreshing my Java skills and starting with the Javadoc Publisher plug-in as a base, it wasn’t long before I had a functional plug-in. We now have an NCover plug-in that can archive reports generated by NCover after a build and provide a link to them from the project or build page. If you are interested in more than one report, you can list as many as you like and each will show up as a tab on the report page. The plug-in has recently been released in the official Hudson repository and can be installed as easily as any other plug-in.
Installing the Plugin
To install it on your system:
- Visit your dashboard
- Click Manage Hudson -> Manage Plugins -> Available
- Scroll down and check “NCover“
- Click the “Install” button at the bottom
- Restart Hudson to activate the plugin (it should offer to restart itself for you)
Configuring the Plugin
Once Hudson is restarted, go to the configuration page for the project which is using NCover. Scroll down to the “Post-build Actions” section and check “Publisher NCover HTML report”. The settings shown here will display two of the generated reports from the previously configured coverage directory.
After you save your NCover configuration and perform a build, you’ll now have a coverage report in Hudson, assuming your build is generating it!
Below is the main view of our Outlook plug-in, with the Code Coverage option now available at the top. The test results trend is courtesy of the Gallio plug-in, and I’d recommend using that as well if you are using Gallio. If you aren’t using Gallio, I’d recommend checking it out as a wrapper around your current testing framework as Gallio likely supports it.
Now whichever reports you specified in the NCover configuration will appear as tabs, allowing you to publish as many reports as you need. When you are done viewing the reports, simply click the “Back to Hudson” link at the top left. Now you’ve got .NET code coverage integrated with Hudson! Check out the NCover plug-in wiki page for more information and feel free to ask any questions or share any comments here. NCover also wrote about this plug-in on their blog, so check out their post too!