简体   繁体   English

NUnit是否需要“魔术字符串”,而MSTest不需要?

[英]Is it true that NUnit requires “magic strings” while MSTest does not?

I'm trying to decide on a unit testing framework. 我正在尝试决定一个单元测试框架。 I was just reading this comparison between NUnit and MSTest , and in the example under "Our Third Test – NUnit", magic strings are used to refer to a property name, while the MSTest version does not. 我刚刚阅读了NUnit和MSTest之间的这种比较 ,在“我们的第三次测试 - NUnit”下的示例中,魔术字符串用于表示属性名称,而MSTest版本则不是。

Here is the MSTest version: 这是MSTest版本:

Assert.IsNotNull(b.Players.Where(x => x.Name.Equals("Cross")));
Assert.IsNotNull(b.Players.Where(x => x.Name.Equals("Nought")));

Here is the NUnit version: 这是NUnit版本:

Assert.That(b.Players, Has.Some.With.Property("Name").EqualTo("Nought"));
Assert.That(b.Players, Has.Some.With.Property("Name").EqualTo("Cross"));

The author (bearbonescoder) claims that the NUnit version is better because its fluent style is more readable, while several commenters disagreed because NUnit requires "magic strings" for property names. 作者(bearbonescoder)声称NUnit版本更好,因为它的流畅风格更具可读性,而一些评论者不同意,因为NUnit需要属性名称的“魔术字符串”。 The author did not appear to address this criticism, but to me, this seems like a rather serious disadvantage for NUnit when it comes to refactoring. 作者似乎没有解决这个批评,但对我来说,这对于NUnit而言在重构方面似乎是一个相当严重的劣势。

(As an aside, I love LINQ, so I don't find MSTest statements even remotely difficult to grok. And as an aside of an aside, I believe the LINQ queries the author used in his example are incorrect--a Where expression that results in no records would return an empty IEnumerable<T> not null .) (顺便说一句,我喜欢LINQ,所以我发现MSTest语句甚至远远都难以理解。而且除了旁白之外,我相信作者在他的例子中使用的LINQ查询是不正确的 - 一个Where表达式结果没有记录会返回一个空的IEnumerable<T>不为null 。)

Anyway, my questions: 无论如何,我的问题:

  1. Is it true that NUnit requires "magic strings" as in the above example, or is there a different and reasonably efficient way to write the same assertions without magic strings? NUnit是否真的需要像上面例子那样的“魔术字符串”,或者是否有一种不同且合理有效的方法来编写没有魔术字符串的相同断言? (Note: I'm referring only to "Name", not to "Naught" and "Cross".) (注意:我只指“名字”,而不是“Naught”和“Cross”。)

  2. Should I care? 我应该关心吗?

The only thing that should matter in the end if the assertion you make is valid or not. 如果您做出的断言有效或无效,那么最后唯一重要的事情就是。 There are several ways to check for things, both in NUnit and MSTest. 在NUnit和MSTest中,有几种方法可以检查事物。 You don't have to use the fluent syntax, you could write the same assertion like this, for example: 您不必使用流畅的语法,您可以编写相同的断言,例如:

Assert.True(b.Players.Any(p => p.Name == "Cross");

Personally, I prefer the fluent syntax for simple(r) checks, such as 就个人而言,我更喜欢简单(r)检查的流利语法,例如

Assert.That(b.Name, Is.EqualTo("Stan"));

or 要么

Assert.That(b.Players, Is.Not.Null);

In the end you should use whatever you find more readable, and not bother yourself with the implementation details of a particular testing framework. 最后,您应该使用您认为更具可读性的任何内容,而不必担心特定测试框架的实现细节。 I know this really doesn't answer your question, but I believe it shouldn't be an issue due to the fact that it's possible to do things in more than one way. 我知道这确实没有回答你的问题,但我认为这不应该是一个问题,因为它可以以多种方式做事。

(As a side note, I prefer NUnit because of the fluent syntax, but also things like Assert.Throws and its opposite Assert.DoesNotThrow , as well as convenient assertion classes such as CollectionAssert and StringAssert , none of which are present in MSTest. (作为旁注,我更喜欢NUnit,因为它具有流畅的语法,但也喜欢Assert.Throws及其相反的Assert.DoesNotThrow ,以及方便的断言类,如CollectionAssertStringAssertStringAssert中都没有这些类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM