This tutorial shows the basic usage of NCover from GotDotNet. The tutorial starts by creating a toy solution, how to set-up NCover execution, analysis and improvements of the results.
A toy solution
using System; namespace UnderCover { public class JamesBond { public void Covered() { Console.WriteLine("Covered"); } public void UnCovered() { Console.WriteLine("UnCovered"); } } }
using System; namespace UnderCover.Cons { class Class1 { [STAThread] static void Main(string[] args) { JamesBond james = new JamesBond(); james.Covered(); } } }
Our objective is now to compute the coverage of UnderCover.dll when UnderCover.Console.exe is executed. It is straightforward to see that JamesBond.Covered will be fully covered while JamesBond.UnCovered will be not covered.
UnderCover.dll
JamesBond.Covered
JamesBond.UnCovered
Setting a NCover batch file
In this step, we create a simple batch file in the bin/debug directory to execute NCover.Console.exe with the command line. The NCover command line takes the command line to execute + the assembly to cover as parameters:
"C:\Program Files\NCover\NCover.Console.exe" /c "UnderCover.Cons.exe" "UnderCover.dll" /v
The output of NCover is as follows
"C:\Program Files\NCover\NCover.Console.exe" /c "UnderCover.Cons.exe" "Under Cover.dll" /v NCover.Console v1.3.3 - Code Coverage Analysis for .NET - http://ncover.org Command: UnderCover.Cons.exe Command Args: UnderCover.dll Working Directory: Assemblies: Coverage File: Coverage Log: ******************* Program Output ******************* Covered ***************** End Program Output ***************** Copied 'C:\Program Files\NCover\Coverage.xsl' to '...\Coverage.xsl'
If everything executed correctly, a Coverage.xml and Coverage.xsl has appeared in the directory.
First look at the results
Open Coverage.xml in your browser and you will get something like this:
The coverage has worked, we have the expected results.
Better XSLT template
Now, this works fine for a simple assembly but the files becomes huge if you have a normal project so we need a better XSLT template. MbUnit has it's own NCover Coverage.xsl template (that you can get here) that supportes expand/collapse and computes the percents of coverages. Let us copy the new coverage.xsl to the directory:
coverage.xsl
Now this looks much better :)
Results in Reflector
As I showed in a previous post, the Reflector Code Coverage Addin can help you visualize the coverage in a more intuitive way. The steps to follow are:
Downloads:
The solution of this tutorial is available in the download section of www.dotnetwiki.org .
Page rendered at Thursday, December 04, 2008 6:50:27 AM UTC
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.