【问题标题】:Ms Test or NUnit?Ms Test 还是 NUnit?
【发布时间】:2011-02-23 11:30:25
【问题描述】:

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

【问题讨论】:

标签: visual-studio-2008 unit-testing nunit mstest integration-testing


【解决方案1】:

它们非常相似。差异是微妙的。

  • NUnit 有用于参数化测试的测试用例; MSTest 没有。

你可以写

[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 个测试或在测试中使用循环。失败测试的错误信息会更清晰,测试结果总是很方便地分组。

  • NUnit 往往更加完整和灵活。例如,它带有一组很酷的 attributes,例如 Combinatorial。

例如

[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,您必须处理最终在测试之间共享的实例变量(尽管这不应该是一个问题:设计良好的测试应该被设计隔离)

    • 使用 NUnit,抽象类可以作为测试装置,您可以从中继承其他测试装置。 MsTest 没有此功能。
  • MSTest 与 Visual Studio 完美集成。您需要第三方插件才能有效地使用 NUnit,例如 ReSharper、Test Driven .NETNCrunch

  • NUnit 有一个流畅的 Assert 版本,所以你可以写

例如

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

而不是

Assert.Greater(9, result);

有了SharpTestEx,你甚至可以写:

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

并利用强类型 IntelliSense。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多