1
votes

I have a SonarQube installation and am running it against a .Net core application with multiple xUnit projects. The SonarQube picks up the bugs and smells fine but like so many people, I can't get the code coverage to complete.

At present, I am generating cobertura coverage XML files using the following command:

dotnet test --collect:"XPlat Code Coverage"

I then copy these to a centralized directory from each of the test projects and use the following command to run/import the files:

dotnet SonarScanner begin /k:"my-project" /d:sonar.cs.vscoveragexml.reportsPaths=".\TestResults\*.xml"
dotnet build
dotnet SonarScanner end

According to the logs, the files are found but are not ingested.

INFO: Parsing the Visual Studio coverage XML report C:...\TestResults\5.coverage.cobertura.xml WARN: Could not import coverage report '..\TestResults\5.coverage.cobertura.xml' because 'Missing root element in C:...\TestResults\5.coverage.cobertura.xml at line 2'

I've confirmed that the files contain valid XML that looks to be correct, but I'm not even sure that SonarQube is supposed to accept cobertura reports.

  • Is my approach the way others have gone when trying to get xUnit coverage reports into SonarQube?
  • Is there a better way? I'm happy to use OpenCover or similar if that is easier.
1

1 Answers

1
votes

In case anyone comes across this in the future, I gave up trying to use the built in coverage in VS and used DotCover. Bit of an easier setup and now seem to have it working to a degree.

  • Download/extract dotCover and add the folder to Path
  • dotCover.exe --output=AppCoverageReport.html --reportType=HTML dotnet -- test
  • dotnet SonarScanner begin /k:"my-project" /d:sonar.cs.dotcover.reportsPaths=AppCoverageReport.html
  • dotnet build
  • dotnet SonarScanner end

Still having an issue where the test coverage gathering seems to be pulling in too much and as a result is causing an error. Investigating but not related to this question.