azure-devopsazure-pipelinescode-coveragevstest

Filter Code Coverage results in Azure DevOps Pipelines


I run my tests with VSTest@2 step with codeCoverageEnabled: true (.NET project)

- task: VSTest@2
  displayName: 'Run Tests'
  inputs:
    vsTestVersion: 'toolsInstaller'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    codeCoverageEnabled: true

But in the Code Coverage tab, I can see all the external dependencies (Managed via NuGet, and Native):

![enter image description here

I hid all the names, but the red ones are external dependencies and only the green one is my assembly that I want to test. As a result, the total line coverage is way below the actual relevant value...

How can I filter out all the external resources to get the results for my code only?


Solution

  • After some research, if you want to filter out all the external resources, i found a workaround to exclude assemblies manually.You have to add all external resources in your .runsetting file one by one. You can check the steps below.

    1.In the .runsettings file,add the following configuration to exclude specific modules (DLLs) from code coverage:

    <ModulePaths>
      <Exclude>
       <ModulePath>.*XXXX.dll</ModulePath>
       <!-- Add more ModulePath nodes here. -->
      </Exclude>
    </ModulePaths>
    

    https://learn.microsoft.com/en-us/visualstudio/test/customizing-code-coverage-analysis?view=vs-2022&source=recommendations#include-or-exclude-assemblies-and-members

    2 Refer to the .runsettings file using the runSettingsFile argument in the VSTest tasK.

    - task: VSTest@2
      displayName: 'Run Tests'
      inputs:
        vsTestVersion: 'toolsInstaller'
        platform: '$(buildPlatform)'
        configuration: '$(buildConfiguration)'
        codeCoverageEnabled: true
        runSettingsFile: '<PATH/TO/FILE.RUNSETTINGS>'
    
    

    Hope it can help you.