1

I have a .net core project with pipeline (YAML, multi-stage) pipeline set up in Azure DevOps. We have a bunch of unit tests that we execute at pipeline run time - everything is fine.

However - we would like to dig more deeper into our code coverage. So we have configured our task like this

- task: DotNetCoreCLI@2
  displayName: Run UnitTests
  enabled: true
  inputs:
    command: test
    projects: '**/PM.UnitTests.csproj'
    arguments: '--configuration $(buildConfiguration) --collect "Code coverage"'

The result is this

enter image description here

We can now see this in pipeline results:

enter image description here

This .coverage file can be downloaded and analyzed in e.g. Visual Studio.

However - we would really like to be able to see the results directly in Azure Pipelines. We have a Typescript project where we do this. The result is this: enter image description here

Sadly, it is not at all apparent to me how to apply this to a .net core project.

Q1: Is it possible to have the same experience for .net core projects ... and how?

Additionally - we would like to be able to apply filtering on which parts of the code is used to calculate the code coverage percentage.

Q2: Is it correctly understood that this is done using a .runsettings file?

Thank you :-)

/Jesper

For reference, this is my complete YAML pipeline in my test .net core solution. The solution is super simple - a .net core class library and a .net core test class library

enter image description here

pool:
  vmImage: vs2017-win2016

steps:
- task: DotNetCoreCLI@2
  inputs:
    command: 'restore'
    feedsToUse: 'select'
    vstsFeed: 'd12c137f-dac4-4ea7-bc39-59bd2b784537'
- task: DotNetCoreCLI@2
  displayName: Build
  inputs:
    projects: '**/*.csproj'
    arguments: --configuration $(BuildConfiguration) --no-restore
- task: DotNetCoreCLI@2
  displayName: Run UnitTests
  inputs:
    command: test
    projects: '**/Test.Coverage/Test.Coverage.csproj'
    arguments: '--configuration $(BuildConfiguration) --collect:"XPlat Code Coverage" '
- script: 'dotnet tool install -g dotnet-reportgenerator-globaltool  '
  displayName: 'Install dotnet-reportgenerator-globaltool'
- script: 'reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetdir:$(build.sourcesdirectory) -reporttypes:"Cobertura"'
  displayName: 'Executes reportgenerator'
- task: PublishCodeCoverageResults@1
  displayName: 'Publish code coverage from $(build.sourcesdirectory)/Cobertura.xml'
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(build.sourcesdirectory)/Cobertura.xml'
Jesper Lund Stocholm
  • 1,973
  • 2
  • 27
  • 49

2 Answers2

5

You could use the dotnet-reportgenerator-globaltool package to generate the HTML Code Coverage Report.

Here is an example:

- task: DotNetCoreCLI@2
  displayName: Test
  inputs:
    command: test
    projects: '$(Parameters.TestProjects)'
    arguments: '--configuration $(BuildConfiguration) --collect:"XPlat Code Coverage" '

- script: 'dotnet tool install -g dotnet-reportgenerator-globaltool  '
  displayName: 'Command Line Script'

- script: 'reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetdir:$(build.sourcesdirectory) -reporttypes:"Cobertura"'
  displayName: 'Command Line Script'

- task: PublishCodeCoverageResults@1
  displayName: 'Publish code coverage from $(build.sourcesdirectory)/Cobertura.xml'
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(build.sourcesdirectory)/Cobertura.xml'

Result:

enter image description here

Kevin Lu-MSFT
  • 20,786
  • 3
  • 19
  • 28
  • 1
    Hi Kevin, thank you so much! I had a bit of an issue getting the data collector to work right, but this question on SO helped me to figure out to add a reference to the coverlet collector on nuget https://stackoverflow.com/a/58885761/670816 – Jesper Lund Stocholm Aug 13 '21 at 09:22
1

What you're looking for is a tool that can generate a report based on the coverage information. You're looking for a ReportGenerator.

ReportGenerator converts coverage reports generated by coverlet, OpenCover, dotCover, Visual Studio, NCover, Cobertura, JaCoCo, Clover, gcov or lcov into human readable reports in various formats.

There are quite a few examples out there working with the dotnet-reportgenerator-globaltool for .NET Core projects.

You might find this Learn -> Usage page helpful.

Coverlet might also be an interesting addition to your toolbelt.

rickvdbosch
  • 14,105
  • 2
  • 40
  • 53