简体   繁体   English

如何从代码覆盖中排除 .NET 6 Minimal API?

[英]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.CodeAnalysisExcludeFromCodeCoverage属性添加到它们的所有包含类中。 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.

相关问题 如何从代码覆盖率中测试或排除私有无法访问的代码 - How to test or exclude private unreachable code from code coverage 如何从代码覆盖率中排除属性中的 lambda 函数? - How can I exclude lambda functions in properties from code coverage? 如何从代码覆盖中排除 MVC Razor 视图? - How to exclude MVC Razor view from code coverage? 从代码覆盖范围中排除类-.runsettings - Exclude class from code coverage - .runsettings NCover:从coverage中排除不可执行的代码行 - NCover: Exclude unexecutable line of code from coverage 如何排除程序文件精细代码覆盖 - how to exclude program file Fine code coverage 如何使 Sonarqube 从覆盖率测量中排除 .NET (C#) 项目 - How to make Sonarqube exclude a .NET (C#) project from coverage measures 如何在 .NET 7 中使用最小的 API setup.UseEndpoints for prometheus? - How setup .UseEndpoints for prometheus in .NET 7 with minimal API? TeamCity-从.NET项目测试覆盖范围中排除实体框架类 - TeamCity - exclude Entity Framework classes from .NET project test coverage 我可以从代码覆盖中排除部分方法吗? - Can I exclude part of a method from code coverage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM