5

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.

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Tushar patel
  • 3,279
  • 3
  • 27
  • 30

2 Answers2

7

The solution provided by @Jack is using dotnet test and 'XPlat code coverage'. For vstest and 'Code Coverage' you will still need the .runsettings file like this:

<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="Code Coverage">
        <Configuration>
          <CodeCoverage>
            <ModulePaths>
              <Exclude>
                <ModulePath>FluentAssertions.*</ModulePath>
                <!-- Add more ModulePath nodes here. -->
              </Exclude>
            </ModulePaths>
          </CodeCoverage>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

(Note the extra <CodeCoverage> node, compared to the other solution)

Next, refer the settings file in the vstest task using the runSettingsFile argument like so:

- 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"'
    runSettingsFile: '<PATH/TO/FILE.RUNSETTINGS>'
pogosama
  • 1,818
  • 1
  • 24
  • 29
4

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

Jack
  • 15,614
  • 19
  • 67
  • 92