Search code examples
c#azure-devopsazure-pipelinescode-coverage

How to exclude code coverage for Program.cs and all files under specific folder in Azure DevOps pipeline


I've been unable to exclude code coverage for Program.cs and all files under our Migrations folder. We'd prefer to not list out the entire namespace that Program.cs is under and where the Migrations folder is at as we are attempting to set up a template for all our projects that will utilize the same folder structures. We are utilizing Azure DevOps.

This is our CodeCoverage.runsettings file:

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="XPlat Code Coverage">
                <Configuration>
                    <Functions>
                        <Exclude>
                            <Function>.*\.Program\..*</Function>
                            <Function>.*\.Migrations\.*\..*</Function>
                        </Exclude>
                    </Functions>
                </Configuration>
            </DataCollector>
        </DataCollectors>
    </DataCollectionRunSettings>
</RunSettings>

This is our test stage in our pipeline:

  - stage: Test
    displayName: 'Run Tests'
    dependsOn: Build
    condition: succeeded()
    jobs:
    - job: TestJob
      displayName: 'Run Tests and Generate Code Coverage'
      steps:
      - task: NuGetAuthenticate@1
        displayName: 'Authenticate with Azure Artifacts'
        inputs:
          forceReinstallCredentialProvider: false
      - task: DotNetCoreCLI@2
        displayName: Run Tests
        condition: succeeded()
        inputs:
          command: test
          projects: '**/tests/**/*.csproj'
          arguments: '--logger trx --results-directory "$(Agent.TempDirectory)/TestResults/Coverage/" --collect "XPlat Code Coverage" --settings CodeCoverage.runsettings'
          publishTestResults: false
      - task: PublishTestResults@2
        displayName: 'Publish Test Results'
        condition: succeeded()
        inputs:
          testResultsFormat: VSTest
          testResultsFiles: '**/*.trx'
          searchFolder: '$(Agent.TempDirectory)/TestResults/Coverage/'
      - task: PublishCodeCoverageResults@2
        displayName: 'Publish Code Coverage Report'
        condition: succeeded()
        inputs:
          summaryFileLocation: '$(Agent.TempDirectory)/**/coverage.cobertura.xml'
          failIfCoverageEmpty: false

Thank you!


Solution

  • In your CodeCoverage.runsettings file, you can try to configure like as below to exclude all methods in the specified namespace using the regular expression pattern. Replace 'myns' with the actual name of namespace in your projects.

    <?xml version="1.0" encoding="utf-8"?>
    <RunSettings>
        <DataCollectionRunSettings>
            <DataCollectors>
                <DataCollector friendlyName="XPlat Code Coverage">
                    <Configuration>
                        <CodeCoverage>
                            <Functions>
                                <Exclude>
                                    <!-- Exclude all methods in the 'myns' namespace. -->
                                    <Function>^myns\..*</Function>
                                </Exclude>
                            </Functions>
                        </CodeCoverage>
                    </Configuration>
                </DataCollector>
            </DataCollectors>
        </DataCollectionRunSettings>
    </RunSettings>
    

    You also can:

    • use the <ModulePath> element to include/exclude specified assemblies by assembly name or file path.
    • use the <CompanyName> element to include/exclude specified assemblies by the company name.
    • use the <PublicKeyToken> element to include/exclude specified signed assemblies by the public key token.
    • use the <Source> element to include/exclude specified source files by the file path.
    • use the <Attribute> element to include/exclude specified attributes by the full name of the attribute.

    enter image description here

    For more details, you can see the following documentations: