.NET Core, SonarQube and Code Coverage

Analyzing .Net applications in Azure DevOps (a.k.a. Visual Studio Team Services – short VSTS) and sending the results to SonarQube was pretty easy – but with .NET Core it has become quite a challenge.

Upate

I found a better solution that uses Opencover. It runs on Windows and Linux ans also displays a nice report in Azure DevOps and is a yml build file. See .NET Core Code Coverage Done Right for more.

Modify .csproj file

First of all you need to modify the project files you want to analyze. You have to add a ‘ProjectGuid’ element. This is a new guid.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <ProjectGuid>{B1EDAD5E-A5B7-400D-A37F-B3C455013C9D}</ProjectGuid>
  </PropertyGroup>
</Project>

You can generate it with the following line:

[Guid]::NewGuid()

If you don’t want to analyse your test projects you just don’t add the attribute there.

Add “Prepare analysis on SonarQube” task to your pipeline

Add the task to your pipeline and configure your endpoint. For the code coverage to work you have to add the following attribute under Advanced:

Prepare Analysis

Configure test task

In the test task you have to add –collect:”Code Coverage” for the task to add a logger for code coverage.

Collect Code Coverage

Convert Code Coverage Files

This is the tricky part. The test task only generates .coverage files for each test project. But SonarQube needs a .coveragexml and does not understand the .coverage file format. To convert the file you have to call CodeCoverage.exe with the (undocumented) parameter /analyse. I’ve created a PowerShell script for that.

$ver = (Get-ChildItem $env:USERPROFILE\.nuget\packages\Microsoft.CodeCoverage | Select-Object -Property Name | Sort-Object -Descending | Select-Object -First 1).Name

Get-ChildItem -Recurse -Filter "*.coverage" | % {
$outfile = "$([System.IO.Path]::GetFileNameWithoutExtension($_.FullName)).coveragexml"
$output = [System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($_.FullName), $outfile)
"Analyse '$($_.FullName)' with output '$output'..."
. $env:USERPROFILE\.nuget\packages\microsoft.codecoverage\$ver\build\netstandard1.0\CodeCoverage\CodeCoverage.exe analyze /output:$output $_.FullName
}
"Done"

You can add this script in a PowerShell task as a inline script. Make sure the WorkingDirectory is the $(Agent.TempDirectory).

2018-09-22 10_32_53-

So that’s it.
Your .NET Core analysis should now show up in SonarQube including Code Coverage.

2018-09-22 10_34_28-Projects

19 thoughts on “.NET Core, SonarQube and Code Coverage

  1. Great find on the Analyse parameter! I was up to building our current .NET Core (2.2) project in VSTS for Sonar code coverage and noticed an xml codecoverage.exe /analyze conversion would be required. Was looking for ‘natural’ options or parameters to be included in-line. This should do the trick! Thx!

  2. Worked like a champ, but I started getting the following messages in the quality gate step:

    [SQ] Error retrieving analysis: [SQ] API GET ‘/api/qualitygates/project_status’ failed, status code was: 404
    [SQ] Could not fetch analysis for ID ‘AWjon3Y-mpDlC2QdzEVE’

    Using VSTS 2018. The code coverage shows up now (awesome, thanks). Would like to eliminate the reason why it can’t find the fetch analysis ?

      1. Hey Mike, using SonarQube 6.7.6. Running TFS 2018 (express). Everything running on localhost. Thanks again for the solution BTW…

    1. Possible.. I’m searching google and stumbled across some references to the SSL. It is just kind of weird in that I did not get this when I ran build prior to implementing the fix for code coverage… The 404 not found is kind of odd..
      Thanks again…

      1. I went to SonarCloud.io – it’s absolutely worth the money. Keeping the SonarQube and all plugins up to date is a pain. If you don’t have legal issues I suggest you give it a try. Pull-Request-Decoration also works 😉

      1. Want to know more about the build pipeline and release pipeline using azure devops and tfs . I am bit confuse on the both. please help

  3. Hello MIke,
    I am trying to follow the article to get test coverage on Sonar Cube server, However, I am not able to proceed correctly as the image links in above post are broken. Can you please help here. Thanks a lot.

  4. Hi i used same steps but code coverage is showing in pipeline but code coverage is 0% in sonarqube server url. Please tell me what am missing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s