'How to exclude a .NET 6 Minimal API from code coverage?
I maintain a class library which contains several reference implementations to demonstrate how the library should be used. I have reference implementations for .Net Framework, core, .NET 5 and now I have added a reference implementation for .NET 6 using minimal APIs.
For all of my other reference implementations I have added the ExcludeFromCodeCoverage
attribute using System.Diagnostics.CodeAnalysis
to all of their containing classes. How would I do something similar for a .NET 6 minimal API?
My program.cs
looks like this:
using Microsoft.AspNetCore.Mvc;
using SharedDataLayer.Repositories;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<PaymentRepository>();
var app = builder.Build();
app.MapGet("/Payments", ([FromServices] PaymentRepository repo) =>
{
return repo.GetPayments();
});
app.Run();
Solution 1:[1]
I've tested out the suggestions from @PanagiotisKanavos and @Tolvic in the comments on the initial question and can confirm that adding the following to the top of your program.cs
does work and the file is excluded from code coverage.
using System.Diagnostics.CodeAnalysis;
[assembly: ExcludeFromCodeCoverage]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Martin Kearn |