Friday, July 16, 2004

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

  1. Create a new solution,
  2. Add a C# assembly project named UnderCover,
  3. Add a class JamesBond defined as follows:
    using System;
    namespace UnderCover
    {
        public class JamesBond
        {
            public void Covered()
            {
                Console.WriteLine("Covered");
            }
            public void UnCovered()
            {
                Console.WriteLine("UnCovered");
            }
        }
    }
  4. Add a C# console application project name UnderCover.Console.exe
  5. Edit the Main entry method as follows:
    using System;
    namespace UnderCover.Cons
    {
        class Class1
        {
            [STAThread]
            static void Main(string[] args)
            {
                JamesBond james = new JamesBond();
                james.Covered();
            }
        }
    }
  6. 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
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:

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:

  1. Add Reflector.TreeMap.dll as a Addin of Reflector,
  2. Load UnderCover.dll,
  3. Right-click on UnderCover assembly and choose Coverage TreeMap,
  4. Right-click on the TreeMap and load the Coverage.xml file
  5. Enjoy the results:

Downloads:

The solution of this tutorial is available in the download section of www.dotnetwiki.org .

posted on Friday, July 16, 2004 12:54:00 PM UTC  #    Comments [13]
Tracked by:
"re: Choosing between Ncover and Ncover" (Rob van der Meijden) [Trackback]
Monday, June 06, 2005 5:48:46 PM UTC
Geek Noise
Monday, June 06, 2005 5:48:46 PM UTC
Make sure you take a look at the "NCover Reloaded" on this blog :)
Jonathan de Halleux
Monday, June 06, 2005 5:48:47 PM UTC
where can i get the correct version of NCover.Console.exe which is related to this tutorial.
<br>
<br>Regards,
<br>Ramki
Ramakrishnan
Monday, June 06, 2005 5:48:47 PM UTC
hi,
<br>
<br>when i execute this command
<br>
<br>&quot;C:\Program Files\NCover\NCover.Console.exe&quot; /c &quot;UnderCover.Cons.exe&quot; &quot;UnderCover.dll&quot; /v
<br>
<br>iam getting IndexoutOfBound exception. can someone help me on this.
<br>
<br>Regards,
<br>Ramki
Ramakrishnan V S
Monday, June 06, 2005 5:48:50 PM UTC
Hi Ramki,
<br>
<br>The latest source is currently in the NCover SVN repository (not on GotDotNet), I don't know when Peter plans to make a release. I think the best solution would be to post a message on the NCover site and ask him about.
<br>
<br>Cheers,
<br>Jonathan
Jonathan de Halleux
Monday, June 06, 2005 5:48:53 PM UTC
Could you ask your questions on the NCover forum please :)
<br>
<br>Thanks, you will surely receive better feedback there...
Jonathan de Halleux
Monday, June 06, 2005 5:48:54 PM UTC
I've also written a NCover tutorial, called
<br>
<br>&quot;Making Code Coverage more palatable with NCoverViewer and NCoverBrowser&quot;:
<br>
<br><a target="_new" href="http://blogs.conchango.com/howardvanrooijen/articles/304.aspx">http://blogs.conchango.com/howardvanrooijen/articles/304.aspx</a>
<br>
<br>/Howard
<br>
Howard van Rooijen
Monday, June 06, 2005 5:48:54 PM UTC
Rob van der Meijden
Monday, June 06, 2005 5:48:54 PM UTC
Rob van der Meijden
Monday, June 06, 2005 5:48:55 PM UTC
Hi,
<br> Where do I find <a title="Reflector" href="http://www.aisto.com/roeder/dotnet/" target="_blank">Reflector</a>.TreeMap.dll to download?
<br>
<br>Thanks,
<br>Gerald Leslie
Gerald Leslie
Monday, June 06, 2005 5:48:55 PM UTC
This dll has been merged into <a title="Reflector" href="http://www.aisto.com/roeder/dotnet/" target="_blank">Reflector</a>.Framework.
<br>
<br>You can get it from <a target="_new" href="http://projectdistributor.net">http://projectdistributor.net</a> (in Peli's Farm).
Jonathan de Halleux
Monday, June 06, 2005 5:48:56 PM UTC
where can i get the correct version of NCover.Console.exe which is related to this tutorial.
Giochi
Monday, June 06, 2005 5:48:56 PM UTC
Hi
<br>Can u tell me where can i get the new version of the coverage.xsl file which gives the drilldown features in the coverage file.
Biswa
Comments are closed.