0

Is there any advantage to picking NUnit for unit/integration testing vs the built in MsTest?

Jeremy Roberts
  • 1,208
  • 8
  • 19
AngryHacker
  • 59,598
  • 102
  • 325
  • 594
  • 1
    possible duplicate of [Are there real differences between NUnit and Microsoft's Unit Testing Framework (VS 2008)?](http://stackoverflow.com/questions/2422238/are-there-real-differences-between-nunit-and-microsofts-unit-testing-framework) – JohnFx May 26 '10 at 23:24
  • There's a similar question [here](http://stackoverflow.com/questions/2422238/are-there-real-differences-between-nunit-and-microsofts-unit-testing-framework/2422339#2422339), in which I gave a link to a blog post by Roy Osherove, highlighting the differences. – Paddyslacker May 25 '10 at 00:26

1 Answers1

2

They are pretty similar. Differences are subtle.

  • NUnit has testcases for parametrized tests; MSTest does not.

You can write

[TestCase(1, "one)]
[TestCase(2, "two)]
[TestCase(3, "three)]
[TestCase(4, "four)]
public void CanTranslate(int number, string expectedTranslation)
{  
     var translation = _sut.Translate(number);

     translation.Should().Be.EqualTo(expectedTranslation);
}

rather than writing 4 tests or using a cycle inside the test. Failing tests' error messages will be much clearer and test results will be always conveniently grouped.

  • NUnit tends to be a bit more complete and flexible. For example, it comes with a cool set of attributes like Combinatorial.

For example

[Test, Combinatorial]
public void MyTest([Values(1,2,3)] int x, [Values("A","B")] string s)
{
    ...
}

which is equivalent to running the tests

MyTest(1, "A")
MyTest(1, "B")
MyTest(2, "A")
MyTest(2, "B")
MyTest(3, "A")
MyTest(3, "B")

(see the original page here)

  • MSTest always instantiates a new instance of the test class for each test method being executed. This is very useful, since prior to each single test, Setup and TearDown methods will be run and every instance variables will be reset. With NUnit you have to take care of instance variables eventually shared between tests (though this shouldn't be a bis issue: a well designed test should be isolated by design)

    • With NUnit an abstract classes can be a test fixtures and you can inherit other test fixtures from it. MsTest does not have this feature.
  • MSTest is well integrated with Visual Studio. You'd need a third party plugin to effectively work with NUnit, for example ReSharper, Test Driven .NET or NCrunch

  • NUnit has a fluent version of Assert, so you can write

for example

Assert.That(result).Is.GreaterThan(9)

rather than

Assert.Greater(9, result);

With SharpTestEx you can even write:

result.Should().Be.GreaterThan(9);

and take advantage of the strong typed IntelliSense.

Arialdo Martini
  • 4,427
  • 3
  • 31
  • 42