azure-devopsazure-pipelinescode-coverageazure-pipelines-yaml

Azure DevOps code coverage status check shouldn't include third party dlls


My Azure Devops yaml pipeline contains the following:

stages:
- stage: 'Build'
  displayName: 'Build, Test, Publish'
  jobs:
  - job: build
    displayName: Build
    steps:

    - task: NuGetAuthenticate@1 # needed to authenticate for private NuGet feed

    - script: |
        dotnet build --configuration Release

    - task: DotNetCoreCLI@2
      displayName: Test
      inputs:
        command: test
        projects: '**/*[Tt]ests/*.csproj'
        arguments: '--configuration release --collect "Code coverage" --no-build'

Which automatically adds a code coverage status check to my pull requests. list of PR checks

However, what's disappointing is the coverage metric includes 3rd party dlls from NuGet, which makes my code coverage appear low:

code coverage report


Solution

  • I am afraid that the code coverage status check in Pull Request will not automatically exclude the third party dlls.

    The code coverage status check in Pull Request will fully reflect the code coverage results in the related Pipeline.

    How can I tell it to only report on the code coverage of my code?

    To meet your requirement, you can add the .runsettings file to your project and use it to exclude the Microsoft dlls when collecting the code coverage in dotnet test task.

    Here is an example:

    <?xml version="1.0" encoding="utf-8" ?>
    <RunSettings>
      <DataCollectionRunSettings>
         <DataCollectors>
          <DataCollector friendlyName="Code Coverage">
            <Configuration>
              <CodeCoverage>
                <ModulePaths>
                  <Exclude>
                    <CompanyName>.*microsoft.*</CompanyName>
                  </Exclude>
                </ModulePaths>
              </CodeCoverage>
            </Configuration>
          </DataCollector>
       </DataCollectors>
      </DataCollectionRunSettings>
    </RunSettings>
    

    When you use the dotnet test task, you can add the argument: --settings filename.runsettings

    For example:

    - task: DotNetCoreCLI@2
      displayName: Test
      inputs:
        command: test
        projects: '**/*[Tt]ests/*.csproj'
        arguments: '--configuration release --collect "Code coverage" --settings filename.runsettings  --no-build'
    

    In this case, the code coverage result in Pipeline will exclude the third parts dlls and the code coverage status check will get the report which doesn't contain the third part dlls.

    For more detailed info, you can refer to this doc: Customize code coverage analysis