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
- Create a new solution,
- Add a C# assembly project named UnderCover,
- Add a class JamesBond defined as follows:
public class JamesBond
public void Covered()
public void UnCovered()
- Add a C# console application project name UnderCover.Console.exe
- Edit the Main entry method as follows:
static void Main(string args)
JamesBond james = new JamesBond();
- Compile in debug mode
Note: NCover needs the symbol files (.mdb) in order to work, so you need to work with the debug version.
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.
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
NCover.Console v1.3.3 - Code Coverage Analysis for .NET - http://ncover.org
Command Args: UnderCover.dll
******************* Program Output *******************
***************** 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:
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:
- Add Reflector.TreeMap.dll as a Addin of Reflector,
- Load UnderCover.dll,
- Right-click on UnderCover assembly and choose Coverage TreeMap,
- Right-click on the TreeMap and load the Coverage.xml file
- Enjoy the results:
The solution of this tutorial is available in the download section of www.dotnetwiki.org .