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!

This entry was posted in How-To's and tagged , , , , . Bookmark the permalink.

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

  1. Baga says:

    We tried with IIS and got below error. I was running the commands under admin mode. Any idea?
    [JetBrains dotCover] Coverage session finished but no snapshots were created.
    One more question, is it possible to get coverage of a specific application hosted in IIS?

  2. Baga says:

    If we run two sessions of dotcover(iisexpress) simultaneously. We get valid report only for the first stopped session. For the second session we get a blank html report.

  3. thulasi says:

    Coverage session finished but no snapshots were created. error i am getting

  4. Yong Yang says:

    Hi,
    I tried to use the newest version in teamCity and try to get coverage of my .net core webapplication, everything went well when I executed locally outside teamCity.
    I set command: dotcover analyze-everything /Output=coverage.html /ReportType=HTML /Instance=888 as one of the build step in teamcity, but each time when execute this step. The agent also exited with code 0 and not wait for dotcover send command to get snapshot So, I tried run like this :
    PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command “”; (coverStart.ps1 with same dotcover command like before) But the same issue occurs. Could anyone please tell me if I did something wrong in teamCity configuration? Or something need add for this newest version agent can keep running when another step call to send command to this agent in teamCity? Thank you so much!

    log:
    [16:12:47] [Step 5/7] PowerShell Executable: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
    [16:12:47] [Step 5/7] Working directory: C:\TeamCity\buildAgent\work\c8a793bdbd68a3d6\build
    [16:12:47] [Step 5/7] Command: C:\Windows\sysnative\WindowsPowerShell\v1.0\powershell.exe
    [16:12:47] [Step 5/7] PowerShell arguments: -NoProfile, -NonInteractive, -ExecutionPolicy, ByPass, -File, C:\TeamCity\buildAgent\temp\buildTmp\powershell449337732755775293.ps1
    [16:12:47] [Step 5/7] Starting: C:\Windows\sysnative\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NonInteractive -ExecutionPolicy ByPass -File C:\TeamCity\buildAgent\temp\buildTmp\powershell449337732755775293.ps1
    [16:12:47] [Step 5/7] in directory: C:\TeamCity\buildAgent\work\c8a793bdbd68a3d6
    [16:12:47] [Step 5/7] Granting necessary permissions
    [16:12:48] [Step 5/7]
    [16:12:48] [Step 5/7] Process exited with code 0
    [16:12:48] [Step 5/7] Publishing artifacts (4s)
    [16:12:48] [Step 5/7] Publishing artifacts (4s)

  5. Daniel Powell says:

    How do I go about using this for non web applications?

  6. Hi Everyone,
    I am trying to make use of dotcover IIS coverage option.

    I am using ‘JetBrains.dotCover.CommandLineTools.2018.2.3’

    Steps i followed is:
    1) make sure no applications where running in the IIS
    2)then i have executed the command on the application server
    dotCover.exe cover-iis /Output=coverageReport_2.xml /Instance=3
    3)made some actions on another client machine
    4)Now i have ran a command
    dotCover.exe send /Command=GetSnapshotAndKillChildren /Instance=3

    Now i got an xml but it is full of some unreadable symbols. SO this didnt help me

    Now what i did is repeated the same steps with output=CoverageReport_3.dcvr .
    got the file but it is showing empty.

    Based on this my question are:
    1) Am i doing any wrong sequence of execution to get the coverage report?
    if so what should i correct.
    2) In the server, we have multiple websites, so how should i target a specific website on which i need the code coverage?

    Could someone please kindly help me to guide on this. as of now no clue and seeking your help on this

  7. Rajkumar Uppala says:

    Thank you and I appreciate your quick response Alexey Totin.

    Could you please advise me whether the steps which i followed is as expected or not?
    If it is, then i can contact the dotCover support to help me
    If not, Please correct what went wrong.

    Thanks
    Rajkumar.

  8. Rajkumar Uppala says:

    Hi Alexey Totin,
    I have raised a ticket with Jetbrains support:
    https://dotnettools-support.jetbrains.com/hc/en-us/requests/1787102

    If anyone has any suggestions, please share in that . So that it will be useful for me to refer and implement them.

    Thanks,
    Rajkumar.

Leave a Reply

Your email address will not be published. Required fields are marked *