RKN
RKN

Reputation: 962

How to specify ExcludeFromCodeCoverage at assembly level for dot net framework class library?

I want to exclude a test project from code coverage, I do see equivalent of same in .Net core 3.1 is adding below line of code in csproj file

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

How to specify same thing in dot net framework 4.8 csproj class library file?

Upvotes: 8

Views: 9590

Answers (2)

Ben
Ben

Reputation: 5715

An alternative for this (I've only tested it in .NET 6) is to include an AssemblyInfo.cs file in your project that includes the following:

using System.Diagnostics.CodeAnalysis;

[assembly: ExcludeFromCodeCoverage]

UPDATE:

This works in at least .NET 6 and .NET Standard 2.1. It definitely doesn't work in .NET Standard 2.0 as the [ExcludeFromCodeCoverage] attribute cannot be used with the assembly: declaration in that version. I haven't tried it in any other versions.

Upvotes: 6

Martin Ullrich
Martin Ullrich

Reputation: 100701

The <AssemblyAttribute> item only works in SDK-based projects. So if you create an SDK-based .NET Framework csproj, it will work (do note this might clash with any existing AssemlyInfo.cs if one exists). e.g.:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <OutputType>Library</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage" />
  </ItemGroup>
</Project>

Otherwise, I worked on a similar situation a while back and created a drop-in replacement for the assembly info generation logic for non-SDK csproj, see https://github.com/dasMulli/AssemblyInfoGenerationSdk for that (do log issues if it doesn't work). Do note that you may need to disable some generation of e.g. Version attributes similar to SDK-style projects so it doesn't clas with existing AssemblyInfo.cs files.

Upvotes: 15

Related Questions