question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Coverage exclusion does not seem to work :(

See original GitHub issue

Hi 😃

I cannot get coverage exclusion as described here 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 or ExcludeFromCodeCoverage 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/>
    <ExcludeFromCodeCoverage/>
  </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 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
svenskmandcommented, Feb 7, 2020

I just want to give a small update to this that I just discovered. You do not have to create a C# file with the attribute above, the same thing can be done in the csproj file like this:

<Project Sdk="Microsoft.NET.Sdk">
    <!-- ... -->
    <ItemGroup>
        <!-- ... -->
        <AssemblyAttribute Include="System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage"/>
    </ItemGroup>
</Project>
1reaction
svenskmandcommented, Feb 6, 2020

I now added a file coverage.cs to my ProtoBuf project with the following content:

using System.Diagnostics.CodeAnalysis;

[assembly: ExcludeFromCodeCoverage]

and now that project is not included in the coverage 😃 Thanks for the help 😄

I think that it would clear up the documentation, if a more explicit explanation is added for how to ignore a class/module/assembly. I misunderstood C# attributes with MSBuild properties so I guess others could also do that 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sonar.coverage.exclusions not excluding too much
When adjusting this field in the sonarqube-project.properties file, it looks like files get excluded in the code, but even ones that aren't ......
Read more >
Coverage exclusion does not seem to work :( · Issue #722
Hi :) I cannot get coverage exclusion as described here to work. If I include the ExcludeByAttribute property in my *.
Read more >
Sonar exclusions are not working which mentioned in ...
I'm using GitLab CI/CD pipeline for deploying the Springboot project. I have integrated SonarQube in my project. In my sonar-project.properties ...
Read more >
Re: sonar.coverage.exclusions not working when added ...
I am using the version 2.5 of the gradle plugin and in the sonar scanner context, i can see the property, so it...
Read more >
Do You Know Your Insurance Policy's Coverage Exclusions?
The CGL policy's “Your Work” exclusion means that there would be no insurance for such a claim (usually brought as a breach of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found