Ms Test 还是 NUnit?

发布于 2024-09-02 09:19:40 字数 47 浏览 2 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弥枳 2024-09-09 09:19:40

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

  • 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 往往更加完整和灵活。例如,它带有一组很酷的属性 就像组合一样。

例如,

[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")

(请参阅原始页面 此处)

  • MSTest 始终为每个正在执行的测试方法实例化测试类的新实例。这非常有用,因为在每个单独的测试之前,将运行 Setup 和 TearDown 方法,并且将重置每个实例变量。使用 NUnit,您必须处理最终在测试之间共享的实例变量(尽管这不应该是一个大问题:设计良好的测试应该通过设计进行隔离)

  • 使用 NUnit,抽象类可以是测试装置,并且您可以继承其他测试装置。 MsTest没有这个功能。

  • MSTest 与 Visual Studio 集成良好。您需要第三方插件才能有效地使用 NUnit,例如 ReSharper、测试驱动 .NETNCrunch

  • NUnit 有一个流畅版本的 Assert,因此您可以编写

示例

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

,而不是

Assert.Greater(9, result);

使用 SharpTestEx 您甚至可以编写:

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

并利用强类型 IntelliSense。

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 big 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文