Don
Don

Reputation: 4192

How to exclude method in coverlet coverage report?

How can I exclude a method from code coverage reporting using coverlet and reportgenerator. Excluding entire namespaces in .runsettings works as expected but using [ExcludeFromCodeCoverage] attribute excludes the entire file instead of only the targeted method. See Comments below for what I've tried in .runsettings.

relevant .runsettings lines:

  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector>
        <Configuration>
          <Format>lcov</Format>
          <Include>[*]*</Include>
          <Exclude> 
             <!-- excluded namespaces -->
          </Exclude>
          <!-- excludes entire file from coverage --> 
          <ExcludeByAttribute>Obsolete, GeneratedCodeAttribute, CompilerGeneratedAttribute,ExcludeFromCodeCoverage</ExcludeByAttribute>
          <!-- included & reported as uncovered --> 
          <ExcludeByAttribute> ExcludeFromCodeCoverageAttribute </ExcludeByAttribute>
          <SingleHit>true</SingleHit>
          <UseSourceLink>true</UseSourceLink>
          <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
          <!-- included and reported as uncovered -->
          <CodeCoverage>
            <Attributes>
              <Exclude>
                <Attribute> ^System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute$</Attribute>
              </Exclude>
            </Attributes>
          </CodeCoverage>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>

UPDATE: It seems to have been a recently resolved issue with coverlet. Updating resolved the issue. https://github.com/coverlet-coverage/coverlet/issues/809

Upvotes: 6

Views: 14439

Answers (1)

Yevheniy Tymchishin
Yevheniy Tymchishin

Reputation: 870

Just apply [ExcludeFromCodeCoverage] on the method instead of the class.

Upvotes: 10

Related Questions