Testing Code Coverage With NCover

I ve been developing the recipe book website mostly using TDD (Test Driven Design) and I thought it would be interesting to measure the code coverage of my unit tests. After researching what tools were available it looked like NCover was the best fit for the sites technology stack. The commercial version has some good features, such as measures for cyclomatic complexity, however it is a bit out of my budget (of approximately 0!). Fortunately a community edition of NCover is also available for free and it stills works just fine for analysing current .NET assemblies.

After installing NCover a console application is available for running code coverage analysis. I got this up and running but the command line interface isn t exactly the most user friendly and the output is just a single summary report. I obviously wasn t alone in these conclusions because back in 2006 Grant Drake developed the NCoverExplorer tool to provide a GUI front end to NCover. This tool also has a community edition available (version 1.4) that works with the community edition of NCover.

Starters - Setting up NCover Explorer

The tool is fairly straight forward to setup, the NCover website includes instructions on how to setup the professional edition to profile an NUnit test project, and the settings are equivalent in the community edition. I also chose to specify which assemblies should be included in the coverage report as otherwise all the Sharp Architecture assemblies are included and the results are skewed.

After running the tool you are provided with an interactive view of the assemblies with the uncovered lines displayed in red. You can see how easy it is to identify the offending lines in the screen grab below:

The Main Course Improving code coverage

The results of my first report are shown below:

The prime candidates for improvement were:

  • The RecipeBook.Data assembly. A couple of repository classes were not tested.
  • The RecipeBook.Web.Controllers assembly. Some of the code paths in the controller methods were not covered.
  • The RecipeBook.ApplicationServices assembly. Not tested at all, oops!

By default NCover sets the acceptable coverage level at 95%, however I felt that this was overly strict. I would rather spend the time writing fewer tests that I ensure are of a high quality than writing tests just to cover every single line of code. So I reduced the threshold to 80% and added tests to cover the essential missing features. Below if my much more healthy looking report.

For Desert A report without the red sauce

OK maybe I m stretching the food analogy a little too far with my headings now! By red sauce I mean those nasty red bars that were blazed across nearly every line of the report. Below you can see the report looks much more healthy after adding some unit tests and trimming some unused code:

I will endeavour to keep the coverage level at a minimum of 80% from now, and hopefully add some more tests to bump off those remaining pesky red bars!

As usual, any comments on this post are welcome. :)

Copyright © 2016 - Hook Technologies Ltd - Powered by Octopress