azure-devopsazure-pipelinescode-coveragepipelinecobertura

Publish a pipeline Azure Devops code coverage report


I am trying to publish a detailed report online in my Azure DevOps Pipeline, but all I got is a link to download this Coverage file. (That can not be read anymore with the community version since the Visual Studio 2019) enter image description here

This is my pipeline:

trigger:
  branches:
    include:
    - '*'

pool:
  vmImage: 'windows-2019'

steps:
- task: NuGetToolInstaller@0
  displayName: Instal Nuget
  inputs:
    checkLatest: true

- task: NuGetCommand@2
  displayName: Restore Nuget Packages
  inputs:
    restoreSolution: '**/*.sln'

- task: UseDotNet@2
  displayName: 'Install .NET Core SDK'
  inputs:
    version: 3.1.x
    performMultiLevelLookup: true

- task: DotNetCoreCLI@2
  displayName: Build Tests
  inputs:
    command: 'build'
    projects: '**/OneTienditaUnitTests/*.csproj'
    arguments: '--configuration Release'

- script: dotnet test OneTienditaUnitTests --logger trx --collect "Code coverage"

- task: PublishTestResults@2
  inputs:
    testRunner: VSTest
    testResultsFiles: '**/*.trx'

- task: XamarinAndroid@1
  displayName: Build Android App
  inputs:
    projectFile: '**/*Android*.csproj'
    outputDirectory: '$(build.binariesDirectory)/Release'
    configuration: 'Release'

and if I use Cobertura like this, doesn't work:

- task: DotNetCoreCLI@2
  displayName: Run Tests
  inputs:
    command: 'test'
    projects: '**/OneTienditaUnitTests/*.csproj'
    arguments: '--configuration Release /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=../reports/coverage/'
    
- task: PublishCodeCoverageResults@1
  displayName: 'Publish code coverage results'
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(build.sourcesdirectory)\reports\coverage\coverage.cobertura.xml'
    reportDirectory: '$(build.sourcesdirectory)\reports\coverage'

Please any help? I am not a professional DevOps


Solution

  • To have there published report you need to use Cobertura. For TRX you will get only link to download file. And to create Cobertura report you need to install in your test projects coverlet.collector nuget package. Here you have code which should fix your problem:

    # You just added coverlet.collector to use 'XPlat Code Coverage'
    - task: DotNetCoreCLI@2
      displayName: Test
      inputs:
        command: test
        projects: '**/*Tests/*.csproj'
        arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage" -- RunConfiguration.DisableAppDomain=true'
        workingDirectory: $(Build.SourcesDirectory)
    
    - task: DotNetCoreCLI@2
      inputs:
        command: custom
        custom: tool
        arguments: install --tool-path . dotnet-reportgenerator-globaltool
      displayName: Install ReportGenerator tool
    
    - script: ./reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/coverlet/reports -reporttypes:"Cobertura"
      displayName: Create reports
    
    - task: PublishCodeCoverageResults@1
      displayName: 'Publish code coverage'
      inputs:
        codeCoverageTool: Cobertura
        summaryFileLocation: $(Build.SourcesDirectory)/coverlet/reports/Cobertura.xml  
    

    [2021 UPDATE]

    You don't need extra tasks to install/run the custom ReportGenerator tool: it is now the default tool for reading coverage.cobertura.xml files and is included in the dotnet CLI.

    By default, it will save the cobertura xml file to the temp directory on the agent, though. So, you just need to update the summaryFileLocation of the PublishCodeCoverageResults task to point to the temp directory and skip the "middle man" steps:

    # You just added coverlet.collector to use 'XPlat Code Coverage'
    - task: DotNetCoreCLI@2
      displayName: Test
      inputs:
        command: test
        projects: '**/*Tests/*.csproj'
        arguments: '--configuration $(buildConfiguration) --collect:"XPlat Code Coverage"'
    
    - task: PublishCodeCoverageResults@1
      displayName: 'Publish code coverage'
      inputs:
        codeCoverageTool: Cobertura
        summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
    

    If you have multiple test projects which generates multiple coverage files please use these steps after test commad. It will merge files before publishing them:

      - task: reportgenerator@4
        displayName: "Merge code coverage reports"
        inputs:
          reports: "**/coverage.cobertura.xml"
          targetdir: "$(Build.ArtifactStagingDirectory)/coverlet"
          reporttypes: "Cobertura"
          verbosity: "Verbose"
    
      - task: PublishCodeCoverageResults@1
        displayName: "Publish code coverage results"
        inputs:
          codeCoverageTool: Cobertura
          summaryFileLocation: "$(Build.ArtifactStagingDirectory)/coverlet/Cobertura.xml"