Can't configure Test coverage for .Net Framework projects

I’m trying to configure test coverage to be send into SonarQube. But I’m not sucess and don’t know what to do with it.

Brief description of project structure:
Solution contains 4 projects and 1 unit test project

  • 2 projects using .Net Core 2
  • 2 projects using .Net Framework 4.8
  • 1 Unit test project using .Net Framework 4.8

I tried to use code below to reach result, but it’s doesn’t give any result.

SonarScanner.MSBuild.exe begin /k:"*****************" 
            /"${{ secrets.SONAR_HOST_URL }}" 
            /d:sonar.login="${{ secrets.SONAR_TOKEN }}" 
MsBuild.exe /t:Rebuild
**dotnet test {project}/UnitTests/UnitTests.csproj**
SonarScanner.MSBuild.exe end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"

Can you help my to configure it correct?


Welcome to the community!

Based on the docs, it doesn’t look like you’re passing --collect "Code Coverage" to your test run.


Hey @ganncamp
I have tried dotnet test UnitTests.dll --collect "Code Coverage". This command runs unit test, but not collect coverage and show messages

Data collection : Unable to find a datacollector with friendly name 'Code Coverage'.
Data collection : Could not find data collector 'Code Coverage'

I google this message and found that when call dotnet test command with .dll it can not collect coverage. Command should run using .sln or .cproj
When I run command dotnet test UnitTests.cproj --collect "Code Coverage" I can’t see any useful information in the GitAction log and test coverage doesn’t send into SQ


Creating the coverage report is actually out of scope for us. The docs provide a pointer as a courtesy. If it’s not working for you, you’ll need to investigate elsewhere.


I have found solution for my issue

I have instaled coverlet.console using comand dotnet tool install --global coverlet.console

Collect code coverage in my case looks next:

SonarScanner.MSBuild.exe begin /k:"charitycheckout_charity-checkout_AYDBr1yaTYI48PPBBb8O" /"${{ secrets.SONAR_HOST_URL }}" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.cs.opencover.reportsPaths=coverage.xml
MsBuild.exe /t:Rebuild
coverlet .\CC.UnitTests.dll --target "dotnet" --targetargs "test --no-build" -f=opencover -o="coverage.xml"
SonarScanner.MSBuild.exe end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"

Changes were, added path to the code coverage stats file for the sonar scaner begin command /d:sonar.cs.opencover.reportsPaths=coverage.xml
Run command to collect coverage
coverlet .\CC.UnitTests.dll --target "dotnet" --targetargs "test --no-build" -f=opencover -o="coverage.xml"

1 Like