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

4.7k views Asked by At

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 the AssemblyInfo.cs file in .NET Core / .NET Standard projects.

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

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

but still no luck.

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

1

There are 1 answers

1
ceiling cat On

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]