SonarQube test coverage .NET 5
Asked Answered
B

3

16

I want to show test coverage for my .NET 5 unit tests in my local SonarQube instance (on Windows).

dotnet sonarscanner begin /k:"MyProject" /d:sonar.host.url="http://localhost:9000"  /d:sonar.login="<token>" /d:sonar.cs.opencover.reportsPaths="**\TestResults\*\*.xml"

dotnet build

dotnet test --no-build --collect:"XPlat Code Coverage"

dotnet sonarscanner end /d:sonar.login="<token>"

The dotnet test command generates the coverage reports as coverage.cobertura.xml files in the <TestProjectDir>.TestResults\<some-guid>\ folder.

In the logs I can see the following warning: WARN: Could not import coverage report '<MyTestProject>\TestResults\a4af5812-7f80-469b-8876-3ea0c7c4c98d\coverage.cobertura.xml' because 'Missing root element <CoverageSession> in C:\Users\<Path>\TestResults\a4af5812-7f80-469b-8876-3ea0c7c4c98d\coverage.cobertura.xml at line 2'. Troubleshooting guide: https://community.sonarsource.com/t/37151

Following the link from the warning message, I can see that only Visual Studio Code Coverage, dotCover and OpenCover/Coverlet are supported. As far as I can tell from their GitHub page, is OpenCover/Coverlet is "XPlat Code Coverage".

In my test projects the coverlet.collector NuGet package v 3.0.3 is installed.

What am I missing?

I found this related question: SonarQube: Unable to import test coverage but it doesn't help me because I can't use dotCover.

Bellyband answered 14/4, 2021 at 7:4 Comment(1)
Coverlet generates a report in cobertura format by default (as you can see in file name)Simplex
B
21

After a lot of trial and error, here's the solution that worked for me.

I had to install the lastest version of dotnet-sonarscanner and dotnet-reportgenerator-globaltool for this to work. I already had the report generator installed, but needed to update it to use the SonarQube report type.

dotnet tool install --global dotnet-sonarscanner --version 5.2.0
dotnet tool update dotnet-reportgenerator-globaltool -g --version 4.8.7

With the reportgenerator the cobertura files can be converted to the SonarQube format.

dotnet sonarscanner begin /k:"MyProject" /d:sonar.host.url="http://localhost:9000"  /d:sonar.login="<token>" /d:sonar.coverageReportPaths=".\sonarqubecoverage\SonarQube.xml"

dotnet build

dotnet test --no-build --collect:"XPlat Code Coverage"

reportgenerator "-reports:*\TestResults\*\coverage.cobertura.xml" "-targetdir:sonarqubecoverage" "-reporttypes:SonarQube"

dotnet sonarscanner end /d:sonar.login="<token>"

Report generator supported file formats: https://github.com/danielpalme/ReportGenerator#supported-input-and-output-file-formats

SonarQube generic test data format: https://docs.sonarqube.org/latest/analysis/generic-test/

Bellyband answered 14/4, 2021 at 12:21 Comment(0)
P
20

I think the problem is you are trying to import opencover tests but the tests are in the cobertura format. You can do this command instead for your tests:

dotnet test --no-build --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover

And it would likely work.

Pitcher answered 13/10, 2021 at 4:49 Comment(3)
Works fine! This should really be mentioned in the official sonarqube docsScullery
Link to the documentation on passing the format: github.com/coverlet-coverage/coverlet/blob/master/Documentation/…Dropkick
I think the same can be achieved by passing format parameter like this: --collect "XPlat Code Coverage;Format=opencover". Tested with .NET 6 and coverlet.collector version 3.1.2.Capricorn
Y
0

I have generated a coverage report by using "Dotnet-Coverage" technique.
Reference Link: https://docs.sonarqube.org/latest/analyzing-source-code/test-coverage/dotnet-test-coverage/
Note: Here, I have a Visual studio and have a project in .Net 6.

Execute the below command one by one. (Open your project in Visual studio > View > Terminal)

dotnet sonarscanner begin /k:"replace_with_your_project_Key" /d:sonar.login="replace_with_your_secret" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml

dotnet build --no-incremental

dotnet-coverage collect 'dotnet test' -f xml  -o 'coverage.xml'

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

Note: "replace_with_your_project_Key" & "replace_with_your_secret" - Replace with the data under http://localhost:9000/

Yankee answered 31/3, 2023 at 13:53 Comment(2)
Hi, and how do you processed if tests are not in the same folder as the main project?Carbonado
Thanks for the comment. For me, I am keeping a test case project and a source project under the same solution/folder. So it works for me. But if you have a different folder then you can reference a DLL as mentioned under the coverlet section in this link - docs.sonarqube.org/latest/analyzing-source-code/test-coverage/… Hope it might works! But not sure. Thank YouYankee

© 2022 - 2024 — McMap. All rights reserved.