Configure Code Coverage for Dotnet Core 2.0 based applications using SonarQube and Azure DevOps

Using MSBuild tool to get code coverage and configure Azure DevOps pipelines to include code coverage results is an easy task for .NET framework based applications. Azure DevOps (formerly VSTS) contains inbuilt functionality to analyze code coverage files generated and publish results back to VSTS itself. However, it is quite a challenge to get it right and working for .NET Core 2.0 based applications. In this blog post, we’ll cover steps on how to get code coverage results for .NET Core based application using SonarQube and Azure DevOps.

Update Nuget package version for Microsoft.NET.Test.Sdk

For code coverage to be enabled, open .csproj file and modify nuget package version for Microsoft.NET.Test.Sdk to version 15.8.0 or plus.

update package version for microsoft.net.test.sdk

Modify Project files for Dotnet Core Application

This section is repeat of our previous blog post on how to modify project files for analysis by SonarQube. The default project files do not contain a project Guid in .NET Core. Since SonarQube needs this, it will fail to analyze the project files. To fix this, we’ll need to add below code section to .csproj files:


<PropertyGroup>
<!– other properties here –>
<!– SonarQube needs this –>
<ProjectGuid>{6224c484-3e23-4f06-a749-195c1e478110}</ProjectGuid>
</PropertyGroup>

The Guid mentioned above can be randomly generated using any GUID generator. It can also be generated using PowerShell using below code:

Guid::newGuid()

Add “Prepare analysis on SonarQube” task

In the Azure DevOps pipeline, for SonarQube analysis, we first need to add the ‘Prepare analysis on SonarQube’. This task needs to be added before we go ahead and add tasks for dotnet build:

prepare analysis for sonarqube task configuration

As shown above, provide values for the Project Key, Name and Version. Also, in the advanced section of the task, we need to add below line:

sonar.cs.vscoveragexml.reportsPaths=$(Agent.TempDirectory)/**/*.coveragexml

Configure test task

To analyze the test cases, add task for Dotnet Core and configure it to run Test Commands. Also, we need to add –collect:”Code Coverage” to add a logger for code coverage as shown:

include dotnet test task to run test cases and gather data

Convert Code Coverage Files

The test task added above 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. We can use below PowerShell code for same:


Get-ChildItem -Recurse -Filter "*.coverage" | % {
$outfile = "$([System.IO.Path]::GetFileNameWithoutExtension($_.FullName)).coveragexml"
$output = [System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($_.FullName), $outfile)
"Analyse '$($_.Name)' with output '$outfile'…"
. $env:USERPROFILE\.nuget\packages\microsoft.codecoverage\15.8.0\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) as in the prepare task, the path we used is the $(Agent.TempDirectory) in the advanced section.

This code can be either run as a script or inline script using the PowerShell task.

Run the Azure DevOps Pipeline

After adding all required tasks and configuring them, we need to run the Azure Pipeline. Once the pipeline is completed, we should be able to see code coverage results generated as below:

sonarqube code coverge results

10 thoughts on “Configure Code Coverage for Dotnet Core 2.0 based applications using SonarQube and Azure DevOps

  1. Hello Mohit,

    Great article! Helped me a lot.
    Just a clarification – For me the code coverage is always “0” in SonarQube. Followed your instruction step by step, but still no luck. Can you give any suggestion?

    Like

  2. Thanks for beautiful blog which helps to get better understanding to setup CI/CD pipeline along with SonarQube.

    I tried the replicate above steps in my pipeline but some issue on running tests. Below are the error message getting from Azure DevOps

    ##[section]Starting: Test
    ==============================================================================
    Task : .NET Core
    Description : Build, test, package, or publish a dotnet application, or run a custom dotnet command. For package commands, supports NuGet.org and authenticated feeds like Package Management and MyGet.
    Version : 2.150.1
    Author : Microsoft Corporation
    Help : [More Information](https://go.microsoft.com/fwlink/?linkid=832194)
    ==============================================================================
    [command]C:\windows\system32\chcp.com 65001
    Active code page: 65001
    [command]”C:\Program Files\dotnet\dotnet.exe” test D:\a\1\s\dotnetcore-tests\dotnetcore-tests.csproj –logger trx –results-directory D:\a\_temp -–collect ”Code Coverage”
    MSBUILD : error MSB1001: Unknown switch.
    Switch: -–collect

    For switch syntax, type “MSBuild /help”
    ##[error]Error: The process ‘C:\Program Files\dotnet\dotnet.exe’ failed with exit code 1
    ##[warning]No test result files were found.
    ##[error]Dotnet command failed with non-zero exit code on the following projects : D:\a\1\s\dotnetcore-tests\dotnetcore-tests.csproj
    ##[section]Finishing: Test

    Like

      • Thanks Mohit! I removed other arguments and tried It’s works as well. But there’s one challenges I have.

        The pipeline can be push the data of bugs, vulnerability & Code smells to SonarQube bit code coverage, it shows 0%. Can you help with some more lights to see updates on code coverage.

        Like

  3. Can you please help me out, I’ve executed the tutorial step by step. But I’m having the following error in ‘Run Code Analysis’ Task.
    ERROR: Error during SonarQube Scanner execution
    ##[error]java.lang.IllegalStateException: The plugin [communitybsl] does not support Java 1.8.0_222

    Like

  4. Hi I followed step by step but still my code coverage 0.0% may i know why i am facing this issue my project is .net core 3.1 used agent pool is Ubuntu.
    Can you please help me

    Like

Leave a comment