This is an extract from an internal How To document I wrote for our .netFusion development framework documentation.
- Download NCover and extract to C:\Program Files\NCover
- Download NCoverGUI and extract to C:\Program Files\NCoverGui
- Replace NCover\Coverage.xsl and NCoverGui\Coverage.xsl with Yves Lorphelin’s improved stylesheet (as the default stylesheet is not very user-friendly)
Code Coverage:
- From the green bars in NUnit, we know we have passing tests – but how much of our Stack implementation is actually being tested? Start C:\Program Files\NCoverGui\NCoverGui.exe and configure it as in the screenshot below:
NCover is a command line code coverage tool that profiles a running application using the .NET CLR profiling API. In the case above we are using NCover to monitor assemblies that are being called by NUnit.
- Application to Profile – this should be the path to the NUnit console application
- Working Directory – this is pretty self explanatory
- Application Arguments – these are the arguments that are going to be passed to the NUnit console application
- Assemblies to watch – this lists the assemblies that will actually be profiled by NCover. This has to be the Name of the Type rather than the name of the assembly thus, in the example above Conchango.UnitTest.Example is entered rather than Conchango.UnitTest.Example.dll. All assemblies listed must have debugging information (.pdb files) available in the same directory as the assembly. If you select All that have debug symbols – you will get coverage information about the unit tests as well as the assembly you are unit testing
- Display coverage report when complete – this lists the location and the name of the report to be generated
If you wanted to run NCover from the command line, the equivalent to the options above is:
NCover.Console /o "C:\Projects\Conchango\BrownBagSessions\Conchango.UnitTest.Example.Fixtures\Example.Fixtures.Coverage.xml" /c "C:\Program Files\NUnit 2.2\bin\nunit-console.exe" "C:\Projects\Conchango\BrownBagSessions\Conchango.UnitTest.Example.Fixtures\bin\Debug\Conchango.UnitTest.Example.Fixtures.dll" /a "Conchango.UnitTest.Example"
Where
- /o means output the report to thefollowing file
- /c means launch the application to be profiled. In the case about the application is NUnit, which in turn runs the Conchango.UnitTest.Example.Fixtures.dll (which references Conchango.UnitTest.Example.dll)
- /a means the assemblies to profile, so this will be Conchango.UnitTest.Example NOTE: the assembly name is case sensitive.
The above will produce the following output:

- Navigate to C:\Projects\Conchango\BrownBagSessions\Conchango.UnitTest.Example.Fixtures\Example.Fixtures.Coverage.xml to browse the output file.
Back to NCoverGui: Click the Profile button – there should be a short pause, a command prompt will be launched; this will run NUnit in console mode and then exit. A new web browser should appear with the coverage test results:
The coverage report shows that only 80% of the code is being executed by the unit tests we have written – the Top method is the culprit
The Visits column highlights in red the lines of code have not been visited during the execution of the unit tests. If you navigate to line 27 in Stack.cs you’ll see that
return this.elements[0];
}
are the lines that haven’t been executed as our test throws a InvalidOperationException and then exits the method, without calling these two lines.
Although this is very informative, tabbing between Visual Studio, and IE it’s not a very productive way of looking at code coverage – there is another tool called NCoverBrowser which give a more integrated view of the coverage results. Download, unzip and run the application.
- Select File > Open and then select the coverage report C:\Projects\Conchango\BrownBagSessions\Conchango.UnitTest.Example.Fixtures\Example.Fixtures.Coverage.xml
The screenshot below shows the result, note how
return this.elements[0];
is underlined in red:
This is a much clearer method for monitoring the code coverage results.