Unable to get code coverage results from Azure DevOps into SonarCloud project

I am using SonarScanner for Azure DevOps extension and followed this documentation https://sonarcloud.io/documentation/analysis/scan/sonarscanner-for-azure-devops/ to setup my first build integration.

I have a MSBuild task that builds a .Net Framework solution and a VsTest (2019) task to run NUnit with code coverage enabled and I see no errors being reported by the build.

The log from the VsTest task includes this:

...
Results File: c:\agent\_work\_temp\TestResults\auser_bld-vm_2020-09-21_18_08_09.trx
Attachments:
  c:\agent\_work\_temp\TestResults\ceb8b574-c5a2-4d72-b511-57ceaae237f1\auser_bld-vm 2020-09-21 18_07_54.coverage
Test Run Successful.
Total tests: 4
    Passed: 4
 Total time: 16.4561 Seconds
Vstest.console.exe exited with code 0.
**************** Completed test execution *********************
Test results files: c:\agent\_work\_temp\TestResults\auser_bld-vm_2020-09-21_18_08_09.trx
...

In the Run Code Analysis task I see this in the logs that seems to suggests the coverage report was imported into SonarCloud successfully as I presume the .coveragexml was found (or it would raise an error):

...
INFO: Importing 6 Roslyn reports
INFO: Sensor C# [csharp] (done) | time=9674ms
INFO: Sensor C# Tests Coverage Report Import [csharp]
INFO: Parsing the Visual Studio coverage XML report c:\agent\_work\_temp\TestResults\ceb8b574-c5a2-4d72-b511-57ceaae237f1\auser_bld-vm 2020-09-21 18_07_54.coveragexml
INFO: Adding this code coverage report to the cache for later reuse: c:\agent\_work\_temp\TestResults\ceb8b574-c5a2-4d72-b511-57ceaae237f1\auser_bld-vm 2020-09-21 18_07_54.coveragexml
INFO: Coverage Report Statistics: 174 files, 171 main files, 171 main files with coverage, 3 test files, 0 project excluded files, 0 other language files.
INFO: Sensor C# Tests Coverage Report Import [csharp] (done) | time=1762ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=114ms
INFO: SCM Publisher is disabled
...

I also see in the build summary page the code coverage results file, but in the SonarCloud project I see 0.0% code coverage being reported.

Any advice of what I should check in order to find a resolution to this problem?

Hi @alexvaccaro and welcome to the community !

If possible, can you check that the file paths in the coveragexml file matches the directory structure that you physically had for your analysis ?

Mickaël

Hi @mickaelcaro,

Thanks, I am new to SonarCloud so I would need all the help I can get :wink:

As far as I can see the files match, in the *.coveragexml I see:

      <source_files>
        <source_file id="0" path="c:\agent\_work\1\s\My.Project\Controllers\BusinessFacade.cs">

And in the log of my Checkout to s step I see:

c:\agent\_work\1\s\My.Project\Controllers\:
...
Getting BusinessFacade.cs
...

Not sure if this helps but this is the start of the log for Run Code Analysis:

SonarScanner for MSBuild 4.10
Using the .NET Framework version of the Scanner for MSBuild
Post-processing started.
10:34:55.33  Fetching code coverage report information from TFS...
10:34:55.33  Attempting to locate a test results (.trx) file...
10:34:55.424  Looking for TRX files in: c:\agent\_work\1\TestResults
10:34:55.424  No test results files found
10:34:55.518  Did not find any binary coverage files in the expected location.
10:34:55.518  Falling back on locating coverage files in the agent temp directory.
10:34:55.518  Searching for coverage files in c:\agent\_work\_temp

The coverage reports are in fact under c:\agent\_work\_temp\TestResults

OK, I think I was misled by the overall figure, but I can see I am getting the code covered when looking at the class file.

SonarCloud is counting it against the whole solution and is showing an overall coverage of 0.0% but in fact navigating to the class being tested it is showing a 13% coverage, while Azure DevOps\VsTest seems to be calculating it against the project and showing 6.3% coverage overall, i.e. not taking into account the code in the whole solution since other projects have no tests covering them.

That is still weird to me. For sure we noticed some differences between the % calculated between Azure DevOps and SonarCloud, but the fact that it’s showing 0% is strange.

Is the coverage shown in AzDo related to the same classes that you have on SonarCloud by chance ?