【发布时间】:2011-02-23 11:30:25
【问题描述】:
选择 NUnit 进行单元/集成测试与内置的 MsTest 相比有什么优势吗?
【问题讨论】:
-
有一个类似的问题here,其中我提供了一个指向 Roy Osherove 的博客文章的链接,强调了不同之处。
标签: visual-studio-2008 unit-testing nunit mstest integration-testing
选择 NUnit 进行单元/集成测试与内置的 MsTest 相比有什么优势吗?
【问题讨论】:
标签: visual-studio-2008 unit-testing nunit mstest integration-testing
它们非常相似。差异是微妙的。
你可以写
[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);
}
而不是编写 4 个测试或在测试中使用循环。失败测试的错误信息会更清晰,测试结果总是很方便地分组。
例如
[Test, Combinatorial]
public void MyTest([Values(1,2,3)] int x, [Values("A","B")] string s)
{
...
}
相当于运行测试
MyTest(1, "A")
MyTest(1, "B")
MyTest(2, "A")
MyTest(2, "B")
MyTest(3, "A")
MyTest(3, "B")
(见原页面here)
MSTest 总是为每个正在执行的测试方法实例化一个新的测试类实例。这非常有用,因为在每个单独的测试之前,Setup 和 TearDown 方法将运行并且每个实例变量都将被重置。使用 NUnit,您必须处理最终在测试之间共享的实例变量(尽管这不应该是一个问题:设计良好的测试应该被设计隔离)
MSTest 与 Visual Studio 完美集成。您需要第三方插件才能有效地使用 NUnit,例如 ReSharper、Test Driven .NET 或 NCrunch
NUnit 有一个流畅的 Assert 版本,所以你可以写
例如
Assert.That(result).Is.GreaterThan(9)
而不是
Assert.Greater(9, result);
有了SharpTestEx,你甚至可以写:
result.Should().Be.GreaterThan(9);
并利用强类型 IntelliSense。
【讨论】: