Code coverage not being displayed on SonarCloud

I have a project in .Net and a unit test associated to it.

When I run SonarScanner everything is ok.

I see the unit test works:

`Starting test execution, please wait…
A total of 1 test files matched the specified pattern.

Passed! - Failed: 0, Passed: 4, Skipped: 0, Total: 4, Duration: 230 ms - USNext.SystemEngine.UnitTest.ExportSystemV7.dll (net6.0)

Attachments:
D:\Platform Development Team\Main\Source\ExportSystemV7\SystemEngine.UnitTest\TestResults\caeecd89-ca1d-4404-839c-3aaaaba5f396\efalchero_WSAMZN-5MANHRAT_2024-03-08.20_03_10.coverage`

The configuration of sonar scanner is simple:

dotnet sonarscanner begin /o:"..." /k:"..." /d:sonar.host.url="https://sonarcloud.io" /d:sonar.token="..." dotnet build SystemEngine/SystemEngine.csproj /t:Rebuild dotnet test SystemEngine.UnitTest/SystemEngine.UnitTest.csproj --collect "Code Coverage" dotnet sonarscanner end /d:sonar.token="..."

but from the log I get these rows:

INFO: Sensor JaCoCo XML Report Importer [jacoco] INFO: 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml INFO: No report imported, no coverage information will be imported by JaCoCo XML Report Importer INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=2ms

Project in sonarcloud cannot get coverage and unit tests, please tell me how to troubleshoot. Is there any problem with any configuration?

Thanks for your support,
Emanuele

Hey there.

Have you looked at the documentation for .NET test coverage in SonarCloud?

There are also specific logs for each language (and coverage importer), you can learn more about what you’re looking for in the logs here:

Thank you so much for your feedback.

I changed the SonarScanner configuration using dotnet-coverage as explained here: .NET test coverage in SonarCloud

dotnet sonarscanner begin /o:"vidrio" /k:"..." /d:sonar.host.url="https://sonarcloud.io" /d:sonar.token="..." /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml
dotnet build SystemEngine/SystemEngine.csproj /t:Rebuild
dotnet-coverage collect "dotnet test SystemEngine.UnitTest/SystemEngine.UnitTest.csproj" -f xml -o "coverage.xml"
dotnet sonarscanner end /d:sonar.token="..."

Now I have a strange result in SonarCloud with this configuration. SonarScanner uploads on SonarCloud both projects, including also the UnitTest, and puts them in two separate subfolders in my SonarCloud project.

I found a workaround changing a little bit the SonarScanner configuration moving the collect coverage before the SonarScanner begin step:

dotnet-coverage collect "dotnet test SystemEngine.UnitTest/SystemEngine.UnitTest.csproj" -f xml -o "coverage.xml"
dotnet sonarscanner begin /o:"vidrio" /k:"..." /d:sonar.host.url="https://sonarcloud.io" /d:sonar.token="..." /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml
dotnet build SystemEngine/SystemEngine.csproj /t:Rebuild
dotnet sonarscanner end /d:sonar.token="..."

Is it correct what I’m doing or do I still miss something? I don’t want the UniTest uploaded in my SonarCloud project.

Hey there.

This sounds expected, not strange. As long as you don’t see Lines of Code being calculated for your test projects, they aren’t being counted against your license.

Thank you,

I excluded the test project from analysis by updating the csproj file and adding the SonarQubeExclude element in the PropertyGroup section.:

  <PropertyGroup>
    <!-- Exclude the project from analysis -->
	<SonarQubeExclude>true</SonarQubeExclude>
  </PropertyGroup>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.