coverlet-coverage / coverlet

Cross platform code coverage for .NET
MIT License
2.97k stars 386 forks source link

Unable to exclude files using .runsettings in dotnet test. #1683

Closed ashishbhatt-afk closed 1 week ago

ashishbhatt-afk commented 1 month ago

I am trying to exclude few files from code coverage but I cant do it.

  1. My .runsettings file is in the root folder.
  2. I am sure that .runsetting file is picked up by the pipeline.

Below is my runsettings file:


   <?xml version="1.0" encoding="utf-8" ?>
   <RunSettings>
     <DataCollectionRunSettings>
       <DataCollectors>
         <DataCollector friendlyName="XPlat code coverage">
           <Configuration>
             <Format>cobertura,opencover</Format>
             <Exclude>[*.Tests?]*</Exclude>
             <ExcludeByFile>**/IRISElementsBookkeeping/*.cs,</ExcludeByFile>
             <SkipAutoProps>true</SkipAutoProps>
           </Configuration>
         </DataCollector>
       </DataCollectors>
     </DataCollectionRunSettings>
   </RunSettings>

Below is my YAML:

- task: DotNetCoreCLI@2
    displayName: Build Unit tests
    inputs:
      command: 'build'
      arguments: '--configuration $(BUILD_CONFIGURATION)'
      projects: '$(TEST_PROJECT_PATH)'

  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: |
        pwd
        ls
        brew install findutils
        gfind . -regex '.*App.xaml.cs'

  - task: DotNetCoreCLI@2
    displayName: 'Run Tests and Collect Coverage'
    inputs:
      command: 'test'
      projects: '**/*Tests.csproj'
      arguments: '--configuration $(BUILD_CONFIGURATION) --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=$(COVERAGE_DIRECTORY)/ --settings "demo.runsettings"'
      publishTestResults: true

  - script: |
      dotnet tool install --global dotnet-reportgenerator-globaltool
      reportgenerator -reports:$(COVERAGE_DIRECTORY)/coverage.cobertura.xml -targetdir:$(COVERAGE_REPORT_DIRECTORY) -reporttypes:Html
    displayName: 'Generate Coverage Report'

  - task: PublishCodeCoverageResults@2
    displayName: 'Publish Code Coverage'
    inputs:
      summaryFileLocation: '$(COVERAGE_DIRECTORY)/coverage.cobertura.xml'

Please help.

ashishbhatt-afk commented 1 month ago

@Bertk @daveMueller Would you guys be able to help here. I saw your few comments on a similar issue.

Bertk commented 1 month ago

@ashishbhatt-afk I use the report from reportgenerator to identify the assemblies and classes which should be excluded.

I have doubts using [*.Tests?]*. Please use assembly names instead

 <Exclude>[typename1]*, [typename2]*</Exclude>
 <ExcludeByFile>**/IRISElementsBookkeeping/*.cs</ExcludeByFile>

https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/MSBuildIntegration.md#filters

example from Coverlet CI https://github.com/coverlet-coverage/coverlet/blob/cf40840873588e3b596eebc25362732728b3d651/eng/build.yml#L33

fossbrandon commented 1 week ago

I'm running into similar issues. I created an issue here with my findings #1695

From what I found, the generated report that you specified under CoverletOutput is not using the runsettings file properly. Until this gets fixed, you could manually add the exclusions using the property /p:CoverletOutput=... I believe.

Bertk commented 1 week ago

The VSTest integration with coverlet.collector uses the .runsettings file (see VSTestIntegration.md)

You are using coverlet.msbuild which only supports MSBuild properties.

  - task: DotNetCoreCLI@2
    displayName: 'Run Tests and Collect Coverage'
    inputs:
      command: 'test'
      projects: '**/*Tests.csproj'
      arguments: '--configuration $(BUILD_CONFIGURATION) --no-build /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=$(COVERAGE_DIRECTORY)/ --settings "demo.runsettings"'
      publishTestResults: true