sonarqubesonarcloudsonarscanner

How to send .coverage file to SonarCloud


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


Solution

  • 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