C#9/10顶级语句,排除FromCodeCoverage-Attribute?

发布于 2025-01-24 06:47:31 字数 582 浏览 6 评论 0 原文

我通常将属性设置为我的程序类 ,因为无论如何都没有对此类的单位测试(或者也没有意义),因此它不会显示为“缺少“在覆盖报告中缺少:

[ExcludeFromCodeCoverage]
public static class Program
{
    public static void Main(string[] args)
    {
       // do something awesome
    }
}

但是使用顶级语句我不知道该如何处理。正如我在这里发现的那样,似乎无法设置属性: a>

到目前为止,我坚持经典的班级声明,但是在单位测试代码覆盖范围时,也许他们考虑了其他事情?

I usually set the attribute [ExcludeFromCodeCoverage] to my Program class, as there are no unit tests for this class possible anyways (or don't make sense either), so it doesn't show up as "missing" in the coverage report:

[ExcludeFromCodeCoverage]
public static class Program
{
    public static void Main(string[] args)
    {
       // do something awesome
    }
}

But with top-level statements I don't know how to handle this. It seems not to be possible to set attributes, as I found here: https://stackoverflow.com/a/69962982/1099519

So far, I stick to the classic Class declaration, but maybe they thought about something else, when it comes to unit test code coverage?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久隐师 2025-01-31 06:47:31

由于C#10更改了顶级语句的生成,现在您可以将部分程序类添加到顶级语句的末尾(或在单独的文件中),然后添加属性:

[ExcludeFromCodeCoverage]
public partial class Program { }

请注意,在初始功能规范(C#9)状态编译器使用的实际名称取决于实现,但是由于使用部分类别的C#10和Net 6是单位测试 asp.net核心应用程序顶级语句。

但是我个人会说,如果您需要这样的“高级”方案,则应切换回“普通旧” program 类。

Since C# 10 the top-level statement generation was changed and now you can add partial Program class to the end of top-level statement (or in separate file) and add attribute to it:

[ExcludeFromCodeCoverage]
public partial class Program { }

Note that in initial feature specification (for C# 9) states that the actual names used by compiler are implementation dependent but since C# 10 and NET 6 using partial class is one of the recommended approaches for unit testing ASP.NET Core apps which use top-level statements.

But personally I would say that if you need such "advanced" scenarios you should switch back to the "plain old" Program classes.

陌上芳菲 2025-01-31 06:47:31

这是对我有用的(.NET 7):

[ExcludeFromCodeCoverage]
public static partial class Program { }

看起来生成的 program 类是静态的,因此将放置 [excludeFromCodeCoverage] 在非静态部分 program上 /代码>类不适用于顶级代码。不确定为什么这样,找不到文档,说明生成的程序是静态的。

This is what worked for me (.NET 7):

[ExcludeFromCodeCoverage]
public static partial class Program { }

Looks like the generated Program class is static, so putting [ExcludeFromCodeCoverage] on non-static partial Program class doesn't apply to top-level code. Not sure why that is, can't find documentation stating that generated Program is static.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文