khaoula ismail
khaoula ismail

Reputation: 41

Code coverage and number of unit test are not showing in sonarQube

I am using sonarQube to analyze my code that in .net6. I've installed sonarScanner and run these 3 commands:

dotnet sonarscanner begin /k:"<sonar-project-key>"
    /d:sonar.login="<sonar-token>"
    /d:sonar.cs.dotcover.reportsPaths=dotCover.Output.html
dotnet build –no-incremental
dotnet dotcover test --dcReportType=HTML
dotnet sonarscanner end /d:sonar.login="<sonar-token>"

I wrote a lot of unit tests using nunit however the number of unit tests is 0

1

I tried to add /d:sonar.cs.nunit.reportsPaths="Nunit\Result.xml" but nothing happened

Can you please help to reveal the missing configuration?

Upvotes: 4

Views: 1160

Answers (2)

Devesh
Devesh

Reputation: 4550

In case you want to use the global tool, here are the steps

  1. dotnet tool install --global coverlet.console
  2. Pass these parameters to sonar scanner along with others /d:sonar.cs.opencover.reportsPaths=coverage.opencover.xml /d:sonar.cs.vstest.reportsPaths=<PATH to This File>TestResult.trx
  3. Execute coverlet <Path to this DLL>SpeQue.Service.Test.dll --target "dotnet" --targetargs "test <Directory of the Test> --no-build --logger:\"trx;LogFileName=TestResult.trx\"" --format opencover

Note: It is vital to associate sonar.cs.vstest.reportsPaths with the trx file to show the number of unit test cases. Coverage % will be pulled from coverage.opencover.xml

Upvotes: 0

Jonatas
Jonatas

Reputation: 108

I fixed that using Coverlet and exporting to OpenCover format.

Make sure these packages are added to your xunit test project:

coverlet.collector
coverlet.msbuild

To scan:

dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover  /p:CoverletOutput="../../" --logger:"trx;LogFileName=..\..\..\TestResult.trx"

dotnet sonarscanner begin /k:"test" /d:sonar.host.url="http://localhost:9000"  /d:sonar.token="XXX" /d:sonar.cs.opencover.reportsPaths="coverage.opencover.xml" /d:sonar.cs.vstest.reportsPaths=TestResult.trx

dotnet build

dotnet sonarscanner end /d:sonar.token="XXX"         

Note: in this example the unit tests is located on "\tests\Project.Tests\Project.Tests.csproj", so the output will be placed on "\". This is also the path where the scanner is run.

enter image description here

Upvotes: 1

Related Questions