SonarScanner.MSBuild Code Coverage always 0%

I have a problem running SonarScanner.MSBuild with a .Net project and a unit test associated to it.

This is my configuration:

# Begin SonarScanner Session
SonarScanner.MSBuild.exe begin /o:"..." /k:"..." /d:sonar.host.url="https://sonarcloud.io" /d:sonar.token="..." /d:sonar.cs.vstest.reportsPaths="D:\TestSolution\Results\TestResults.trx"

# Build the Target Project
MSBuild.exe "D:\TestSolution\TargetProject\TargetProject.csproj" -t:Rebuild -p:Configuration=Release

# Build the Test Project
MSBuild.exe "D:\TestSolution\TestProject\TestProject.csproj" -t:Rebuild -p:Configuration=Release

# Run the Test Project
vstest.console "D:\TestSolution\TestProject\bin\Release\TestProject.dll" /EnableCodeCoverage /ResultsDirectory:"D:\TestSolution\Results" /Logger:"trx;LogFileName=TestResults.trx"

# End SonarScanner Session
SonarScanner.MSBuild.exe end /d:sonar.token="..."

The builds of both projects don’t have any problem.

vstest.console runs the tests as expected and generates the trx file and the CodeCoverage report too.

SonarScanner reads the Test Results and it shows this message:

INFO: Parsing the Visual Studio Test Results file 'D:\TestSolution\Results\TestResults.trx'.
INFO: Sensor C# Unit Test Results Import [csharp] (done) | time=22ms
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=40ms

Everything seems ok but when I go on Sonarcloud the coverage is still 0%

I have already looked at the documentation for .NET test coverage in SonarCloud but I don’t understand where is the problem. Please let me how to troubleshoot it

Hey there.

What do the logs specifically say about test coverage?

As noted in How to find logs about importing code coverage, look for these messages in your logs.

C# Tests Coverage Report Import [csharp]

13:02:28.534 INFO: Sensor C# Tests Coverage Report Import [csharp]
13:02:28.535 DEBUG: Analyzing coverage with wildcardPatternFileProvider with base dir 'D:\TestSolution\.' and file separator '\'.
13:02:28.542 DEBUG: Pattern matcher extracted prefix/absolute path 'D:\TestSolution\TestResults' from the given pattern 'D:\TestSolution\TestResults\**\*.xml'.
13:02:28.603 DEBUG: Gathering files for wildcardPattern '**\*.xml'.
13:02:28.604 DEBUG: Pattern matcher returns '2' files.
13:02:28.605 DEBUG: The current user dir is 'D:\TestSolution'.
13:02:28.605 INFO: Parsing the Visual Studio coverage XML report D:\TestSolution\TestResults\4320afcd-3bc4-4b59-b888-0c32b37e1662\admin2_SR-WEB-TEST2_2024-03-29.13_02_13.xml
13:02:28.618 INFO: Adding this code coverage report to the cache for later reuse: D:\TestSolution\TestResults\4320afcd-3bc4-4b59-b888-0c32b37e1662\admin2_SR-WEB-TEST2_2024-03-29.13_02_13.xml
13:02:28.619 DEBUG: The current user dir is 'D:\TestSolution'.
13:02:28.619 INFO: Parsing the Visual Studio coverage XML report D:\TestSolution\TestResults\admin2_SR-WEB-TEST2_2024-03-29_13_02_13\In\SR-WEB-TEST2\admin2_SR-WEB-TEST2_2024-03-29.13_02_13.xml
13:02:28.626 INFO: Adding this code coverage report to the cache for later reuse: D:\TestSolution\TestResults\admin2_SR-WEB-TEST2_2024-03-29_13_02_13\In\SR-WEB-TEST2\admin2_SR-WEB-TEST2_2024-03-29.13_02_13.xml
13:02:28.745 DEBUG: Analyzing coverage after aggregate found '3' coverage files.
13:02:28.745 DEBUG: Skipping 'D:\TestSolution\TestProject\DataAccessLayer.cs' as it is a test file.
13:02:28.745 DEBUG: Skipping 'D:\TestSolution\TestProject\UnitTestEventBridgeClient.cs' as it is a test file.
13:02:28.746 DEBUG: Skipping 'D:\TestSolution\TestProject\UnitTestSQSClient.cs' as it is a test file.
13:02:28.746 DEBUG: The total number of file count statistics is '3'.
13:02:28.749 INFO: Coverage Report Statistics: 3 files, 0 main files, 0 main files with coverage, 3 test files, 0 project excluded files, 0 other language files.
13:02:28.750 WARN: The Code Coverage report doesn't contain any coverage data for the included files. Troubleshooting guide: https://community.sonarsource.com/t/37151
13:02:28.750 INFO: Sensor C# Tests Coverage Report Import [csharp] (done) | time=216ms

I analyzed the coverage files and I found the target project is included in the skipped modules with the reason ```
no_symbols

I checked and pdb files are available and not empy. 
Also changing -p:Configuration to Debug doesn't help me.

It sounds like that’s the root issue you need to fix. SonarQube just reads the coverage reports – it isn’t involecd in generating them.