开发者

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.

Here is the MSTest version:

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:

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. 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.

(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.)

Anyway, my questions:

  1. Is it true that NUnit requires "magic strings" as in the above exampl开发者_开发百科e, or is there a different and reasonably efficient way to write the same assertions without magic strings? (Note: I'm referring only to "Name", not to "Naught" and "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. 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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜