This article enhances the one where I explained the steps of generation and publication code coverage in Azure DevOps pipeline. This time we go further and implement “shift-left” strategy by introducing SonarQube in Azure DevOps CI/CD process. Our task here will be not only configure code analysis in SonarQube but also get code coverage metrics so we can use it in quality gates and follow “clean as you code” principle.
These two steps of the pipeline will perform tests, generate code coverage results and publish code coverage results in Azure DevOps pipeline in “Code coverage” tab:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Add coverlet.collector nuget package to test project - 'dotnet add <TestProject.cspoj> package coverlet.collector' - task: DotNetCoreCLI@2 displayName: dotnet test inputs: command: 'test' projects: '**/*.Tests.csproj' arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage"' publishTestResults: true # Publish code coverage report to the pipeline - task: PublishCodeCoverageResults@1 displayName: 'Publish code coverage' inputs: codeCoverageTool: Cobertura summaryFileLocation: $(Agent.TempDirectory)/*/coverage.cobertura.xml # using ** instead of * finds duplicate coverage files |
However, code coverage results will be generated in Cobertura format which is not supported by SonarQube, thus cannot be imported and if you try to import it into SonarQube you will see the following error:
--collect "Code Coverage"
parameter without the need for an explicit report path setting. It will also automatically convert the generated report to XML. No further configuration is required.However, in case of tests in Linux images the option above is unavailable and we need to get code coverage in one of dotCover, OpenCover or Coverlet formats. For that we can use reportgenerator
extension and convert Cobertura report or pass special parameter
1 |
DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format |
to dotnet test
command to get desired format.
I prefer the second option and here is the dotnet test
commmand that will generate code coverage reports in both OpenCover (for SonarQube) and Cobertura (for Azure DevOps) formats:
1 |
dotnet test --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura,opencover |
And now we have two files of different formats and we can pass the Opencover report to SonarQube with
sonar.cs.opencover.reportsPaths parameter.
After that you will get coverage metrics in SonarQube:
Full yml pipeline is now available and can be applied immediately! You can get it here
Good luck!