Analyze Coverage of Web Apps with Command Line Tool
If you want to automate coverage of a web application (for example, in continuous integration), you can use the dotCover command-line tool.
Unlike manual coverage, where you get coverage snapshots by clicking the Get Snapshot button, the command-line tool gets a snapshot only once the profiled process correctly finishes its work. Thus, to get a snapshot, you should either correctly stop IIS in the system or run a second instance of the command-line tool and explicitly send the "get snapshot and kill process" command to the first running instance. How does the second instance know about the first one? It's quite simple: when running the first instance, you specify its ID using the /Instance=<ID>
argument. Then you can use this ID to send commands to the instance.
The coverage process is performed somewhat differently depending on your coverage target and whether IIS or IIS Express is used to host the application. There are three main coverage scenarios:
Get coverage of a web application (IIS Express/IIS).
Get coverage of a web application (IIS Express)
Using the dotCover command-line tool, start IIS Express and your application under coverage. For example:
dotCover.exe cover /TargetExecutable="C:\Program Files (x86)\IIS Express\iisexpress.exe" /TargetArguments=/site:MyWebApp /systray:false /Output=coverageReport.xml /ReportType=XML /Instance=1Here
Instance
is the ID of the current dotCover.exe instance. You will use this ID to send commands to this command-line tool.The
/systray:false
argument makes sense in case you launch more than one IIS Express instance in parallel.In the covered application, go through the desired scenario or execute tests that do this.
Run the second command-line tool instance that sends the "get snapshot and kill process" command to the first running instance:
dotCover.exe send /Command=GetSnapshotAndKillChildren /Instance=1Here
Instance
is the ID of the first dotCover.exe instance you have specified in step 1.After you send the command, IIS Express and dotCover.exe instances will be stopped and the coverage snapshot will be saved.
Get coverage of a web application (IIS)
Make sure you have administrative privileges on the server.
Using the dotCover command-line tool, start coverage of the IIS service. For example:
dotCover.exe cover-iis /Output=coverageReport.xml /ReportType=XML /Instance=1Here
Instance
is the ID of the current dotCover.exe instance. You will use this ID to send commands to this command-line tool.In the covered application, go through the desired scenario or execute tests that do this.
Run the second command-line tool instance that sends the "get snapshot and kill process" command to the first running instance:
dotCover.exe send /Command=GetSnapshotAndKillChildren /Instance=1Here
Instance
is the ID of the first dotCover.exe instance you have specified in step 2.After you send the command, the IIS service and dotCover.exe instances will be stopped and the coverage snapshot will be saved.
Get coverage of tests if the application and tests are run separately
Make sure you have administrative privileges on the server.
Make sure the application is not already running in an instance of IIS Express or IIS application pool.
Run the application (skip this step if you want to get coverage of the application as well).
Using the dotCover command-line tool, start coverage of subsequent .NET processes. For example:
dotCover.exe cover-everything /Output=coverageReport.xml /ReportType=XML /Instance=1Here
Instance
is the ID of the current dotCover.exe instance. You will use this ID to send commands to this command-line tool.Note that in the
cover-everything
mode, the command-line tool will get coverage data on all managed processes that are run after it.[Only if you skipped step 3] Run the application.
Run tests.
Run the second command-line tool instance that sends the "get snapshot and kill process" command to the first running instance:
dotCover.exe send /Command=GetSnapshotAndKillChildren /Instance=1Here
Instance
is the ID of the first dotCover.exe instance you have specified in step 4.After you send the command, IIS Express and dotCover.exe instances will be stopped and the coverage snapshot will be saved. Note that if the application was run in step 5 instead of step 3, the console runner will get two snapshots (one for the application and one for the tests) and merge them into a single one.
Get coverage of tests if the application is run by tests
Using the dotCover command-line tool, start the tests under coverage. For example, if you use NUnit:
dotcover.exe cover /TargetExecutable="C:\Program Files\NUnit 2.6\bin\nunit-console.exe" /TargetArguments=C:\MyWebApp\bin\Debug\AppTests.dll /Output=coverageReport.xml /ReportType=XML /Instance=1Here
Instance
is the ID of the current dotCover.exe instance. You will use this ID to send commands to this command-line tool.Test assembly setup must start the application in IIS Express / IIS app pool.
Test assembly teardown must run the command:
dotCover.exe send /Command=GetSnapshotAndKillChildren /Instance=1Here
Instance
is the ID of the first dotCover.exe instance you have specified in step 1.After the command is sent, IIS Express / IIS app pool, dotCover.exe instances, and the process that executes the tests will be stopped and the coverage snapshot will be saved.