C#

My new team uses some MSTest for our C# unit tests, but mostly uses NUnit. Though NUnit has been around a long dern time now, I’ve never found myself in a professional environment that favored it. So lately, I’ve had no choice but to dig into it, and I quickly found it to be far preferable to MSTest for one simple reason: clarity.

Here’s the MSTest version of a simple StringCalculator test:

1: [TestMethod]

2: publicvoid Add_1And2Returns3_Success()

3: {

4: var result = StringCalculator.Add("1,2");

5: Assert.AreEqual(3, result);

6: }

This isn’t bad. But you know what often happens when an engineer uses MSTest’s AreEqual method? The order of the parameters gets changed.

1: Assert.AreEqual(result, 3);

Technically, the above still works. But what the code says is, “Assert that my expected value, which is the result of the operation I am testing, is equal to the actual result my code returned, the static value 3.” That’s total nonsense.

NUnit makes the value that is expected and the actual value returned by the method under test much more explicit. You can still reverse the actual/expected order, and like with MSTest it will still work, but the naturalness of the syntax really helps prevent that from happening.

1: [Test]

2: publicvoid Add_1And2Returns3_Success()

3: {

4: var result = StringCalculator.Add("1,2");

5: Assert.That(result, Is.EqualTo(3));

6: }

Remember that one of the three pillars of unit tests is readability. (The other two are trustworthiness and maintainability; see Chapter 8 of The Art of Unit Testing by Roy Osherove.) NUnit is far preferable to MSTest on readability grounds alone.