skip to Main Content

I am trying to exclude a folder from the code coverage analysis but I haven’t had any success after a lot of trial and error attempts and research on here.

This is my test task in the build pipeline:

- task: DotNetCoreCLI@2
  displayName: 'Running Unit Tests'
  inputs:
    command: test
    projects: '$(unitTestsProject)'
    arguments: '--configuration $(buildConfiguration) --settings ./cover.runsettings'
    publishTestResults: true
  continueOnError: false

and in the run settings I have tried different approaches, some of them are as bellow:*

<DataCollector friendlyName="XPlat code coverage">
    <Configuration>
        <CodeCoverage>
            <ModulePaths>
                <Exclude>
                    <ModulePath>.*Migrations.*</ModulePath>
                    <ModulePath>.*\Migrations\.*</ModulePath>
                </Exclude>
            </ModulePaths>
        </CodeCoverage>
    </Configuration>
</DataCollector>
<DataCollector friendlyName="XPlat code coverage">
    <Configuration>
        <CodeCoverage>
                        <ExcludeDirectories>
                <Directory>.*\Migrations\.*</Directory>
            </ExcludeDirectories>
        </CodeCoverage>
    </Configuration>
</DataCollector>

All of the variations above didn’t work. But when I used the attribute [ExcludeFromCodeCoverage] on each class I didn’t want included in the code coverage, it worked well. I need it to work from the runsettings file as the migrations folder will grow quickly and it’s not practical to keep going through the generated classes and adding attributes.

Any help or guidance will be highly appreciated. Thank you

2

Answers


  1. According to this doc for Customize code coverage analysis, you could test to define your code with below.

            <!-- Match the path of the source files in which each method is defined: -->
            <Sources>
              <Exclude>
                <Source>.*\atlmfc\.*</Source>
                <Source>.*\vctools\.*</Source>
                <Source>.*\public\sdk\.*</Source>
                <Source>.*\microsoft sdks\.*</Source>
                <Source>.*\vc\include\.*</Source>
              </Exclude>
            </Sources>
    
    Login or Signup to reply.
  2. My guess is, it would be easier to block them by namespace…

            <Functions>
              <Exclude>
                <Function>^Fabrikam.UnitTest..*</Function>
                <Function>^std::.*</Function>
                <Function>^ATL::.*</Function>
                <Function>.*::__GetTestMethodInfo.*</Function>
                <Function>^Microsoft::VisualStudio::CppCodeCoverageFramework::.*</Function>
                <Function>^Microsoft::VisualStudio::CppUnitTestFramework::.*</Function>
              </Exclude>
            </Functions>
    

    Source files relies on the symbol data being present and a few other things. The namespace is embedded in the assembly itself.

    Make sure you try it locally to see if it work, before putting it on the build server too….

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search