Isolating NCover code coverage collection within a command session

With the version 4 release, NCover now monitors programs for coverage rather than making the user jump through hoops to launch their program in just the right way to get coverage. For simple testing, the difference is probably not very noticeable, but for software running on test or build clusters NCover 4 makes it much easier to gather coverage.

In this new model, you specify which programs you’d like coverage on, and NCover does all the work of making sure that happens. Even so, there are users who want to be more surgical about the way they collect coverage data. They would like to open a command prompt session and have coverage collected on specific programs within that session. Or they want to do the same thing for a build server where multiple builds are happening concurrently.

The first step is to disable the global NCover profiler. This can be accomplished simply by removing 4 global environment variables from the computer in question.

After NCover 4 has been installed, you will find the following global environment variables defined on the machine.

CORECLR_ENABLE_PROFILING=1
CORECLR_PROFILER=NCover.ProfilerLauncher
COR_ENABLE_PROFILING=1
COR_PROFILER=NCover.ProfilerLauncher

To disable the global profiler, simply remove those entries from the global environment. This can be accomplished through the System Properties dialog by clicking the “Environment Variables” button and removing those entries from the System Variables section of the dialog.

Please note, once you disable the global profiler settings, NCover will only operate in the manner that we talk about in the rest of this article. You will not be able to just set rules and have coverage collected. To restore global coverage monitoring, just restore the global environment variables to their original settings.

Next, to collect coverage locally with NCover, you can use the “NCover Run” command. To run coverage on the NUnit test runner, for example, you could use a command line that looks something like this.

ncover run --project='MyProject' -- nunit-console.exe mytest.dll

The NCover Run command will set up the environment locally within the command prompt so that the processes that are launched by NCover Run will be monitored for coverage. The NCover run command does require a project to be setup as well. So in this example, you must have ‘MyProject’ setup in the ncover interface with the appropriate match rules to gather coverage on nunit-console when it launches. (More on the NCover Run command here.)

Let’s say you have a build server configured to collect coverage data with NCover. Maybe you are building 10 different applications with accompanying unit tests, etc… Using NCover Run in your build script to run your tests will allow you to have tighter control over which test processes are selected for coverage and which projects will be used to store the resulting data.

Another Note: These command updates are present in NCover version 4.1.2099.921 and beyond. Please make sure you upgrade your copy of NCover before trying it out.