Code Coverage and Azure DevOps Build Pipeline

Code Coverage and Azure DevOps Build Pipeline

·

2 min read

The Azure DevOps Build Pipeline YAML was a pain to get Code Coverage reports generating correctly. After much Googling and looking at the actual dotnet test options, I had overlooked the --collect argument.

If this is set to "Code Coverage", the default .coverage files are generated which can be imported via Visual Studio. What I wanted was the Cobertura.xml to be generated which requires the value for the collect argument to be "XPLAT Code Coverage".

    - task: DotNetCoreCLI@2
      displayName: 'Run Tests'
      inputs:  
        command: test
        projects: '**/*[Tt]ests/**/*.csproj'
TestCategory!=Integration&TestCategory!=E2E --collect:"XPlat Code Coverage" /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura'
        publishTestResults: true

    - task: reportgenerator@4
      displayName: 'Generate Code Coverage Report'
      inputs:
        reports: '$(Agent.TempDirectory)\**\coverage.cobertura.xml'
        targetdir: '$(Build.SourcesDirectory)\TestResults\CoverageReport'
        sourcedirs: '$(Build.SourcesDirectory)'

    - task: PublishCodeCoverageResults@1
      displayName: 'Publish Code Coverage Results'
      inputs:
        codeCoverageTool: 'Cobertura'
        summaryFileLocation: '$(Agent.TempDirectory)\**\coverage.cobertura.xml'
        #reportDirectory: '$(Build.SourcesDirectory)/**/Coverage'

Note we could also specify the following output path for coverage.cobertura.xml file(s):

/p:CoverletOutput=.$(Build.SourcesDirectory)/TestResults/CodeCoverage/

This wasn't tested since I wasn't sure if it would generate unique subdirectories or for each test's coverage results.

I also opted to use the Report Generator available from Azure Marketplace.

CodeCoverageReport.png

Working on getting that code coverage percentage up.

Full example gist found here

Next steps code coverage trend report...

References: