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

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