Is it true that NUnit requires magic strings, and MSTest doesn't?

I am trying to make a decision on a unit test structure. I just read this comparison between NUnit and MSTest , and in the example under “Our third test - NUnit” magic strings are used to indicate the name of the property, and the version of MSTest is not.

Here is the version of MSTest:

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

Here is the version of 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) argues that the version of NUnit is better because its free style is more readable, while several commentators disagree that NUnit requires "magic strings" for property names. The author did not seem to consider this criticism, but to me it seems like a pretty serious flaw for NUnit when it comes to refactoring.

(Aside from me, I love LINQ, so I do not find MSTest statements even remotely difficult to intercept. And as aside from third-party, I believe that the LINQ queries used by the author in his example are incorrect - Where expression, resulting in no records return an empty IEnumerable<T> not null .)

Anyway, my questions are:

  • Is it true that NUnit requires “magic lines” as in the above example, or is there another and reasonably efficient way to write the same statements without magic lines? (Note: I mean only “Name”, not “Nothing” and “Cross”.)

  • I do not care?

+4
source share
1 answer

The only thing that should matter at the end is if the statement you make is valid or not. There are several ways to test things, both in NUnit and in MSTest. You do not need to use free syntax, you can write the same statement as this, for example:

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

Personally, I prefer free syntax for simple (r) checks like

 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 worry about the details of implementing a particular testing environment. I know that this really does not answer your question, but I believe that this should not be a problem due to the fact that he can do something in more than one way.

(As a side note, I prefer NUnit because of the smooth syntax, but also things like Assert.Throws and its opposite Assert.DoesNotThrow , as well as convenient statement classes like CollectionAssert and StringAssert , none of which are present in MSTest.

+6
source

All Articles