2

i'm new to sonar cloud and my company implmented it in AzureDevOps Pipeline. The problem we are facing is that, to know our code coverage we have to create a pull request and build the solution in devops for the code to get analysed.

So i'm trying to do this locally, i installed SonarQube and SonarScanner and when i run the commands provided by sonarQube documentation it runs without problems, but when i check the sonarcloud page my project as 0% code coverage. I think i am missing one step in the commands i run but i am not able to find the solution.

The commands i run are:

    dotnet sonarscanner begin /k:"project-key" /d:sonar.login="myToken" /o:"myOrg"
    dotnet build "myPathTo .sln"
    dotnet test --collect "Code Coverage" (this step creates the .coverage file in my UnitTest project)
    dotnet sonarscanner end /d:sonar.login="myToken" 

After the commands the page gets updated but not with code coverage

enter image description here

Can you guys help me with the missing step?

Thanks in advance

Miguel Vale
  • 59
  • 11

2 Answers2

1

Sonar only looks for Coverage file in very specific places (or it does on our version). When we do the build on our .NET core projects we have to tweak with the output for the scanner to pick it up like this (I know this is an Azure pipeline but the commands are the same):

.NET Core Sonar

As you can see from the arguments we re-route the coverage file to the TestResults folder in the Azure pipeline under the working folder.

It might be your issue, it might not be, but you should see the paths the tools looking into during the Code analysis Phase:

Code Analysis

If it says something like "No coverage files found" it means the file's not included in the paths it's looking into. Look into any output files, or turn on verbose logging and see what it's doing.

This is what got it working for us. This only appears to happen on .NET core projects, .NET framework Solutions seems to be picked up by default. Later versions of Sonar may have changed this.

Tom Ruyter
  • 176
  • 2
  • 9
  • amazing help, im almost there now, i just need to grant permission to some specific user that i cant guess witch, because i am getting this warning in the log Could not import coverage report 'C:\TestResults' because 'java.io.FileNotFoundException: C:\TestResults (Access is denied)'. Do you know witch user is running the commands that needs ther permission? – Miguel Vale Oct 22 '21 at 13:27
  • No idea, but you could always set the permissions on the 'everyone' object on that folder to read / write and allow it through that way. – Tom Ruyter Oct 22 '21 at 13:30
1

enter image description here

So i managed to do it. In the "Begin" command i was missing the directory where the file will live. And also on the "Test" command i was missing the output directory to match the fist one. After that i was able to upload the file as you can see in the image.

I have just a problem since i have to point to the exact file and this is not dynamic at all, each time i have to edit the command to point to the correct file location

My commands now are:

dotnet sonarscanner begin /k:"myKey" /d:sonar.login="myToken" /o:"myOrg" /d:sonar.cs.vscoveragexml.reportsPaths="C:\TestResults\DotnetCoverage.coveragexml"

dotnet build "pathToSLN"

dotnet test --collect "Code Coverage" --results-directory "C:\TestResults"

CodeCoverage.exe analyze /output:"C:\TestResults\DotnetCoverage.coveragexml" "C:\TestResults\c887677b-b89e-4222-93e4-09e563b48b7a\randomGeneratedFileName.coverage"

dotnet sonarscanner end /d:sonar.login="myToken" 

so as you can see, when i run the code coverage command i have to match the new directory the file was created following ../guidId/randomGeneratedFileName.coverage

Miguel Vale
  • 59
  • 11
  • This got me close to a working solution. I also had to add the 'coverlet.msbuild' nuget library to my Test project and then things worked! – Louis Cribbins Apr 20 '22 at 17:40