unit-testingazure-devopscode-coveragecobertura

Excluding files from code coverage analysis in Azure devops pipeline


I have enabled code coverage in Cobertura format and I am trying to exclude some files (Especially 3rd party DLLs) from Code Coverage analysis in the Azure DevOps pipeline. Currently, below is the output I get in the pipeline

enter image description here

Here 3rd party DLLs are also included in the coverage report. I want to exclude all 3rd party DLLs like FluentAssertion, Microsoft.Azure etc.

Below are the some line from my YAML file which produces above output

- task: VSTest@2
  displayName: 'Run .NET Core Unit Tests $(ucSolution)'
  continueOnError: true
  inputs:
    testSelector: 'testAssemblies'
    testAssemblyVer2: |
      **\MyApp.*.UnitTests.dll
      !**\*TestAdapter.dll
      !**\obj\**
      !**\ref\**
    searchFolder: '$(System.DefaultWorkingDirectory)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    diagnosticsEnabled: true
    rerunFailedTests: true
    rerunFailedThreshold: '10'
    rerunMaxAttempts: '1'
    resultsFolder: '$(build.ArtifactStagingDirectory)\Test\Results\core'
    otherConsoleOptions: '/collect:"Code Coverage;Format=Cobertura"'

- task: PublishCodeCoverageResults@1
  displayName: 'Publish code coverage results'
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: $(build.ArtifactStagingDirectory)/Test/Results/**/**/*.cobertura.xml

Could anyone suggest how I can exclude 3rd party DLLs from the analysis or code coverage report?

I really appreciate any help you can provide.


Solution

  • Add a .runsettings file to your solution, and reference it in the test step. The runsettings file will need a ModulePaths, Exclude, ModulePath nodes see below:

        <?xml version="1.0" encoding="utf-8" ?>
        <RunSettings>
            <DataCollectionRunSettings>
                <DataCollectors>
                    <DataCollector friendlyName="XPlat code coverage">
                        <Configuration>
                            <ModulePaths>
                                <Exclude>
                                    <ModulePath>.*FluentAssertions.*</ModulePath>
                                </Exclude>
                            </ModulePaths>
                        </Configuration>
                    </DataCollector>
                </DataCollectors>
            </DataCollectionRunSettings>
        </RunSettings>
    

    Example test task in the pipeline yaml. It'll be slightly different for your VSTest@2 task but similar principal. See how I've added an argument for a .net core test task --settings MyFolder/.runsettings

          - task: DotNetCoreCLI@2
            displayName: 'Tests'
            inputs:
              command: test
              projects: 'MyTestProject.csproj'
              arguments: '--configuration debug --collect:"XPlat Code Coverage" --settings MyFolder/.runsettings'
              publishTestResults: true
              testRunTitle: "Run Tests"
    

    Microsoft documentation can be found here: https://learn.microsoft.com/en-us/visualstudio/test/customizing-code-coverage-analysis?view=vs-2022