[英]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.我维护一个 class 库,其中包含几个参考实现来演示应该如何使用该库。 I have reference implementations for.Net Framework, core, .NET 5 and now I have added a reference implementation for .NET 6 using minimal APIs.
我有 .Net Framework 核心 .NET 5 的参考实现,现在我使用最少的 API 添加了 .NET 6 的参考实现。
For all of my other reference implementations I have added the ExcludeFromCodeCoverage
attribute using System.Diagnostics.CodeAnalysis
to all of their containing classes.对于我的所有其他参考实现,我已经使用
System.Diagnostics.CodeAnalysis
将ExcludeFromCodeCoverage
属性添加到它们的所有包含类中。 How would I do something similar for a .NET 6 minimal API?我将如何为 .NET 6 最小 API 做类似的事情?
My program.cs
looks like this:我的
program.cs
看起来像这样:
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();
Your Program.cs shoud look like this:你的 Program.cs 应该是这样的:
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 { }
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.我已经在最初问题的评论中测试了@PanagiotisKanavos 和@Tolvic 的建议,并且可以确认将以下内容添加到您的
program.cs
的顶部确实有效,并且该文件被排除在代码覆盖范围之外。
using System.Diagnostics.CodeAnalysis;
[assembly: ExcludeFromCodeCoverage]
using System.Diagnostics.CodeAnalysis;使用 System.Diagnostics.CodeAnalysis;
[assembly: ExcludeFromCodeCoverage] worked for me [程序集:ExcludeFromCodeCoverage] 为我工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.