'How to add [ExcludeFromCodeCoverage] attribute at an assembly-level in a .Net Standard 2.1 project

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.



Solution 1:[1]

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]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ceiling cat