Excluding files from code coverage analysis in Azure devops pipeline
Asked Answered
H

2

11

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.

Herminahermine answered 14/6, 2022 at 12:6 Comment(0)
H
5

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

Hobson answered 16/6, 2022 at 7:53 Comment(0)
R
10

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>'
Romanfleuve answered 17/8, 2022 at 10:48 Comment(0)
H
5

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

Hobson answered 16/6, 2022 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.