-
Notifications
You must be signed in to change notification settings - Fork 385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ExcludeFromCodeCoverage attribute is ignored in an static class in a ASP.NET Core 3.1 project #670
Comments
Thank's @jmoralesv for reporting this, we'll take a look ASAP. |
@jmoralesv the code inside the static method with attribute applied has got some async/await code? |
Hi @MarcoRossignoli , using System.Diagnostics.CodeAnalysis;
using System.Net;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
[ExcludeFromCodeCoverage]
public static class ExceptionMiddlewareExtensions
{
[ExcludeFromCodeCoverage]
public static void ConfigureExceptionHandler(this IApplicationBuilder app, ILogger logger)
{
app.UseExceptionHandler(appBuilder =>
{
appBuilder.Run(async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "application/json";
var contextFeature = context.Features.Get<IExceptionHandlerPathFeature>();
if (contextFeature != null)
{
logger.LogError($"Something went wrong at {contextFeature.Path}. Error: {contextFeature.Error}");
await context.Response.WriteAsync($"{ 'StatusCode': {context.Response.StatusCode}, 'Message': 'Internal Server Error' }");
}
});
});
}
} The extra attribute I checked issue #129 and I think it could be the same, although the context is different, as my static class is being referenced by my A snippet of my Startup class is as follows: [ExcludeFromCodeCoverage]
public class Startup
{
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
{
app.ConfigureExceptionHandler(logger);
// The rest of the code was removed for brevity
}
} Please let me know if you need any other information. |
I have to repro but I think that it's the same issue, static class is present because the async/await state machine is generated inside that class by compiler. |
Hi @MarcoRossignoli , I agree with you, it's the same issue, having async/await code with static methods or classes makes Coverlet ignore the |
Don't worry your code fragment is enough to me |
Hi @MarcoRossignoli, |
Sure thing! |
@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. For now you can remove that file from coverage using filters,
Or you can use filter by filename
In my repro I put the extension inside a file called |
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
The text was updated successfully, but these errors were encountered: