单元测试方法中是否需要总结

发布于 2024-08-05 06:09:46 字数 1432 浏览 3 评论 0原文

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

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

发布评论

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

评论(7

西瑶 2024-08-12 06:09:46

实际上,我更喜欢使用 DescriptionAttribute 而不是摘要标签。原因是“描述”属性的值将显示在结果文件中。当您仅查看日志文件时,它使故障更容易理解

[TestMethod,Description("Ensure feature X doesn't regress Y")]
public void TestFeatureX42() {
  ..
}

I actually prefer to use the DescriptionAttribute over a summary tag. The reason being that the value of the Description attribute will show up in a results file. It makes failures easier to understand when you're just looking at a log file

[TestMethod,Description("Ensure feature X doesn't regress Y")]
public void TestFeatureX42() {
  ..
}
Hello爱情风 2024-08-12 06:09:46

我认为长描述性名称比 XML 注释更重要。由于单元测试不会成为 API 的一部分,因此您不需要 XML 注释。

例如: 比

[TestMethod]
public void FormatException_Should_Thrown_When_Parse_CountryLine_Containing_InvalidNumber()
{
  ...
}

///<summary>
/// Exception Should Thrown When Parse CountryLine Containing InvalidNumber
///</summary>
[TestMethod]
public void Test42()
{
  ...
}

XML 注释应该用于记录 API 和框架。

I think the long descriptive name is more important than the XML comment. Since the unit test isn't going to be part of an API, you don't need the XML comment.

For Example:

[TestMethod]
public void FormatException_Should_Thrown_When_Parse_CountryLine_Containing_InvalidNumber()
{
  ...
}

is more useful than:

///<summary>
/// Exception Should Thrown When Parse CountryLine Containing InvalidNumber
///</summary>
[TestMethod]
public void Test42()
{
  ...
}

XML Comments should be used for documenting APIs and frameworks.

弥繁 2024-08-12 06:09:46

就我个人而言,我尝试使测试足够容易阅读,以免文档变得多余。我在测试方法中使用内联注释来解释为什么我正在以特定的方式做某事,而不是我在做什么

Personally, I try to make the tests easy enough to read that documentation would be redundant. I use inline comments within the test method to explain why I'm doing something a particular way, not what I'm doing.

夜访吸血鬼 2024-08-12 06:09:46

当摘要可以提供更多可以/应该编码在方法名称中的信息时,有必要添加摘要。请注意,当我在提到任何文档时说“必要”时,我的意思是“有必要将 100% 所需的上下文/细节/细微差别传达给继承该项目的新编码员或 5 年后的您自己”。

It is necessary to add a summary when the summary can provide more information that can/should be encoded in method name. Please note that when I say "necessary" when referring to any documentation, I mean is "necessary to convey 100% of needed context/detail/nuances to a new coder inheriting the project or to you yourself 5 years later".

娇纵 2024-08-12 06:09:46

没有必要,但如果您觉得 XML 注释的附加值超出了单元测试本身的名称(看起来很全面),那么您将为其他开发人员提供服务。

如果摘要本质上是单元测试方法名称的直接重复,那么我认为这是矫枉过正。

Not necessary, but if you feel the XML comments add value above and beyond the name of the unit test itself (which looks like to be comprehensive) then you'll be doing other developers a service.

If the summary is essentially a direct duplicate of the unit test method name, then I think this is overkill.

箜明 2024-08-12 06:09:46

对于上面的例子,我想说这是没有必要的,除非您使用从源代码中提取文档的工具(例如 javadoc 或其他东西)。

一个常见的经验法则是,代码说明了您正在做什么,注释说明了原因,但由于名称非常冗长(我认为这是可以的,因为没有人必须键入它),我不认为该评论有任何贡献。

For the example above, I would say that it's not necessary, unless you use a tool that extracts documentation from source (like javadoc or something).

A common rule of thumb is that the code says what you're doing and the comment says why, but since the name is so very verbose (which I think is ok, since no one ever has to type it) I don't think that the comment contributes anything.

初熏 2024-08-12 06:09:46

如果您有描述性方法名称,则完全不需要 XML 注释。这对于单元测试方法来说是必须的。

您已经走在正确的道路上,拥有一个描述性的测试方法名称。 (许多敏捷和TDD实践者认为测试方法应该包括“应该”,例如链接文本这篇博文。

就我个人而言,我喜欢这样的方法名称:

MyClass_OnInvalidInput_ShouldThrowFormatException()

但这只是我个人的偏好。

An XML comment is totally unnecessary if you have a descriptive method name. And it's a must for unit-test methods.

You're already on the right track having a descriptive test method name. (Many agile and TDD practitioners believe that a test method should include "should", e.g. as shown in link text this blog post.

Personally, I like method names like this:

MyClass_OnInvalidInput_ShouldThrowFormatException()

But that's merely my personal preference.

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