C# NUnit 与 Visual Studio 2008 的单元测试测试项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/92869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:35:32  来源:igfitidea点击:

NUnit vs Visual Studio 2008's Test Projects for Unit Testing?

提问by Ryan Skarin

I am going to be starting up a new project at work and want to get into unit testing. We will be using VS 2008, C#, and the ASP.NET MVC stuff. I am looking at using either NUnit or the built in test projects that VS2008 has, but I am open to researching other suggestions. Is one system better than the other or perhaps easier to use/understand than the other? I am looking to get this project set up as kind of the "best practice" for our development efforts going forward.

我将在工作中启动一个新项目并想进行单元测试。我们将使用 VS 2008、C# 和 ASP.NET MVC 的东西。我正在考虑使用 NUnit 或 VS2008 的内置测试项目,但我愿意研究其他建议。一个系统是否比另一个更好,或者比另一个更易于使用/理解?我希望将这个项目设置为我们未来开发工作的“最佳实践”。

Thanks for any help and suggestions!!

感谢您的任何帮助和建议!!

回答by Patrick Desjardins

I have been using NUnit for 2 years. All is fine but I have to say that the Unit system in VS is pretty nice because it's inside the Gui and can more easily do test for private function without having to mess around. Also, the Unit Testing of VS let you do covering and other stuff that NUnit alone can't do.

我已经使用 NUnit 2 年了。一切都很好,但我不得不说 VS 中的 Unit 系统非常好,因为它在 Gui 内部并且可以更轻松地测试私有函数而不必乱七八糟。此外,VS 的单元测试让您可以完成 NUnit 单独无法完成的其他工作。

回答by Mendelt

Daoknamed all the pro's of VS2008 test projects, here are the pro's of NUnit.

Daok 列出了所有 VS2008 测试项目的 pro,这里是 NUnit 的 pro。

  • 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 MS build server like CC.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 2008 but 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.
    Accorting to Kjetil Klaussen this is caused by the Visual Studio testrunner, running MSTest tests in TestDriven.Net makes MSTest performance comparable to NUnit.
  • NUnit 有一个模拟框架。
  • NUnit 可以在 IDE 之外运行,如果您想在非 MS 构建服务器(如 CC.Net)上运行测试,这会很有用
  • NUnit 的版本比 Visual Studio 多。您无需为新版本等待数年,也无需安装新版本的 IDE 即可获得新功能。
  • 正在为 NUnit 开发扩展,如行测试等。
  • 由于某些原因,Visual Studio 测试需要很长时间才能启动。这在 2008 年更好,但对我的口味来说仍然太慢。快速运行测试以查看您是否没有破坏某些东西可能需要很长时间。使用 Testdriven.Net 之类的 NUnit 从 IDE 运行测试实际上要快得多。特别是在运行单个测试时。
    根据 Kjetil Klaussen 的说法,这是由 Visual Studio 测试运行程序引起的,在 TestDriven.Net 中运行 MSTest 测试使 MSTest 性能与 NUnit 相当。

回答by Tuomas Hietanen

The unit-testing framework doesn't actually matter much, because you can convert test classes with separate project files and conditional compilation (like this, VS->NUnit):

单元测试框架实际上并不重要,因为您可以使用单独的项目文件和条件编译来转换测试类(例如,VS->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

The TestDriven.Net plugin is nice and not very expensive... With only plain VS2008 you have to find the test from your test class or test list. With TestDriven.Net you can run your test directly from the class that you are testing. After all, unit test should be easy to maintain and near the developer.

TestDriven.Net 插件很好而且不是很贵...只有普通的 VS2008 你必须从你的测试类或测试列表中找到测试。使用 TestDriven.Net,您可以直接从您正在测试的类运行您的测试。毕竟,单元测试应该易于维护并且靠近开发人员。

回答by Ben Fulton

XUnit is another possibility for a greenfield project. It's got perhaps a more intuitive syntax, but is not really compatible with the other frameworks.

XUnit 是绿地项目的另一种可能性。它可能有更直观的语法,但与其他框架并不真正兼容。

http://www.codeplex.com/xunit

http://www.codeplex.com/xunit

回答by Dror Helper

First I want to correct a wrong statement: you can run msTest outside of visual studio using command line. Although several CI tools such as TeamCity have better support for NUnit (probably would change as msTest becomes more popular). In my current project we use both and the only big diffrence we found is that mstest always runs as a 32bit while NUnit runs as either 32bit or 64bit test which only matters if your code uses native code that is 32/64 dependant.

首先,我想纠正一个错误的说法:您可以使用命令行在 Visual Studio 之外运行 msTest。尽管 TeamCity 等几个 CI 工具对 NUnit 有更好的支持(可能会随着 msTest 变得越来越流行而改变)。在我当前的项目中,我们同时使用两者,我们发现的唯一大区别是 mstest 始终以 32 位运行,而 NUnit 以 32 位或 64 位测试运行,这仅在您的代码使用依赖于 32/64 的本机代码时才重要。

回答by gilles27

As far as I know, there a four frameworks available for unit testing with .NET these days

据我所知,现在有四个框架可用于使用 .NET 进行单元测试

  • NUnit
  • MbUnit
  • MSTest
  • xUnit
  • 单位
  • 单位
  • 测试
  • 单位

NUnit has always been out in front but the gap has closed in the last year or so. I still prefer NUnit myself, especially as they added a fluent interface a while back which makes tests very readable.

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

If you're just getting started with unit testing it it probably doesn't make much difference. Once you're up to speed you'll be in a better position to judge which framework is best for your needs.

如果您刚刚开始进行单元测试,它可能没有太大区别。一旦您掌握了速度,您就可以更好地判断哪种框架最适合您的需求。

回答by Tarsier

One slight annoyance of Visual Studio's testing framework is that it will create many test run files that tend to clutter your project directory - though this isn't that big of a deal.

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

Also, if you lack a plugin such as TestDriven.NET, you cannot debug your NUnit (or MbUnit, xUnit, etc.) unit tests within the Visual Studio environment, as you can with the Microsoft VS testing framework, which is built in.

此外,如果缺少 TestDriven.NET 等插件,则无法在 Visual Studio 环境中调试 NUnit(或 MbUnit、xUnit 等)单元测试,就像使用内置的 Microsoft VS 测试框架一样。

回答by Simara

Benefits/changes of VS2008 Built-in Unit Testing Framework

VS2008 内置单元测试框架的好处/变化

  1. The 2008 version now is available in professional editions (before it required expensive versions of VS, this is just for developer unit testing.) that left a lot of developers with the only choice of open/external testing frameworks.
  2. Built in API supported by single company.
  3. Use the same tools to to run and create tests (you may run them using the command line also MSTest)
  4. Simple design (granted no Mock framework, but this is a great starting point for many programmers)
  5. Long term support granted (I still remember what happened to nDoc, I don't want to commit to a testing framework that might not be supported in 5 years, but I still consider nUnit a great framework.)
  6. If using team foundation server as your backend, you can create work items or bugs with the failed test data in a simple fashion.
  1. 2008 版本现在有专业版(在它需要昂贵的 VS 版本之前,这只是用于开发人员单元测试。)这让很多开发人员只能选择开放/外部测试框架。
  2. 由单个公司支持的内置 API。
  3. 使用相同的工具来运行和创建测试(您可以使用命令行也可以使用 MSTest 运行它们)
  4. 简单的设计(不授予 Mock 框架,但这对许多程序员来说是一个很好的起点)
  5. 获得长期支持(我仍然记得 nDoc 发生了什么,我不想承诺一个可能在 5 年内不受支持的测试框架,但我仍然认为 nUnit 是一个很棒的框架。)
  6. 如果使用团队基础服务器作为您的后端,您可以以一种简单的方式使用失败的测试数据创建工作项或错误。

回答by Richard Ev

Slightly off-topic, but if you go with NUnit I can recommend using ReSharper- it adds some buttons to the VS UI that make it a lot easier to run and debug tests from within the IDE.

有点跑题,但如果你使用 NUnit,我可以推荐使用ReSharper- 它向 VS UI 添加了一些按钮,使在 IDE 中运行和调试测试变得更加容易。

This review is slightly out-of-date, but explains this in more detail:

这篇评论有点过时,但更详细地解释了这一点:

http://codebetter.com/blogs/paul.laudeman/archive/2006/08/15/Using-ReSharper-as-an-essential-part-of-your-TDD-toolkit.aspx

http://codebetter.com/blogs/paul.laudeman/archive/2006/08/15/Using-ReSharper-as-an-essential-part-of-your-TDD-toolkit.aspx