Analyzing .Net applications in Azure DevOps (a.k.a. Visual Studio Team Services – short VSTS) and sending the results to SonarQube was pretty easy – but with .NET Core it has become quite a challenge.
Upate
I found a better solution that uses Opencover. It runs on Windows and Linux ans also displays a nice report in Azure DevOps and is a yml build file. See .NET Core Code Coverage Done Right for more.
Modify .csproj file
First of all you need to modify the project files you want to analyze. You have to add a ‘ProjectGuid’ element. This is a new guid.
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> <ProjectGuid>{B1EDAD5E-A5B7-400D-A37F-B3C455013C9D}</ProjectGuid> </PropertyGroup> </Project>
You can generate it with the following line:
[Guid]::NewGuid()
If you don’t want to analyse your test projects you just don’t add the attribute there.
Add “Prepare analysis on SonarQube” task to your pipeline
Add the task to your pipeline and configure your endpoint. For the code coverage to work you have to add the following attribute under Advanced:
Configure test task
In the test task you have to add –collect:”Code Coverage” for the task to add a logger for code coverage.
Convert Code Coverage Files
This is the tricky part. The test task only generates .coverage files for each test project. But SonarQube needs a .coveragexml and does not understand the .coverage file format. To convert the file you have to call CodeCoverage.exe with the (undocumented) parameter /analyse. I’ve created a PowerShell script for that.
$ver = (Get-ChildItem $env:USERPROFILE\.nuget\packages\Microsoft.CodeCoverage | Select-Object -Property Name | Sort-Object -Descending | Select-Object -First 1).Name Get-ChildItem -Recurse -Filter "*.coverage" | % { $outfile = "$([System.IO.Path]::GetFileNameWithoutExtension($_.FullName)).coveragexml" $output = [System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($_.FullName), $outfile) "Analyse '$($_.FullName)' with output '$output'..." . $env:USERPROFILE\.nuget\packages\microsoft.codecoverage\$ver\build\netstandard1.0\CodeCoverage\CodeCoverage.exe analyze /output:$output $_.FullName } "Done"
You can add this script in a PowerShell task as a inline script. Make sure the WorkingDirectory is the $(Agent.TempDirectory).
So that’s it.
Your .NET Core analysis should now show up in SonarQube including Code Coverage.
Thank you mike it makes easer to understand the combination Sonar with .Net Core
Great find on the Analyse parameter! I was up to building our current .NET Core (2.2) project in VSTS for Sonar code coverage and noticed an xml codecoverage.exe /analyze conversion would be required. Was looking for ‘natural’ options or parameters to be included in-line. This should do the trick! Thx!
Worked like a champ, but I started getting the following messages in the quality gate step:
[SQ] Error retrieving analysis: [SQ] API GET ‘/api/qualitygates/project_status’ failed, status code was: 404
[SQ] Could not fetch analysis for ID ‘AWjon3Y-mpDlC2QdzEVE’
Using VSTS 2018. The code coverage shows up now (awesome, thanks). Would like to eliminate the reason why it can’t find the fetch analysis ?
Do you use SonarCloud or SonarQube? Du you have the correct steps in the build?
Hey Mike, using SonarQube 6.7.6. Running TFS 2018 (express). Everything running on localhost. Thanks again for the solution BTW…
Try to create a new SonarQube connection. Could this be a SSL issue? https://stackoverflow.com/questions/50906497/sonarqube-v-4-3-0-vsts-task-publish-analysis-result-throw-error-could-not-fet
Possible.. I’m searching google and stumbled across some references to the SSL. It is just kind of weird in that I did not get this when I ran build prior to implementing the fix for code coverage… The 404 not found is kind of odd..
Thanks again…
I went to SonarCloud.io – it’s absolutely worth the money. Keeping the SonarQube and all plugins up to date is a pain. If you don’t have legal issues I suggest you give it a try. Pull-Request-Decoration also works 😉
Thanks a lot Mike.
please post a blog on CI & CD .
What du you want to know?
Want to know more about the build pipeline and release pipeline using azure devops and tfs . I am bit confuse on the both. please help
OK. I will try to deliver 🙂
Can you give solution for Ubuntu 1608 Agent ?
Have you tried the solution here: https://writeabout.net/2019/04/27/net-core-code-coverage-done-right/
Hello MIke,
I am trying to follow the article to get test coverage on Sonar Cube server, However, I am not able to proceed correctly as the image links in above post are broken. Can you please help here. Thanks a lot.
Did you check https://writeabout.net/2019/04/27/net-core-code-coverage-done-right/ ? Also – which link is broken?
Hi i used same steps but code coverage is showing in pipeline but code coverage is 0% in sonarqube server url. Please tell me what am missing.