Publish code coverage results to Azure pipeline with Cobertura and Coverlet for C# .Net 6 project

Hashan Pallewatte
4 min readMay 7, 2023

--

Code coverage is a crucial aspect of software development, providing insights into the effectiveness of your testing efforts. By measuring code coverage, you can determine the extent to which your codebase is tested by your unit tests. Azure Pipeline, a powerful CI/CD platform provided by Microsoft, allows you to seamlessly integrate code coverage analysis into your development process. In this article, we will explore how to publish code coverage results to Azure Pipeline using Cobertura and Coverlet for a C# .NET 6 project and we are going to display that report for every build we run on Azure DevOps.

Benefits of publishing code coverage results :

  1. Enhanced Visibility: By displaying code coverage results directly on Azure Pipeline, all team members have easy access to the coverage metrics. This promotes transparency and encourages collaboration, allowing developers and stakeholders to gain a clear understanding of the code’s testability.
  2. Continuous Monitoring: Azure Pipeline enables continuous monitoring of code coverage. With each pipeline run, updated code coverage results are displayed, making it easier to track improvements or identify areas that need additional testing. This helps in maintaining a high level of code quality throughout the development process.
  3. Data-Driven Decision Making: By publishing code coverage results, you can make data-driven decisions regarding test coverage improvements. Analyzing the coverage reports on Azure Pipeline allows you to identify areas of low coverage, prioritize test creation or enhancement, and ensure critical parts of your codebase are adequately tested.

First, let’s take a look at Cobertura and Coverlet.

Cobertura is a popular open-source code coverage tool that provides detailed reports on the coverage of your unit tests. It supports multiple programming languages, including C# .NET, and generates XML reports that can be easily consumed by various CI/CD platforms.

Coverlet, on the other hand, is a cross-platform code coverage library specifically designed for .NET applications. It integrates seamlessly with popular testing frameworks like MSTest, NUnit, and xUnit, and produces coverage reports in the Cobertura format.

Let’s get started.

Step 1: Configuration in the .NET 6 Project:

  • Begin by ensuring that your C# .NET 6 project includes the necessary NuGet packages for your test project: Coverlet.MSBuild and Coverlet.collector
Nuget packages need to install

Step 2: Configuring the Azure Pipeline:

  • In your project, navigate to the repository containing your Azure Pipeline configuration file (eg:- azure-pipelines.yml)
  • Add a new task or step to your pipeline configuration that runs the unit tests with code coverage after the dotnet build step.
 - task: DotNetCoreCLI@2
displayName: Build
inputs:
projects: $(BuildParameters.RestoreBuildProjects)
arguments: --configuration $(BuildConfiguration)
- task: DotNetCoreCLI@2
displayName: Test
inputs:
command: test
projects: '**/*Test.csproj'
publishTestResults: true
arguments: '--configuration $(buildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura'

Step 3: Publishing Code Coverage Results:

  • After running the tests with code coverage enabled, you need to publish the generated coverage reports to Azure Pipeline.
  • Add another task to your pipeline configuration that publishes the Cobertura-formatted coverage report. Use the PublishCodeCoverageResults task and specify the path to the coverage report file.
- task: PublishCodeCoverageResults@1
displayName: 'Publish code coverage results'
condition: succeededOrFailed()
inputs:
codeCoverageTool: 'Cobertura'
summaryFileLocation: '**/*coverage.cobertura.xml'

Step 4: Saving and Running the Azure Pipeline:

  • Save the changes made to your Azure Pipeline configuration file.
  • Commit and push the changes to your repository.
  • Navigate to your Azure DevOps project and go to the Pipelines section.
  • Locate and select the pipeline associated with your project.
  • Start a new pipeline run.

Step 5: Viewing Code Coverage Results on Azure Pipeline:

  • Once the pipeline run completes, navigate to the pipeline run summary page.
  • Look for the “Code Coverage” section, which should display the coverage metrics and a link to the detailed coverage report.

Conclusion:

Integrating code coverage analysis into your Azure Pipeline using Cobertura and Coverlet for your C# .NET project is a powerful approach to ensure the quality and reliability of your code. By publishing code coverage results to Azure Pipeline, you enable enhanced visibility, continuous monitoring, and data-driven decision-making. Follow the step-by-step instructions outlined in this article to set up and leverage code coverage analysis as an integral part of your development process on Azure Pipeline.

--

--