是Test还是NUnit? [英] Ms Test or NUnit?

查看:310
本文介绍了是Test还是NUnit?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与内置的MsTest相比,选择NUnit进行单元/集成测试是否有任何优势?

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

推荐答案

它们非常相似.差异微妙.

They are pretty similar. Differences are subtle.

  • 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个测试或在测试内部使用一个循环.失败的测试错误消息将更加清晰,并且测试结果将始终被方便地分组.

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趋向于更加完整和灵活.例如,它带有一组很酷的属性像组合.
  • NUnit tends to be a bit more complete and flexible. For example, it comes with a cool set of attributes like 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")

(请参见原始页面此处)

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

  • 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)

  • 使用NUnit,抽象类可以成为测试夹具,您可以从中继承其他测试夹具. MsTest没有此功能.

MSTest与Visual Studio很好地集成在一起.您需要第三方插件才能有效地与NUnit配合使用,例如ReSharper,测试驱动的.NET NCrunch

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具有流利的Assert版本,因此您可以编写

NUnit has a fluent version of Assert, so you can write

例如

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

而不是

Assert.Greater(9, result);

使用 SharpTestEx ,您甚至可以编写:

With SharpTestEx you can even write:

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

并利用强类型的IntelliSense.

and take advantage of the strong typed IntelliSense.

这篇关于是Test还是NUnit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆