5
votes

I have set up a pipeline for my .NET Core project in Azure Devops using the '.NET Core with SonarCloud' template. When I build the analysis gets run in SonarCloud but with 0% Code coverage (I have tests in my solution).

No matter what configuration tweaks I make to the build I cannot get the code coverage working.

What am I missing?

I came across this article and https://dejanstojanovic.net/aspnet/2019/may/publishing-code-analysis-to-sonarcloud-from-azure-build-pipeline/ implemented the powershell script described in it but still I get no code coverage in SonarCloud

I tried using coverlet as described here but still no joy https://gunnarpeipman.com/aspnet/azure-devops-code-coverage/

My pipeline consists of the following tasks

  • .NET Core - Restore
  • Prepare Analysis Configuration
  • .NET Core - Build
  • .NET Core - Test
  • Run Code Analysis
  • Publish Quality Gate Result

My test task is configured:

Arguments: --configuration $(BuildConfiguration)

Publish test results and code coverage - checked

In the console of the Run Code Analysis task I get:

10:43:54.7  Fetching code coverage report information from TFS...
10:43:54.702  Attempting to locate a test results (.trx) file...
10:43:54.753  Looking for TRX files in: C:\\TFSBuilds\\TJPYHG04-GHJ01\\_work\\475\\TestResults
10:43:54.755  No test results files found
10:43:54.81  Did not find any binary coverage files in the expected location.
10:43:54.811  Falling back on locating coverage files in the agent temp directory.
10:43:54.812  Searching for coverage files in C:\\TFSBuilds\\TJPYHG04-GHJ01\\_work\\_temp
10:43:54.814  No coverage files found in the agent temp directory.
2
Can you get coverage locally with coverlet? If so, I would try to put a coverlet step and produce a lcov file to be collected by Sonar. Additionally, Sonar needs a ProjectGuid tag in csproj if you build your project instead of the entire solution, as you can see here: docs.sonarqube.org/display/SCAN/…rogerdossantos
I do have ProjectsGuids set up. When I implemented coverlet in the test task I did get the coverage results displaying in the console of the test task and said it published those results but I couldn't get SonarCloud to pick them up (in the console it said it couldn't find them). No matter what configuration I set up they were published to the temp folder and SonarCloud was always looking in the testresults folder.Tracey Penberthy

2 Answers

11
votes

hope this answer still relevant to you.

Recently I have similar problem as you and I am also using Azure DevOps for my case.

This is how I solved it.

Step 1 - Change directory into your unit testing sub folder (same as the unit-testing .csproj file) and run the following dotnet command.

dotnet add package coverlet.msbuild

Step 2 - Add followings into SonarCloudPrepare Task Additional Properties or append directly into the yml file (if you're using yml instead of classic editor)

    extraProperties: |
      sonar.exclusions=**/obj/**,**/*.dll
      sonar.cs.opencover.reportsPaths=$(Build.SourcesDirectory)/**/coverage.opencover.xml
      sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx

The directory is up to your choice to configure.

Or you can also create a file at your repo titled sonar-project.propertiesand store all the relevant SonarCloud properties inside.

Step 3 - Add followings into your dotnet test task

- task: DotNetCoreCLI@2
  inputs:
    command: 'test'
    arguments: '--configuration $(BuildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --logger trx'
    testRunTitle: 'dotnet test'

You may notice there's a tickbox for "Publish test results and code coverage" but I still prefer using /p:CollectCoverage=true.

You may also test locally to run the dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --logger trx command and coverage.opencover.xml will be generated at your unit testing folder.

Refer to reference 2 & 3 for more parameters and their description.

Side Note: If you're doing any Sonar test inclusion properties, note that Sonar.Tests and Sonar.Test.Inclusions their Test(s) are different. lol

References:

  1. Using SonarCloud in Azure pipelines
  2. Analysis Parameters (official docs)
  3. Test Coverage & Execution (official docs)

Hope this helps :)

1
votes

Not getting code coverage in SonarCloud from an Azure Devops .NET core build

This issue may caused by vstest output path changed recently:

The output path of the vstest coverage file change from D:\a\1\s\TestResults\... to D:\a\_temp\...

Which broke subsequent scripts in the pipeline (like, codecoverage.exe to convert to xml and later import to sonarqube).

Microsoft suggest that use the rest APIs to check for the test artefacts and re-download them to the build agent.

More investigation on this issue, you can check the thread Azure DevOps (VSTS) extension no longer import coverage and unit tests automatically for the issue tracking.

Fortunately, SonarSourcer team have just release new versions of the SonarQube (v4.6.3) and SonarCloud (v1.6.3) extensions to address the coverage issue and the regression.

Hope this helps.