Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code coverage tab not showing in Azure DevOps

I have a relative simple test project under Azure DevOps and I want to generate code coverage.

This works... kinda. I get this:

Azure DevOps Code coverage

I get the files I needed ( I think at least) But the tab is missing.

I have those three steps:

Do .NET test task Install report generator Run report generator to convert ( -reporttypes:HtmlInline_AzurePipelines;Cobertura") publish result (s)

But the tab is not showing up? Any ideas?

    - stage: Run_Unit_tests 
  jobs:
  - job: 'Tests'
    pool: 
      vmImage: 'windows-latest'
    variables:
      buildConfiguration: 'Release'
    continueOnError: true
    steps:
    - task: DotNetCoreCLI@2
      inputs:
        command: custom
        custom: tool
        arguments: install --tool-path . dotnet-reportgenerator-globaltool
      displayName: Install ReportGenerator tool

    - task: DotNetCoreCLI@2
      displayName: Test .NET
      inputs:
        command: test
        projects: '**/*Test/*.csproj'
        arguments: '--configuration $(buildConfiguration) --logger trx --collect:"XPlat Code Coverage"'
      condition: succeededOrFailed()

    - task: reportgenerator@4
      inputs:
        reports: '$(Agent.TempDirectory)\**\coverage.cobertura.xml'
        targetdir: '$(Build.SourcesDirectory)\coverlet\reports'
        verbosity: 'Verbose'
    - task: PublishCodeCoverageResults@1
      displayName: 'Publish code coverage'
      inputs:
        codeCoverageTool: Cobertura
        summaryFileLocation: $(Build.SourcesDirectory)\coverlet\reports\Cobertura.xml
        failIfCoverageEmpty: false
        reportDirectory: $(Build.SourcesDirectory)\coverlet\reports\

I tried with code generator, without, enable code coverage variable or disable, tried with report generator and without...

like image 479
Unomagan Avatar asked Sep 05 '25 03:09

Unomagan


2 Answers

I had the same problem, and just pressed F5 and it appeared!

It's mad, but it actually does it consistently.

It seems there's occasionally a bug in the devops front-end code?

like image 119
Ian Grainger Avatar answered Sep 07 '25 21:09

Ian Grainger


You can try below yaml to publish code coverage.

First you need to make sure your project reference to nuget package coverlet.msbuild

<PackageReference Include="coverlet.msbuild" Version="2.5.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>

Then in your dotnet test task to enable CollectCoverage

arguments: '/p:CollectCoverage=true /p:CoverletOutput=$(Build.SourcesDirectory)\TestResult\ /p:CoverletOutputFormat=cobertura'

Then in reportgenerator task specify the reports folder to the CoverletOutput folder reports: '$(Build.SourcesDirectory)\TestResult\**\coverage.cobertura.xml'

Please check below yaml for reference:

steps:
  - task: UseDotNet@2
    inputs:
      version: 2.2.x 

  - task: DotNetCoreCLI@2
    inputs:
      command: restore
      projects: '**\*.csproj'

  - task: DotNetCoreCLI@2
    inputs:
      command: custom
      custom: tool
      arguments: install --tool-path . dotnet-reportgenerator-globaltool
    displayName: Install ReportGenerator tool

  - task: DotNetCoreCLI@2
    displayName: Test .NET
    inputs:
      command: test
      projects: '**\*Test*.csproj'
      publishTestResults: false
      arguments: '--configuration $(buildConfiguration) /p:CollectCoverage=true /p:CoverletOutput=$(Build.SourcesDirectory)\TestResult\ /p:CoverletOutputFormat=cobertura'
    condition: succeededOrFailed()

  - task: reportgenerator@4
    inputs:
      reports: '$(Build.SourcesDirectory)\TestResult\**\coverage.cobertura.xml'
      targetdir: '$(Build.SourcesDirectory)\coverlet\reports'
      verbosity: 'Verbose'

    condition: succeededOrFailed()
  - task: PublishCodeCoverageResults@1
    displayName: 'Publish code coverage'
    inputs:
      codeCoverageTool: Cobertura
      summaryFileLocation: $(Build.SourcesDirectory)\coverlet\reports\Cobertura.xml
      failIfCoverageEmpty: false
      reportDirectory: $(Build.SourcesDirectory)\coverlet\reports\
    condition: succeededOrFailed()

You can also refer to this blog.

like image 34
Levi Lu-MSFT Avatar answered Sep 07 '25 21:09

Levi Lu-MSFT