5

I have a .Net Core application which has got one .Net Standard 2.1 project in it. I am using Coverlet to get the code coverage in Cobertura format.

I am using "coverlet.msbuild" nuget package in all my Test projects.

I want to add [ExcludeFromCodeCoverage] attribute at a assembly-level so that coverlet ignores this project while performing the analysis.

I cannot find AssemblyInfo.cs file in .Net Core / .Net Standard projects.

I tried adding below tag in the proejct's .csproj file

  <ItemGroup>
     <AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute" />
  </ItemGroup>

but still no luck.

The only workaround for me is to add [ExcludeFromCodeCoverage] attribute manually in all the class files which is not a best way.

Pradeep
  • 1,108
  • 1
  • 15
  • 31

1 Answers1

4

This worked for .NET 6. I suspect it will work for .NET Core 3.1+

Add the following to the csproj file

    <ItemGroup>
        <AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage" />
    </ItemGroup>

Or this in any file in the project for the assembly

using System.Diagnostics.CodeAnalysis;

[assembly: ExcludeFromCodeCoverage]
ceiling cat
  • 496
  • 4
  • 12
  • 2
    Using the attribute, I'm running into [CS0592](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0592?f1url=%3FappId%3Droslyn%26k%3Dk(CS0592)) – derekbaker783 Sep 09 '22 at 15:42