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:

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:

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

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

  1. 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

      1. 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

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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s