Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 433
Feber Castellon Avatar asked Oct 29 '20 13:10

Feber Castellon


People also ask

How do I publish Azure DevOps code coverage results?

In order to publish the results to the pipeline, the resulting artifacts should be to be made available to the Publish Code Coverage Results task. For reference you can see a similar example for publishing test results under Build, test, and publish results with a Docker file section for Docker.

How do I publish my Azure pipeline?

Search for the Publish Pipeline Artifacts task and then select Add to add it to your pipeline. Fill out the following fields: Display name: the task display name. File or directory path: the path of the file or directory to publish.

What is coverage status check in Azure DevOps?

The status check evaluates the diff coverage value for all the code files in the pull request. If you would like to view the % diff coverage value for each of the files, you can turn on details as mentioned in the configuration section.


1 Answers

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"
like image 111
Krzysztof Madej Avatar answered Nov 16 '22 04:11

Krzysztof Madej