Wednesday 18 October 2023

Code Unit Test Coverage with Azure Pipelines

 Unit tests are essntial to ensure the code we develop is working as intended. Running the unit tests Azure pipelines is really helpful to not to miss the unit test failures. However, to give afurther assuarance we need to check the coverage of code with unit tests in our projects. Let's look at steps required to obtain a code unit tests coverage report in Azure piplines in  cobertura format.

The expected outcome

The coverage here shows the first library, all code lines (100%) are covered with tests and the the other library has only 22.2% code coverage. 

We can dig deeper into the report by clicking on the code file name and find which lines are not covered.

How to do

As the first step we need to add coverlet.collector NuGet package to all our test projects as shown below.

<PackageReference Include="coverlet.collector" Version="6.0.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

Then we can define a coverage.settings file (this is essentially a run setting file for unit tests) wit below content.

<?xml version="1.0" encoding="utf-8" ?>
      <DataCollector friendlyName="XPlat code coverage">

Using that coverage setting we can generate the Cobertura format report in pipeline tasks as shown below.

The additional argument passed to dotnet test are

  --collect "XPlat Code Coverage" --settings pipelines/settings/coverage.settings

- task: DotNetCoreCLI@2
    displayName: 'dotnet test'
      command: test
      projects: '**/*.tests.csproj'
      arguments: '--configuration $(build_configuration)  --collect "XPlat Code Coverage" --settings pipelines/settings/coverage.settings'
      publishTestResults: true
      testRunTitle: '${{ parameters.build_os }} Unit Tests'
      testResultsFiles: '**/*.trx'
      workingDirectory: '$(build.sourcesdirectory)'
  - task: PublishCodeCoverageResults@1
    displayName: 'Publish code coverage report'
      codeCoverageTool: 'Cobertura'
      summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'

Once pipline with above task executed the code coverage report as shown in above expected outcome can be obtained.

No comments:

Popular Posts