SonarQube

SonarQube, unit tests and code coverage with a .NET Core project in TFS or Azure Devops

SonarQube, UnitTests and CodeCoverage does not work with a .NET Core project out of the box in TFS and Azure DevOps. Here are the extra steps you have to do in your code and in your build:

SonarQube build tasks

Get started by adding the three Sonar tasks – these steps should be added before build and after test.

Project key and project name

You have to provide a name and a key to identify your project in SonarQube.

You also have an option to add additional properties under the advanced tab.
Add the following if you. for example, would like to exclude files in the test and lib directories.

sonar.exclusions=**/*test*

sonar.exclusions=**/lib/**

Enable Code Coverage

Enable code coverage in the test task to get that data to SonarQube.

--collect "Code coverage"

Exceptions/strange stuff

There are some “strange” things you also need to do to get the code coverage and unit tests working when you use .NET Core and SonarQube. I hope this is temporary.

Trx and Coverage files

Trx and Coverage files end up in a temporary folder outside of the build, you need to copy them to the $(Common.TestResultsDirectory) folder. Add the Copy Files task to do this.

$(Agent.HomeDirectory)\_work\_temp

$(Common.TestResultsDirectory)

Project Guids

SonarQube requires project Guids, which is not part of .NET Core projects by default. We add this code below to each csproj file to be analyzed. Make sure to select a different Guid for each project.

<PropertyGroup>

    <!-- SonarQube needs this -->

  <ProjectGuid>{E2CEBBAF-6DF7-41E9-815D-9AD4CF90C840}</ProjectGuid>

</PropertyGroup>

The result in SonarQube

And we’re done! Here’s the final result in SonarQube:

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.