我将在工作中开始一个新项目,并想进入单元测试。我们将使用Visual Studio 2008, c#和ASP。NET MVC之类的东西。我正在考虑使用NUnit或Visual Studio 2008的内置测试项目,但我也愿意研究其他建议。是一种系统比另一种更好,还是比另一种更容易使用/理解?
我希望让这个项目成为我们未来发展努力的“最佳实践”。
我将在工作中开始一个新项目,并想进入单元测试。我们将使用Visual Studio 2008, c#和ASP。NET MVC之类的东西。我正在考虑使用NUnit或Visual Studio 2008的内置测试项目,但我也愿意研究其他建议。是一种系统比另一种更好,还是比另一种更容易使用/理解?
我希望让这个项目成为我们未来发展努力的“最佳实践”。
当前回答
如果你正在考虑MSTest或NUnit,那么我建议你看看MbUnit。我的理由是
TestDriven.Net compatibility. Nothing beats have TestDriven.Net.ReRunWithDebugger bound to a keyboard combination. The Gallio framework. Gallio is a test runner like NUnit's. The only difference is it doesn't care if you wrote your tests in NUnit, MSTest, xUnit or MbUnit. They all get run. Compatibility with NUnit. All features in NUnit are supported by MbUnit. I think you don't even need to change your attributes (will have to check that), just your reference and usings. Collection asserts. MbUnit has more Assert cases, including the CollectionAssert class. Basically you no longer need to write your own tests to see if two collections are the same. Combinatorial tests. Wouldn't it be cool if you could supply two sets of data and get a test for all the combinations of data? It is in MbUnit.
我最初选择MbUnit是因为它的[RowTest ....我找不到一个回去的理由。我把我所有的活动测试套件从NUnit移过来,再也没有回头。从那时起,我已经将两个不同的开发团队转换为收益。
其他回答
据我所知,目前。net有四个可用的单元测试框架:
NUnit 运行工具 MSTest xUnit
NUnit一直处于领先地位,但在过去一年左右,这种差距已经缩小。我自己还是更喜欢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.
我已经使用NUnit两年了。一切都很好,但是我不得不说Visual Studio中的单元测试系统非常好,因为它在GUI中,可以更容易地对私有函数进行测试,而不必搞得乱七八糟。
此外,Visual Studio的单元测试允许你做覆盖和其他NUnit单独不能做的事情。
我不喜欢Visual Studio的内置测试框架,因为它迫使你创建一个单独的项目,而不是把你的测试作为你正在测试的项目的一部分。
MSTest is essentially NUnit slightly reworked, with a few new features (such as assembly setup and teardown, not just fixture and test level), and missing some of the best bits (such as the new 2.4 constraint syntax). NUnit is more mature, and there is more support for it from other vendors; and of course since it's always been free (whereas MSTest only made it into the Professional version of Visual Studio 2008, and before that it was in way more expensive SKUs), and most ALT.NET projects use it.
话虽如此,还是有一些公司非常不愿意使用没有微软标签的东西,尤其是OSS代码。因此,拥有官方的微软测试框架可能是这些公司需要进行测试的动机;老实说,重要的是测试,而不是你使用什么工具(使用Tuomas Hietanen的代码,你几乎可以让你的测试框架可互换)。