我将在工作中开始一个新项目,并想进入单元测试。我们将使用Visual Studio 2008, c#和ASP。NET MVC之类的东西。我正在考虑使用NUnit或Visual Studio 2008的内置测试项目,但我也愿意研究其他建议。是一种系统比另一种更好,还是比另一种更容易使用/理解?

我希望让这个项目成为我们未来发展努力的“最佳实践”。


当前回答

据我所知,目前。net有四个可用的单元测试框架:

NUnit 运行工具 MSTest xUnit

NUnit一直处于领先地位,但在过去一年左右,这种差距已经缩小。我自己还是更喜欢NUnit,特别是他们不久前添加了一个流畅的界面,这使得测试非常易读。

如果您刚刚开始进行单元测试,这可能没有太大区别。一旦你跟上了进度,你就能更好地判断哪个框架最适合你的需求。

其他回答

我不喜欢Visual Studio的内置测试框架,因为它迫使你创建一个单独的项目,而不是把你的测试作为你正在测试的项目的一部分。

我从MSTest开始,但我因为一个简单的原因而改变了。MSTest不支持从其他程序集继承测试方法。

我讨厌多次编写相同的测试。特别是在一个大型项目中,测试方法可以很容易地运行到100个测试。

NUnit正是我所需要的。NUnit唯一缺少的是Visual Studio插件,它可以显示每个测试的红色/绿色状态(就像VSTS一样)。

Visual Studio 2008内置单元测试框架的优点/变化:

The 2008 version now is available in professional editions (before it required expensive versions of Visual Studio, and this is just for developer unit testing) that left a lot of developers with the only choice of open/external testing frameworks. Built-in API supported by a single company. Use the same tools to to run and create tests (you may run them using the command line also MSTest). Simple design (granted without a mock framework, but this is a great starting point for many programmers). Long term support granted (I still remember what happened to NDoc, and I don't want to commit to a testing framework that might not be supported in five years, but I still consider NUnit a great framework). If using Team Foundation Server as your backend, you can create work items or bugs with the failed test data in a simple fashion.

我收到消息说“NUnit文件结构比VSTest更丰富”… 当然,如果你更喜欢NUnit的文件结构,你可以使用这个解决方案的另一种方式,像这样(NUnit→Visual Studio):

 #if !MSTEST
     using NUnit.Framework;
 #else
     using Microsoft.VisualStudio.TestTools.UnitTesting;
     using TestFixture = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute;
     using Test = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute;
     using SetUp = Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute;
     using TearDown = Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute;
 #endif

或任何其他转换…:-)这里的用法只是编译器的别名。

首先我想纠正一个错误的说法:你可以使用命令行在Visual Studio之外运行MSTest。尽管一些CI工具,如TeamCity,对NUnit有更好的支持(可能会随着MSTest变得更流行而改变)。

在我目前的项目中,我们两者都使用,唯一的大区别是,我们发现MSTest总是作为32位运行,而NUnit运行为32位或64位测试,这只在你的代码使用32/64位依赖的本机代码时才重要。