How to exclude test code from code coverage results using Azure DevOps build pipeline

Viewed 419

I am compiling an .Netcore application using Azure DevOps build pipeline. In the .Netcore test build task, I have passed the below arguments for collecting the code coverage results:

--configuration $(BuildConfiguration) --collect"XPlat Code Coverage"

For publishing Code Coverage results, I have used "Publish Code Coverage Results" build task using "Cobertura" tool.

The code coverage analyzes all solution assemblies that are loaded during unit tests. But I want to exclude test code from code coverage results and only include application code through the Azure DevOps build pipeline.

For that I have referred this documentation. Based on this documentation you need to add the ā€œExcludeFromCodeCoverageAttributeā€ attribute to each test class. But I want do it through the Azure DevOps build pipeline.

1 Answers

I was in your same situation months ago. But instead of Coverlet I'm using the native MS Code Coverage tool in this way:

  - task: DotNetCoreCLI@2
    displayName: 'DotNetCoreCLI Test with report (cobertura format)'
    condition: succeededOrFailed()
    inputs:
      command: test
      projects: '**/**.Tests.csproj'
      arguments: '--configuration $(BuildConfiguration) --no-restore --collect "Code Coverage" --logger trx --results-directory "TestResults/Coverage/" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=cobertura'
      publishTestResults: false

This works well but there is your same problem. I resolved using filtering with ReportGenerator task:

  - task: reportgenerator@5
    condition: succeededOrFailed()
    inputs:
      reports: '$(Build.SourcesDirectory)/TestResults/Coverage/**/**.cobertura.xml'
      targetdir: '$(Build.SourcesDirectory)/TestResults/Coverage/Reports'
      reporttypes: 'HtmlInline_AzurePipelines_Dark;Cobertura'
      assemblyfilters: '+My.Company.**;-My.Company.**.Tests'

It works well. For other questions, see my GitHub answer here

Related