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

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


当前回答

Visual Studio测试框架的一个小麻烦是,它会创建许多测试运行文件,这些文件往往会使您的项目目录变得混乱——尽管这并不是什么大问题。

另外,如果你缺少一个像TestDriven这样的插件。NET,你不能在Visual Studio环境中调试你的NUnit(或MbUnit, xUnit等)单元测试,而你可以在Microsoft Visual Studio测试框架中进行,这是内置的。

其他回答

xUnit是新项目的另一种可能。它的语法可能更直观,但它与其他框架并不兼容。

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两年了。一切都很好,但是我不得不说Visual Studio中的单元测试系统非常好,因为它在GUI中,可以更容易地对私有函数进行测试,而不必搞得乱七八糟。

此外,Visual Studio的单元测试允许你做覆盖和其他NUnit单独不能做的事情。

Daok列出了Visual Studio 2008测试项目的所有优点。下面是NUnit的优点。

NUnit has a mocking framework. NUnit can be run outside of the IDE. This can be useful if you want to run tests on a non-Microsoft build server, like CruiseControl.NET. NUnit has more versions coming out than visual studio. You don't have to wait years for a new version. And you don't have to install a new version of the IDE to get new features. There are extensions being developed for NUnit, like row-tests, etc. Visual Studio tests take a long time to start up for some reason. This is better in Visual Studio 2008, but it is still too slow for my taste. Quickly running a test to see if you didn't break something can take too long. NUnit with something like Testdriven.Net to run tests from the IDE is actually much faster. Especially when running single tests. According to Kjetil Klaussen, this is caused by the Visual Studio testrunner. Running MSTest tests in TestDriven.Net makes MSTest performance comparable to NUnit.

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

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