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.

ExcludeFromCodeCoverage attribute is ignored in an static class in a ASP.NET Core 3.1 project

See original GitHub issue

Hi all, I’m facing an issue when using Coverlet in a ASP.NET Core 3.1 project. I can see that a static class, which has a static void method, is not being excluded from code coverage, although both the class and the method have the ExcludeFromCodeCoverage attribute applied, from the namespace System.Diagnostics.CodeAnalysis.

This static method is an extension method referenced by the Startup class in my ASP.NET Core 3.1 project, Startup and Program classes are also excluded from code coverage and they don’t appear in the code coverage results, however my static class and its static void method appear in the results.

Is this a known issue with Coverlet, .NET Core 3.1 and static classes and methods? This is the command I’m running to get the results:

dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura

My unit test project references coverlet.msbuild with version 2.7.0. It also references Xunit, version 2.4.1, and my ASP.NET Core 3.1 project. The unit test project also targets .NET Core 3.1. This is extracted from my csproj file for the reference to coverlet.msbuild:

<PackageReference Include="coverlet.msbuild" Version="2.7.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>

I found this issue in my build pipeline in Azure DevOps too.

Finally, I’d like to mention that the code coverage tools included in Visual Studio 2019 Enterprise, version 16.4.2, do exclude the class and the method from code coverage, so I think this is an issue with Coverlet itself, or the commands I’m using with dotnet test.

Please advise.

Regards, Jorge

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
MarcoRossignolicommented, Jan 5, 2020

@jmoralesv I did a repro and the issue is “related” to the other, btw for now I’ll leave this open because I don’t know if the fix will work with “nested” async machine state.

image

For now you can remove that file from coverage using filters,

dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=./Results/ /p:ExcludeByAttribute="CompilerGeneratedAttribute"

ExcludeByAttribute exclude by attribute name and CompilerGeneratedAttribute is the attribute added by the compiler to the state machines

image

Or you can use filter by filename

dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=./Results/ /p:ExcludeByFile=\"**/Ext.cs\"

In my repro I put the extension inside a file called Ext.cs

1reaction
MarcoRossignolicommented, Jan 5, 2020

Sure thing!

Read more comments on GitHub >

github_iconTop Results From Across the Web

ExcludeFromCodeCoverage not work with asp.net core 3.1 ...
I created asp.net core 3.1 MS unit test project and everything work as expected but [ExcludeFromCodeCoverage] not working.
Read more >
ExcludeFromCodeCoverage ignored (in async code)
I get blocks not covered in an async method excluded from code coverage. Code : [TestFixture] internal static class ExcludedFromCoverageProblem ...
Read more >
How to exclude code from code coverage
The easiest way to exclude code from code coverage analysis is to use ExcludeFromCodeCoverage attribute. This attribute tells tooling that class ...
Read more >
ExcludeFromCodeCoverageAttri...
Placing this attribute on a class or a structure excludes all the members of that class or structure from the collection of code...
Read more >
C# static code analysis: "ExcludeFromCodeCoverage" ...
The ExcludeFromCodeCoverageAttribute is used to exclude portions of code from code coverage reporting. It is a bad practice to retain code that is...
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