How-To's

Analyzing code coverage of web apps with the dotCover 2018.1 Console Runner

These days automating unit tests and application coverage is a routine activity in areas like CI and some complex integration testing scenarios. Seeing how most developers work on web applications, it was really annoying that the dotCover console runner had no straight and easy workflow for getting coverage of web apps running on IIS or IIS Express. We fixed this with the dotCover 2018.1 release, adding a number of new commands to its arsenal!

What was the issue?

  1. There was no good way to start coverage analysis of the IIS service from the command line. While it was not a problem for the standalone dotCover, the console runner lacked an ability to attach to running services. In 2018.1, this is solved by adding four new commands:
    • analyze-service and cover-service: in this mode, the console runner is able to attach to a service by its name and get coverage, e.g.,
      dotCover.exe analyze-service /ServiceName=myService
    • analyze-everything and cover-everything: in this mode, the console runner attaches to any managed process started after it.
  2. In 2017.3 and earlier, the only way to get a coverage snapshot by the console runner was to correctly finish the profiled process. Now, you can run a second instance of the console runner and explicitly send the “get snapshot and kill child processes” command to the first running instance. How does the second instance know about the first one? It’s 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.
    E.g., running the first instance:
    dotCover.exe analyze-everything /Instance=1
    and sending the command from the second one:
    dotCover.exe send /Command=GetSnapshotAndKillChildren /Instance=1

Now, let’s see some more detailed instructions on how you can get coverage of unit tests and web apps running on IIS and IIS Express.

Getting coverage of a web application with IIS Express

  1. Using the dotCover console runner, start IIS Express and your application under coverage. For example:
    dotCover.exe analyze /TargetExecutable="C:\Program Files (x86)\IIS Express\iisexpress.exe"
    /TargetArguments=/site:MyWebApp
    /Output=coverageReport.xml
    /Instance=1

    Here, Instance is the ID of the current dotCover.exe instance. You will use this ID to send commands to this console runner.
  2. In the covered application, go through the desired scenario or execute tests that do this.
  3. Run the second console runner instance that sends the “get snapshot and kill process” command to the first running instance:
    dotCover.exe send /Command=GetSnapshotAndKillChildren /Instance=1
    Here Instance is the ID of the first dotCover.exe instance you have specified in step 1.
  4. After you send the command, IIS Express and dotCover.exe instances will be stopped and the coverage snapshot will be saved.

Getting coverage of a web application hosted in IIS

  1. Make sure you have administrative privileges on the server.
  2. Using the dotCover console runner, start coverage of the IIS service. For example:
    dotCover.exe analyze-service /ServiceName=w3svc
    /Output=coverageReport.xml
    /Instance=1

    Here, Instance is the ID of the current dotCover.exe instance. You will use this ID to send commands to this console runner.
  3. In the covered application, go through the desired scenario or execute tests that do this.
  4. Run the second console runner instance that sends the “get snapshot and kill child processes” command to the first running instance:
    dotCover.exe send /Command=GetSnapshotAndKillChildren
    /Instance=1

    Here, Instance is the ID of the first dotCover.exe instance you have specified in step 2.
  5. After you send the command, the IIS service and dotCover.exe instances will be stopped and the coverage snapshot will be saved.

To get coverage of tests (the app and tests are run separately)

  1. Make sure you have administrative privileges on the server.
  2. Make sure the application is not already running in an instance of IIS Express or IIS application pool.
  3. Run the application (skip this step if you want to get coverage of the application as well).
  4. Using the dotCover console runner, start coverage of subsequent .NET processes. For example:
    dotCover.exe analyze-everything
    /Output=coverageReport.xml
    /Instance=1

    Here, Instance is the ID of the current dotCover.exe instance. You will use this ID to send commands to this console runner.
    Note that in the analyze-everything mode, the console runner will get coverage data on all managed processes that are run after it.
  5. [Only if you skipped step 3] Run the application.
  6. Run tests.
  7. Run the second console runner instance that sends the “get snapshot and kill process” command to the first running instance:
    dotCover.exe send /Command=GetSnapshotAndKillChildren
    /Instance=1

    Here, Instance is the ID of the first dotCover.exe instance you have specified in step 4.
  8. 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.

To test the updated dotCover console runner in action, download the latest ReSharper Ultimate EAP. We’d love to hear your feedback!

image description