NCover FAQ

If you have questions that this document does not address, contact Peter Waldschmidt.

1. What is code coverage analysis?

A code coverage analyzer monitors your code at runtime and records information about which lines of code were executed. NCover shows each sequence point in your application along with the number of times that point was executed. Sequence points are generated by the compiler and stored in the debug information (.pdb) files. A sequence point basically corresponds to a single program statement (often a line of code) in your high-level language.

2. Why would I want to do code coverage analysis?

Unit test suites are often used as a quality tool during the development process to keep the codebase stable as it changes and expands. Tools such as NUnit are often used to run and report on the test suites. However, when implementing unit testing in your build process, you have no way of knowing how much of your code the unit tests are actually testing. This is where code coverage comes in. You can run NUnit within NCover and use the code coverage report to determine which code was not tested by that particular test suite.

3. What versions of the CLR does NCover support?

At this time, NCover has only been tested on the .NET framework version 1.1.4322.

4. What is the command line syntax for NCover?

Here is the usage info from the NCover command line:

    Usage: NCover /c <command line> [/a <assembly list>]

    /c Command line to launch profiled application.
    /a List of assemblies to profile. i.e. "MyAssembly1;MyAssembly2"
    /v Enable verbose logging (show instrumented code)

- The /c command-line argument is required and specifies the command-line of the .NET application you want to analyze.
- The /a command-line argument specifies the assemblies that you want to analyze. NCover can only analyze assemblies that have .pdb files included with them. If you do not specify the /a argument, NCover will attempt to analyze every loaded assembly that has debug information available.
- The /v command-line argument makes the profiler emit all the original IL and modified IL instructions to the coverage log. This is useful for debugging purposes. Beware that this can make your coverage log file very large!

5. Does NCover required a special compilation step for my code?

No. Some code coverage tools change your source code and force you to recompile it into a special build. NCover is designed to work on shipping code. NCover uses the .NET Framework profiling API to monitor your code. It does require build symbols, but can be run on release code without any modifications.

6. How does NCover work?

NCover uses the .NET Framework profiler API to monitor an application's execution. When a method is loaded by the CLR, NCover retrieves the IL and replaces it with instrumented IL code. NCover does not change your original IL code, it simply inserts new code to update a visit counter at each sequence point. After the .NET process shuts down, the profiler outputs statistics to a file in the current directory.

7. How do I analyze an ASP.NET application with NCover?

xmspc got this to work on ASP.NET. See his post on the message board for more information. Here are the basics for getting it to work on Windows XP.

1) Ensure the NCoverLib COM component is registered (setup does this, but you can manually register it with regsvr32 if you wish).

2) Set the machine-wide COR profiling environment variables (or find some other way to get them set in the aspnet_wp.exe's environment):
COR_ENABLE_PROFILING=1
COR_PROFILER=CvrLib.CoverageProfiler

3) Create %windir%\System32\Coverage.log and %windir%\System32\Coverage.xml and grant .\ASPNET read+write+modify permission to these 2 files only.

4) Force a restart of IIS so aspnet_wp.exe picks up the new environment variables. Coverage info will be written to the System32 directory.

All ASP.Net processes (on Windows XP for sure, don't know about Windows 2003) use %windir%\System32 as their working dir, therefore if multiple processes are running they will stomp over the Coverage files.

...would be nice if NCover used AppDomain basedir and/or value from app config file to set output file dir...

8. What is the output of NCover?

NCover writes three files to the directory after analysis completes.

- Coverage.log - This file is a log of the events and messages from the profiler during the analysis process. Most of the time, error messages are recorded in this log. If you enable verbose logging, the coverage log will contain disassembly of the original and instrumented IL code.
- Coverage.xml - This file is the analysis output of NCover. You can see an example of the output below.
- Coverage.xsl - This file is a simple XML transformation that makes the XML output easily readable.

Example XML output

    <method class="NCoverTest.ClassLoaded" name="HasDeadCode">
        <seqpnt document="C:\Dev\Utilities\ncover\NCoverTest\NCoverTest.cs"
                column="13" line="48" endcolumn="58" endline="48" visitcount="1" />
        <seqpnt document="C:\Dev\Utilities\ncover\NCoverTest\NCoverTest.cs"
                column="13" line="49" endcolumn="22" endline="49" visitcount="1" />
        <seqpnt document="C:\Dev\Utilities\ncover\NCoverTest\NCoverTest.cs"
                column="17" line="50" endcolumn="24" endline="50" visitcount="1" />
        <seqpnt document="C:\Dev\Utilities\ncover\NCoverTest\NCoverTest.cs"
                column="13" line="51" endcolumn="48" endline="51" visitcount="0" />
        <seqpnt document="C:\Dev\Utilities\ncover\NCoverTest\NCoverTest.cs"
                column="9" line="52"  endcolumn="10" endline="52" visitcount="0" />
    </method>

Example transformed output
NCoverTest.ClassLoaded.HasDeadCode

Visit Count | Line | Column | End Line | End Column | Document
1 | 48 | 13 | 48 | 58 | C:\Dev\Utilities\ncover\NCoverTest\NCoverTest.cs
1 | 49 | 13 | 49 | 22 | C:\Dev\Utilities\ncover\NCoverTest\NCoverTest.cs
1 | 50 | 17 | 50 | 24 | C:\Dev\Utilities\ncover\NCoverTest\NCoverTest.cs
0 | 51 | 13 | 51 | 48 | C:\Dev\Utilities\ncover\NCoverTest\NCoverTest.cs
0 | 52 | 9 | 52 | 10 | C:\Dev\Utilities\ncover\NCoverTest\NCoverTest.cs

9. How do you test NCover?

Unfortunately, all the original testing was done with production code that cannot be published. I have started on a test suite called NCoverTest. It is pretty anemic right now, but feel free to chip in and contribute some tests.