1

In project we have both asp.net core and asp.net framework sln. Now I need to get Code Coverage Report for both asp.net framework and asp.net core unit tests in Azure DevOps. Please help me out. I have already checked "This Article" it is not working for me. I am using Visual Studio Test task for Tests it is working fine and showing the results in Tests tab. When I enabled Code coverage enabled in the VS Test task it is simply publishing .coverage file but not displaying the code coverage report.

Swapna
  • 11
  • 1

1 Answers1

1

First, you need to compute the code coverage when you run the tests. There are multiple solutions, some are free such as coverlet, some are paid such as dotCover. Here, coverlet is used.

First, you need to install the NuGet package coverlet.msbuild. This package is integrated directly with dotnet test.

<Project Sdk="Microsoft.NET.Sdk">

  ...

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

  ...

</Project>

You can test by running the command dotnet test /p:CollectCoverage=true.

After the test run, a JSON file should be created next to the csproj file. Azure DevOps doesn't support this file, but coverlet can output the result file in many standard formats. So, you can use the cobertura format which is supported by Azure DevOps.

dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura

After running the command for each test project, you get one code coverage file per project. These files are not very convenient to read. But we can generate a UI using ReportGenerator.

This free tool allows you to generate a website to navigate into the files and see the lines covered by the tests. You can install as a .NET global tool:

dotnet tool install -g dotnet-reportgenerator-globaltool
reportgenerator -reports:tests/**/coverage.cobertura.xml -targetdir:Report -reporttypes:HtmlInline_AzurePipelines;Cobertura

You now have a website that conveniently displays the code coverage. Note that there are multiple output formats. Here we use HtmlInline_AzurePipelines because at the end we would like to view this web site directly in Azure Pipelines, so it seems to be the best output for that.

Now let's integrate the tests and the code coverage on the build system (Azure Pipeline). Here's the YAML file that describes the build steps:

variables:
  buildConfiguration: Release
  DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1

steps:
# Install the latest version of the dotnet sdk
- task: DotNetCoreInstaller@0
  displayName: 'Use .NET Core sdk 2.2.103'
  inputs:
    version: 2.2.103

# build all projects
- task: DotNetCoreCLI@2
  displayName: dotnet build
  inputs:
    projects: 'src/**/*.csproj'
    arguments: '--configuration $(BuildConfiguration)'

# Run all tests with "/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura" to generate the code coverage file
- task: DotNetCoreCLI@2
  displayName: dotnet test
  inputs:
    command: test
    arguments: '--configuration $(BuildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura'
    projects: 'tests/**/*.csproj'
    nobuild: true

# Generate the report using ReportGenerator (https://github.com/danielpalme/ReportGenerator)
# First install the tool on the machine, then run it
- script: |
    dotnet tool install -g dotnet-reportgenerator-globaltool
    reportgenerator -reports:$(Build.SourcesDirectory)/tests/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/CodeCoverage -reporttypes:HtmlInline_AzurePipelines;Cobertura
  displayName: Create Code coverage report

# Publish the code coverage result (summary and web site)
# The summary allows to view the coverage percentage in the summary tab
# The web site allows to view which lines are covered directly in Azure Pipeline
- task: PublishCodeCoverageResults@1
  displayName: 'Publish code coverage'
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(Build.SourcesDirectory)/CodeCoverage/Cobertura.xml'
    reportDirectory: '$(Build.SourcesDirectory)/CodeCoverage'

When you run a build, you now have the code coverage result in the Summary tab of the build:

enter image description here

A new tab is also available: "Code Coverage". When you click on it, you'll see the generated web site.

Warning: You must enable Boards for your project to view the Code Coverage page.

Also refer this SO thread to get Code Coverage of .NET Framework Project using coverlet in Azure DevOps.