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">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>


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" ?>
<RunSettings>
  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="XPlat code coverage">
        <Configuration>
          <Format>Cobertura</Format>
          <ExcludeByFile>**/Resources/*.Designer.cs,**/Program.cs,**/Startup.cs</ExcludeByFile>
          <SkipAutoProps>true</SkipAutoProps>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

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'
    inputs:
      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'
    inputs:
      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