Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude a .NET 6 Minimal API from code coverage?

Tags:

c#

.net

.net-6.0

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();
like image 489
Tolvic Avatar asked Apr 28 '26 18:04

Tolvic


1 Answers

Your Program.cs shoud look like this:

using Microsoft.AspNetCore.Mvc;
using SharedDataLayer.Repositories;
using System.Diagnostics.CodeAnalysis;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSingleton<PaymentRepository>();

var app = builder.Build();

app.MapGet("/Payments", ([FromServices] PaymentRepository repo) =>
{
    return repo.GetPayments();
});


app.Run();`

[ExcludeFromCodeCoverage]
public partial class Program { }
like image 97
Ronald Ramos Avatar answered May 01 '26 07:05

Ronald Ramos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!