Coverlet: Why Coverlet use custom attribute ExcludeFromCoverage instead of ExcludeFromCodeCoverage

Created on 30 Mar 2018  路  9Comments  路  Source: coverlet-coverage/coverlet

Hi guys!

Why Coverlet use the custom attribute ExcludeFromCoverage from its own namespace instead of using already exists in .NET Core attribute ExcludeFromCodeCoverage from namespace System.Diagnostics.CodeAnalysis?

To Coverlet ignore any class, I need to install the nuget package coverlet.msbuild in all projects with tests, right?

Most helpful comment

All 9 comments

@Valeriy1991 no, you can create a ExcludeFromCoverage attribute and use that. Coverlet uses the name and not the full type so anyone will work.

Is there any reason to not support ExcludeFromCodeCoverage as well? That would make it easier to transition to Coverlet from other products.

Hello! I'm still work on adding support on both attributes: ExcludeFromCoverage from Coverlet and ExcludeFromCodeCoverage from System.Diagnostics.CodeAnalysis in my fork.

In case you're wondering where to make the change it's right here... (in Coverlet.Core.Instrumentation.Instrumentation)

        private void InstrumentModule()
        {
            using (var stream = new FileStream(_module, FileMode.Open, FileAccess.ReadWrite))
            {
                var parameters = new ReaderParameters { ReadSymbols = true };
                ModuleDefinition module = ModuleDefinition.ReadModule(stream, parameters);

                foreach (var type in module.GetTypes())
                {
                    if (type.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage"))
                        continue;

                    foreach (var method in type.Methods)
                    {
                        if (!method.CustomAttributes.Any(a => a.AttributeType.Name == "ExcludeFromCoverageAttribute" || a.AttributeType.Name == "ExcludeFromCoverage"))
                            InstrumentMethod(method);
                    }
                }

                module.Write(stream);
            }
        }

@StillLearnin thanks, I'm in the know. But first I want to add unit tests to check the current functionality (use ExcludeFromCoverage attribute)

Hi :)

I cannot get this to work. If I include the ExcludeByAttribute property in my *.Tests.csproj file and add a property GeneratedCode to my generated code project then Coverlet still computes coverage for the generated code. Similarly if I add the property ExcludeFromCoverage to my generated code project coverage is again still computed for the project.

Here is my *.Tests.csproj file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <IsPackable>false</IsPackable>
    <CollectCoverage>true</CollectCoverage>
    <CoverletOutputFormat>lcov</CoverletOutputFormat>
    <CoverletOutput>./Datahub/lcov.info</CoverletOutput>
    <ExcludeByAttribute>GeneratedCode</ExcludeByAttribute>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="coverlet.msbuild" Version="2.8.0">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
    <PackageReference Include="xunit" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
    <PackageReference Include="coverlet.collector" Version="1.0.1" />
    <PackageReference Include="FluentAssertions" Version="5.10.0" />
  </ItemGroup>

  <ItemGroup>
    ... project references ...
  </ItemGroup>
</Project>

and here is my ProtoBuf.csproj file for the generated code:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <GeneratedCode>GeneratedCode</GeneratedCode>
    <ExcludeFromCoverage/>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Google.Protobuf" Version="3.11.2" />
    <PackageReference Include="Grpc" Version="2.26.0" />
    <PackageReference Include="Grpc.Tools" Version="2.26.0" PrivateAssets="All">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <Protobuf Include="**/*.proto" />
  </ItemGroup>
</Project>

Am I missing something or what is wrong?

Thanks :)

@svenskmand please open new issue, we'll discuss there.

@MarcoRossignoli Ok, thanks :) I have opened a new issue here.

Was this page helpful?
0 / 5 - 0 ratings