4

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...

Unomagan
  • 313
  • 1
  • 4
  • 9
  • Its going to be tough to answer your question unless you give some more detail. Ideally your pipeline.yml steps. See this question https://stackoverflow.com/questions/54627918/view-code-coverage-report-on-azure-devops-portal – Eric Smith Feb 07 '20 at 03:06
  • Hi @Unomagan Did you check below yml? please let me know if there is any question. – Levi Lu-MSFT Feb 14 '20 at 09:27
  • No, didn't, and at this point I'm giving up for now... Maybe later – Unomagan Feb 15 '20 at 10:33
  • Hi @Unomagan Please check out below steps if you get the chance, it is not very complicated. I successfully got the code coverage shown on my pipeline with below steps – Levi Lu-MSFT Feb 16 '20 at 13:54
  • 1
    I've upvoted this... I have a screenshot of a coverage tab. When I come back to the _same run_ in devops, there's no tab any more! – Ian Grainger Nov 24 '20 at 16:48
  • 3
    I have a number of pipelines with multiple stages and the Code Coverage tab only appears after all the stages have been completed (even though the build and test happens in the first stage). I spent a lot of time running the test stage, not seeing the tab and assuming it wasn't working! – theduck Aug 11 '21 at 14:20

3 Answers3

6

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?

Ian Grainger
  • 5,148
  • 3
  • 46
  • 72
0

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.

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
  • hey @levi-lu-msft - this doesn't seem to be working again :/ The report get generated but not visible. It's also not clear how to get it to work with multiple test assemblies - each is producing a `TestResults\Coverage\coverage.cobertura.xml` file, so dotnet test seem to be overwriting them – Karpik Jun 14 '20 at 03:52
  • You can get it to make a single xml file by doing the following: set output format to cobertura,json and then set MergeWith=OutputDir\coverage.json it will then merge all the json files and as it does that it will regenerate the xml file with the contents merged as well. You might have to tweak the settings a bit, but that's generally how you do it – pinkfloydx33 Jun 24 '20 at 21:38
0

I had this issue too and tracked it down to having a mixture of VS Test and dotnet test tasks in my pipeline. Once I removed the VS Test task it all worked.

It seems the VS Test task uploads its own code coverage results and the combination of publishing cobertura results + this VS Test task confused Azure Devops.