问题
Is there any advantage to picking NUnit for unit/integration testing vs the built in MsTest?
回答1:
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.
来源:https://stackoverflow.com/questions/2901219/ms-test-or-nunit