简体   繁体   中英

Ms Test or NUnit?

与内置的MsTest相比,选择NUnit进行单元/集成测试有什么优势吗?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM