0

I am trying to run unit tests using NUnit and coverlet but exclude assemblies that end with the name .Testing from the coverage report. However, whatever I do the file (for example MyProj.Testing.dll) is being added in the coverage report (other assemblies I don't want in the report, such as test assemblies, are not present).

I am using the following coverlet packages in my test assemblies:

  • coverlet.msbuild 3.2.0
  • coverlet.collector 3.2.0

I am using a .runsettings file to configure coverlet.

coverlet.runsettings file:

<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
  <RunConfiguration>
    <ResultsDirectory>./CodeCoverage/</ResultsDirectory>
  </RunConfiguration>
  <DataCollectionRunSettings>
    <DataCollectors>
      <DataCollector friendlyName="XPlat Code Coverage">
        <Configuration>
          <Format>cobertura</Format>
          <ExcludeByAttribute>Obsolete,GeneratedCodeAttribute,CompilerGeneratedAttribute,TestSDKAutoGeneratedCode</ExcludeByAttribute>
          <IncludeTestAssembly>false</IncludeTestAssembly>
          <CodeCoverage>
            <ModulePaths>
              <Include>
                <ModulePath>.*\.dll$</ModulePath>
                <ModulePath>.*\.exe$</ModulePath>
              </Include>
              <Exclude>
                <ModulePath>.*Testing\.dll$</ModulePath>
              </Exclude>
            </ModulePaths>
          </CodeCoverage>
        </Configuration>
      </DataCollector>
    </DataCollectors>
  </DataCollectionRunSettings>
</RunSettings>

I have looked at:

I have also tried:

  • Removing the Include section
  • Setting the Exclude/ModulePath to: .*Testing.*
  • Using Sources/Exclude/Source instead of ModulePath

I have been trying to do this seemingly trivial thing for hours but am now out of ideas. Any help would be appreciated.

bytedev
  • 8,252
  • 4
  • 48
  • 56
  • Sometimes, runtimesettings files are quite nasty. Try to run your command with debug log to see, what is happening. dotnet test --settings runsettings --diag:log.txt . Otherwise, you can explicitly mark the project assembly itself to exclude from code coverage & coverlet will exclude them in the coverage file. – vishal shah Feb 10 '23 at 09:49

1 Answers1

0

I got this issue, and after some research I discovered that coverlet uses another format to exclude assemblies. The documentation is here https://github.com/coverlet-coverage/coverlet/blob/master/Documentation/VSTestIntegration.md

So the filter in runsettings would be something like:

<RunSettings>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="XPlat code coverage">
                <Configuration>
                    <Format>opencover</Format>
                    <Exclude>[*Testing*]*</Exclude> <!-- [Assembly-Filter]Type-Filter -->
                </Configuration>
            </DataCollector>
        </DataCollectors>
    </DataCollectionRunSettings>
</RunSettings>