I have a dot net core version: '3.0.100', building on 'Ubuntu 16.04' and am trying to get code coverage pushing to our self hosted SonarQube.
I have been using Coverlet to generate Cobertura files which can then use the PublishCodeCoverageResults@1 to publish to the Devops pipelines code coverage viewer.
I haven't been able to push the cobertura.xml file to sonarqube though.
I've read this and it seems to me that the only mention of cobertura is with python and flex. Is it possible to use that file to cover my C# project?
I've been playing around with the following, but suspect what I have in extraProperties is incorrect.
- task: SonarQubePrepare@4
inputs:
SonarQube: 'My SonarQube'
scannerMode: 'MSBuild'
projectKey: 'dcap'
projectName: 'DCAP'
extraProperties: 'sonar.flex.cobertura.reportPaths=**/DCAP.Testing.Unit/TestResults/*/coverage.cobertura.xml'
Thanks :-)
I think the easiest solution is to configure coverlet to output multiple formats (see documentation).
For local testing, this will generate the opencover format for SonarQube and the cobertura format for Azure DevOps:
# >= dotnet 7
dotnet test --collect "XPlat Code Coverage;Format=cobertura,opencover"
# < dotnet 6
dotnet test --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura,opencover
For integrating into your Azure Pipeline
...
- task: SonarQubePrepare@4
displayName: Prepare SonarQube
inputs:
SonarQube: 'SonarQube Enterprise'
projectKey: $(ProjectKey)
projectName: $(ProjectName)
extraProperties: |
sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
sonar.cs.opencover.reportsPaths=$(Agent.TempDirectory)/*/coverage.opencover.xml
...
- task: DotNetCoreCLI@2
displayName: Run tests
inputs:
command: test
arguments: --configuration $(BuildConfiguration) --collect:"XPlat Code Coverage;Format=cobertura,opencover"
Is it possible to use that file to cover my C# project?
I am afraid there is no such out of box property to cover C# project with Cobertura format.
Just as you read, the cobertura is for python and flex. For the C#, we need to use the sonar.cs.dotcover.reportsPaths or sonar.cs.opencover.reportsPaths with the format dotCover or OpenCover.
To resolve this issue, you could try to use a custom powershell script provided by Chameera Dulanga as workaround:
$Env:DOTNET_ROOT= (Get-Command dotnet).Path.Replace('dotnet.exe','sdk\2.1.300')
dotnet tool install dotnet-reportgenerator-globaltool --tool-path . --version 4.0.12
dotnet tool install coverlet.console --tool-path . --version 1.4.1
mkdir .\reports
$testDll = gci -Recurse | ?{ $_.FullName -like ("*bin\{0}\{1}" -f "$(BuildConfiguration)", "$(TestDllFile)") }
$coverlet = "$pwd\coverlet.exe"
& $coverlet $testDll.FullName --target "dotnet" --targetargs ("vstest {0} --logger:trx" -f $testDll.FullName) --format "cobertura"
gci -Recurse |
?{ $_.Name -eq "coverage.cobertura.xml"} |
%{ &"$pwd\reportgenerator.exe" ("-reports:{0}" -f $_.FullName) "-targetdir:reports" "-reportstypes:HTMLInline;HTMLChart" }
You could check his blog Running ASP.NET NUnit Tests in Azure DevOps Build Pipeline and Publishing Results to Sonar Cloud (LINK BROKEN) for some details.
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With