我将在工作中开始一个新项目,并想进入单元测试。我们将使用Visual Studio 2008, c#和ASP。NET MVC之类的东西。我正在考虑使用NUnit或Visual Studio 2008的内置测试项目,但我也愿意研究其他建议。是一种系统比另一种更好,还是比另一种更容易使用/理解?
我希望让这个项目成为我们未来发展努力的“最佳实践”。
我将在工作中开始一个新项目,并想进入单元测试。我们将使用Visual Studio 2008, c#和ASP。NET MVC之类的东西。我正在考虑使用NUnit或Visual Studio 2008的内置测试项目,但我也愿意研究其他建议。是一种系统比另一种更好,还是比另一种更容易使用/理解?
我希望让这个项目成为我们未来发展努力的“最佳实践”。
当前回答
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
或任何其他转换…:-)这里的用法只是编译器的别名。
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.
随着。net 4.0中代码契约系统的发布和静态检查器的可用性,理论上你将需要编写更少的测试用例,而像Pex这样的工具将有助于识别这些用例。将这一点与前面的讨论联系起来,如果您需要更少地使用单元测试,因为您的契约会覆盖您的尾部,那么为什么不直接使用内置组件呢,因为这样可以少管理一个依赖项。这些天来,我只追求简单。: -)
参见:
微软Pex -自动化单元测试 使用Visual Studio 2010和c# 4.0与Pex生成单元测试
单元测试框架实际上并不重要,因为你可以用单独的项目文件和条件编译转换测试类(就像这样,Visual Studio→NUnit):
#if !NUNIT using Microsoft.VisualStudio.TestTools.UnitTesting; #else using NUnit.Framework; using TestClass = NUnit.Framework.TestFixtureAttribute; using TestMethod = NUnit.Framework.TestAttribute; using TestInitialize = NUnit.Framework.SetUpAttribute; using TestCleanup = NUnit.Framework.TearDownAttribute; using TestContext = System.String; using DeploymentItem = NUnit.Framework.DescriptionAttribute; #endif
TestDriven。Net插件很好,不是很贵…对于普通的Visual Studio 2008,你必须从你的测试类或测试列表中找到测试。TestDriven。Net中,您可以直接从正在测试的类中运行测试。毕竟,单元测试应该易于维护,并且离开发人员很近。
我已经使用NUnit两年了。一切都很好,但是我不得不说Visual Studio中的单元测试系统非常好,因为它在GUI中,可以更容易地对私有函数进行测试,而不必搞得乱七八糟。
此外,Visual Studio的单元测试允许你做覆盖和其他NUnit单独不能做的事情。