Getting code coverage working in SonarQube using the SonarQube docs
what have you tried so far to achieve this
All 3 different options for dotnet-coverage output formats (.coverage, .xml and .cobertura.xml). None work
On the SonarSource documentation there’s some information on how you should create a coverage file for dotnet with dotnet-coverage. This however, does not work and results in a failure during import.
Here’s the guidance on the docs:
dotnet sonarscanner begin /k:"<sonar-project-key>"
/d:sonar.token="<sonar-token>"
/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.token="<sonar-token>"
When I run this a file is generated which starts as follows:
When SonarQube picks this up in the scanner it shows the following error:
2025-08-04T16:05:37.6203622Z INFO: Parsing the OpenCover report /home/runner/work/DeveDiskSpaceInfo/DeveDiskSpaceInfo/././DeveDiskSpaceInfo.Tests/bin/Release/net9.0/TestResults/f4939be0-c657-4425-8a6c-1dd96060820a.xml
2025-08-04T16:05:37.6492824Z WARN: Could not import coverage report '././DeveDiskSpaceInfo.Tests/bin/Release/net9.0/TestResults/f4939be0-c657-4425-8a6c-1dd96060820a.xml' because 'Missing root element <CoverageSession> in /home/runner/work/DeveDiskSpaceInfo/DeveDiskSpaceInfo/././DeveDiskSpaceInfo.Tests/bin/Release/net9.0/TestResults/f4939be0-c657-4425-8a6c-1dd96060820a.xml at line 2'. Troubleshooting guide: https://community.sonarsource.com/t/37151
The scanner logs indicate that SonarQube is attempting to parse a file at /home/runner/work/DeveDiskSpaceInfo/DeveDiskSpaceInfo/././DeveDiskSpaceInfo.Tests/bin/Release/net9.0/TestResults/f4939be0-c657-4425-8a6c-1dd96060820a.xml as an OpenCover report, not the ‘coverage.xml’ you mentioned generating (which should be parsed as ‘vscoveragexml’ or ‘sonar.cs.vscoveragexml.reportsPaths’ setting).
I would suggest that you start by checking to make sure sonar.cs.opencover.reportsPaths isn’t being set anywhere in your build, or in your .csproj files.
If you’re running dotnet-coverage collect "dotnet test" -f xml, make sure you use sonar.cs.vscoveragexml.reportsPaths instead of sonar.cs.opencover.reportsPaths.
The docs explain that this is because the dotnet-coverage tool’s output matches the Visual Studio Code Coverage format, so that’s the parameter SonarQube expects.
Note that we specify the path to the reports using sonar.cs.vscoveragexml.reportsPaths because this tool’s output format is the same as the Visual Studio Code Coverage tool (see the Test coverage parameters section for information about this parameter). We use the -f xml parameter to specify that the output format is in XML.
Also, keep in mind that you’re using a wildcard like ./DeveDiskSpaceInfo.Tests/bin/Release/net9.0/TestResults/*.xml to specify the report paths. That folder often holds both test execution results and coverage reports, all saved as .xml files. So, it’s better to specify just your coverage.xml file here to avoid any mix-ups.