.netcode-coveragegitlab-cinunit-consoledotcover

dotcover print coverage results to cmd line


Is there a way to get dotcover to print a summary of the coverage results to the command line?

I am trying to get my gitlab pipeline to output coverage results to the console so they can be picked up by a regex I specified in the pipeline settings. When I run the dotcover command I can only get it to generate a report and not print out anything to the console. I haven't been able to find anything in the docs about printing results to the console.

dotcover cover 
    --targetExecutable="nunit3-console.exe" 
    --targetArguments="..." 
    --output=report.html 
    --reportType=HTML

The above generates the report correctly, but the only coverage output does not include any details about the coverage. I was hoping for something like Coverage Summary: 89% that I could pull out.

[JetBrains dotCover] Coverage session finished [7/27/2020 10:24:09 AM]
[JetBrains dotCover] Coverage results post-processing started [7/27/2020 10:24:09 AM]
[JetBrains dotCover] Report generation started [7/27/2020 10:24:09 AM]
[JetBrains dotCover] Report generation finished [7/27/2020 10:24:10 AM]
[JetBrains dotCover] Coverage results post-processing finished [7/27/2020 10:24:10 AM]

I realize I can use --output=report.json --reportType=JSON instead of HTML and then read the file to get the summary from the first CoveragePercent attribute, but I then I am no longer able to save the HTML report as an artifact.

Edit - here's how I solved this, based on KaiserBones answer

# first run the tests with coverage, generating a snapshot
dotcover cover 
    --targetExecutable="nunit3-console.exe" 
    --targetArguments="..." 
    --output="coverage-results.snapshot"

# then, generate the JSON report 
dotcover report --Source="results.snapshot" --Output="coverage-results.json" --ReportType="JSON"

# then, generate the HTML report (a nice way of examining results manually)
dotcover report --Source="results.snapshot" --Output="coverage-results.html" --ReportType="HTML"

# Read the JSON file (my runners are on windows, so I'm using powershell)
$coverageJson = (Get-Content "coverage-results.json" -Raw) | ConvertFrom-Json

# Print the top level coverage result
Write-Host "Statements: $($coverageJson.CoveragePercent)%"

Solution

  • I don't know if there is a way to do it directly with the cover option.

    The way I would do it though is drop the --reportType flag. This will create a snapshot(.dcvr) that you can then use "dotcover report" to create both an HTML and a JSON report. I haven't used gitlab so I don't know if that will interfere with your archiving process though.

    I do this in Jenkins since I haven't found a dotcover plugin that allows me publish reports and enforce thresholds. I generate an HTML report that I publish and a DetailedXML that I use to set thresholds using an if statement in Powershell.