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

2

7

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?

Spacial answered 28/4, 2021 at 10:27 Comment(0)
C
14

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.

Crosseyed answered 2/5, 2021 at 19:53 Comment(0)
G
5

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.

Geesey answered 26/8, 2022 at 8:7 Comment(4)
Looks like this doesn't work on .NET Standard projects...Karyotype
@Karyotype I just tried it in a new .NET Standard 2.1 project and it works absolutely fineGeesey
Right, I meant to say Standard2.0.Karyotype
Seems you are correct, if vague. [ExcludeFromCodeCoverage] is not valid with an assembly declaration in .NET Standard 2.0Geesey

© 2022 - 2024 — McMap. All rights reserved.