DotNet core code coverage

DotNet core code coverage 0 tests 0%

Hi guys !
I have some trouble with my code coverage :slight_smile:

CI process

  • dotnet sonarscanner begin /k:“project-key”
  • dotnet build my.sln
  • dotnet test myTest.csproj
  • powershell script to convert .coverage to coveragexml
  • dotnet sonarscanner end

other information

I can see my trx file from the VSTest and the coveragexml file.

Sonarqube say that he correctly find my test.

My CI pipeline is under Jenkins.

I also follow the topic on this community website

I still have zero test in my sonarqube coverage dashboard…

Any idea ?

You scanner begin should include the code coverage property

  • dotnet sonarscanner begin /k:“project-key” /d:sonar.cs.vscoveragexml.reportsPaths=<projectdirectory>\TestResults\*.coveragexml
  • dotnet build my.sln
  • dotnet test myTest.csproj --collect:“Code Coverage”
  • powershell script to convert .coverage to coveragexml
  • dotnet sonarscanner end
2 Likes

Well :slight_smile:

I made some more change. There is all the code

& dotnet sonarscanner begin /k:"ProjectName" /d:sonar.cs.vscoveragexml.reportsPaths="*.coveragexml" /d:sonar.cs.vstest.reportsPaths="*.trx" /d:sonar.verbose=true /d:sonar.log.level="TRACE" /d:sonar.host.url="sornarqube_url" /d:sonar.login="secret_key"`

& dotnet build "myproject.sln"
    
& dotnet test "mytests.csproj" --collect "Code Coverage" --logger trx --results-directory "TestsResults"
   
Get-ChildItem -Recurse -Filter "*.coverage" | Foreach-Object {
    $outfile = "$([System.IO.Path]::GetFileNameWithoutExtension($_.FullName)).coveragexml"
    $output = [System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($_.FullName), $outfile)
    & $env:CodeCoveragePath\CodeCoverage.exe analyze /output:$output $_.FullName 
}
    
Get-ChildItem -Recurse -Filter "*.coveragexml" | Foreach-Object { 
    $file = Join-Path -Path $_.Directory -ChildPath $_.Name
    Copy-Item $file -Destination 'TestsResults'
}
    
& dotnet sonarscanner end /d:sonar.login="secret_key"

Look “nice” coverage is not so good but I have a percent value :slight_smile:

But I still have 0 Unit Test discovered :confused:
my bad : path to the trx was wrong

Hi again,

In fact I have the correct Unit Tests number but no one are listed.
The UT come from VSTest export as trx

This is related to issue #886 - the SQ plugin is not pushing the necessary information to SQ to display the UTs.

Hum… sad,

I try to convert the trx file to a valid junit xml report.
The report look fine.

I try to add it using : sonar.junit.reportPaths but doesn’t work.

Is it only working with a compatible sonarqube language defined ?

Well, can’t wait the fix for that.
I wrote a small ps script to convert the trx sml to the Generic Test Data xml requested by sonarqube.

Thanks guys

1 Like

Here’s a really simple working example:
https://seanke-dev.blogspot.com/2020/06/sonarqube-code-coverage-azure-devops.html