Collecting Coverage from the Command Line for IIS

Collecting Coverage from the Command Line for IIS

command_line_iisWe recently hosted a webinar covering the basic steps for profiling IIS and web applications using NCover.  The session was very well attended and one question that was raised repeatedly by attendees was: “How can I collect coverage from the command line when profiling IIS?” We’ve received this question from users and, in the past, we’ve shared the steps outlined below to individual users. In this post, we’re providing background on this process and sharing specific commands to be used in scripting. Hopefully, these steps will result in a simple, reliable approach to collecting coverage on web applications via IIS.

Some Background

It’s very common for NCover users to automate the collection of coverage on unit tests using the NCover Run command. The advantage of this approach is that an automated solution is often preferable, particularly in a team environment, to ensure that coverage is collected as a standard part of the development process.  Although the NCover Run command works in a variety of situations, it is not the recommended approach when collecting coverage from the the command line for IIS.  There are a couple of reasons for this.  First, running IIS with the NCover Run command from the command line and setting the correct parameters can be challenging.  Timing and number of variables involved leaves little room for error.  Second,  this approach is often outside many users’ experience in general with IIS and the command line environment.

How The Script Works

Instead of relying on the NCover Run command, we recommend using a scripted approach.  It provides the benefits of being relatively simple to setup, allows for easy inclusion in the development process and also provides a clear approach for configuring for your specific needs.  Although this script can be modified to your specific needs, it does provide a solid framework for collecting coverage from the command line for IIS.

For simplicity, we have separated the syntax into two parts.  The first part, the “pre-test” command syntax, includes the key steps of (1) stopping IIS, (2) registering the appropriate NCover profiling DLLs, (3) setting the profiling environment variables prior to executing tests and then (4) starting IIS prior to running your specific tests.

The second part, the ‘post-test’ command syntax, (5) stops IIS and (6) re-sets the profiling environment variables to their defaults.

/// PRE-TEST COMMAND SYNTAX

iisreset /STOP
regsvr32 NCover.Launcher.x86.dll /s
regsvr32 NCover.Coverage.x86.dll /s
IF NOT "%PROCESSOR_ARCHITECTURE%"=="AMD64" goto POSTREG
regsvr32 Debug\NCover.Launcher.x64.dll /s
regsvr32 Debug\NCover.Coverage.x64.dll /s
:POSTREG
setx COR_ENABLE_PROFILING "1"
setx COR_PROFILER "NCover.ProfilerLauncher"
setx CORECLR_ENABLE_PROFILING "1"
setx CORECLR_PROFILER "NCover.ProfilerLauncher"
iisreset /START

///  Do your work here

/// POST-TEST COMMAND SYNTAX

iisreset /STOP
setx COR_ENABLE_PROFILING ""
setx COR_PROFILER ""
setx CORECLR_ENABLE_PROFILING ""
setx CORECLR_PROFILER ""
iisreset /START

It is important to remember that NCover will automatically start coverage collection when the worker process starts and continue collecting coverage until the process exits. Following these steps should enable the user to achieve a relatively high degree of automation.

If you have questions about your specific situation, please submit a support request and we will be happy to help.

Trackbacks

  1. […] Collecting Coverage from the Command Line for IIS (Kerry Meade) […]