Setting up Code Coverage data in Azure DevOps Pipeline, C# .NET 9


I am trying to set up Tasks in my Azure DevOps pipeline to collect Code Coverage results, after running UTs using the VsTest Task, to then have a Powershell Task in the Pipeline write to a SQL db the contents of those metrics.

The main issue I am encountering is actually finding the published results after the UTs successfully run. I have set up Tasks to publish the results, then find them & then insert, but the publish doesn’t seem to actually publish to the directory I specify, or if it does publish, I cannot see where to.

Here are the Tasks I currently have set-up.

Task to run UTs:

steps:
- task: VSTest@2
  displayName: 'VsTest - testAssemblies'
  inputs:
    testAssemblyVer2: |
     **\$(BuildConfiguration)\*\*test*.dll
     !**\obj\**
    runSettingsFile: '$/B3API/Main/B3API.Tests/codecoverage.runsettings'
    runInParallel: true
    runTestsInIsolation: false
    codeCoverageEnabled: true
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'
    failOnMinTestsNotRun: true

codecoverage.runsettings file:

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="Code Coverage">
        <Configuration>
          <Format>cobertura</Format>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

Task to publish results:

steps:
- task: PublishCodeCoverageResults@2
  displayName: 'Publish code coverage results'
  inputs:
    summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.cobertura.xml'
    pathToSources: '$(System.DefaultWorkingDirectory)/**/coverage'

Task to find published file & store into variable:

steps:
- powershell: |
   $coverageFile = "$(System.DefaultWorkingDirectory)/**/coverage.cobertura.xml"
   [xml]$coverageData = Get-Content $coverageFile
   $coveragePercentage = $coverageData.coverage.@line-rate
   
   # Store the coverage data in a variable
   Write-Host "##vso[task.setvariable variable=coveragePercentage]$coveragePercentage"
   
  displayName: 'Store Coverage in variable'

The main issue it the Task to publish, it does not publish the results, I think it is due to not finding them in the first place.

Screenshot of result of Publish Results Task, showing "No code coverage results were found to publish"

Any help would be greatly appreciated, thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *