对于 Visual Studio 2008 中的 C#,我应该从哪个单元测试框架开始? (Windows 窗体应用程序)

发布于 2024-08-07 05:55:30 字数 210 浏览 6 评论 0原文

问题 - 如果我使用 Visual Studio 2008 在 C# 中启动 Windows 窗体应用程序,我应该使用哪个单元测试框架?

VS2008 似乎有一个内置版本?或者我应该研究像 NUnit 这样的东西?或者NUnit是在VS2008中使用的?什么是最受欢迎的。

如果有一些东西可以实现: (a) 模拟/存根,以及 (b) 断言异常的能力,

谢谢

QUESTION - If I'm starting a Windows Forms application in C# using Visual Studio 2008, which unit testing framework should I use?

There seems to be one build into VS2008? Or should I look into something like NUnit? Or is NUnit what is used in VS2008 under the bonnet? What's the most popular.

Would be good to have something that allows for:
(a) mocking/stubs, and
(b) ability to assert exceptions

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

九歌凝 2024-08-14 05:55:31

我建议您使用 NUnit 作为测试框架。如果您需要在构建服务器上设置它,它非常轻量级并且易于运输。使用 MSTest 时情况并非如此。至于模拟/隔离框架,Rhino Mocks 拥有最大的用户群,您可能会通过 Rhino Mocks 最快地找到问题的答案。

I would recommend using NUnit for your testing framework. It's very lightweight and easy to ship around if you need to get it set up on a build server. This isn't the case using MSTest. As for mocking/isolation frameworks, Rhino Mocks has the largest user base and you're likely to find answers to your questions quickest with Rhino Mocks.

有深☉意 2024-08-14 05:55:31

如果只是一个简单的应用程序,VS2008 中内置的 MSTest 框架应该满足所有条件。它与 NUnit 不同,尽管很多人(错误地)将其视为同一事物。

我从未用它来嘲笑,所以也许其他人可以详细说明这一点。它会在您的解决方案中生成一个单独的项目,仅用于测试目的。 这是关于如何使用它的很好的解释。

替代文字< br>
(来源:geekzone.co.nz)

编辑:
做了更多的挖掘,我发现 MSTest 和 NUnit 的比较

NUnit 框架的缺点

  • NUnit 安装在单独的 MSI 中。
  • 没有与 Visual Studio 集成。
  • 需要手动编写测试用例。
  • 没有自动生成代码。
  • 需要打开单独的窗口(NUnit 控制台或 GUI)来执行测试用例。
  • 测试用例执行的顺序不可用。
  • 没有内置功能来调试测试用例。
  • 没有内置功能来启用/禁用测试用例。
  • 没有内置功能可以提供测试用例的附加信息,例如堆栈跟踪、跟踪信息等。
  • 没有内置功能可以根据计算机名称、类名称和主机类型等对测试用例进行排序。

这是来自我链接到的比较文章。

我从未使用过 NUnit,只使用过 C# 的 MSTest 和 Java 的 JUnit,所以我在这方面有点偏见。 MSTest 对于我来说对于 WinForms 一直非常有效,其功能包括能够单独运行测试、显示非常详细的报告(带有单独的跟踪日志)以及自动生成所有样板测试代码并执行所有这些操作这些因素使得 VS2008 成为如此出色的 IDE。听起来,NUnit 对于其他人来说效果很好,他们也有喜欢它的理由。

除非你有特殊的理由采用 NUnit/Testdriven.NET 方法,比如你需要某个功能,或者你只是更喜欢这种设置测试并尝试将其集成回 VS 的方式,那么我看不出有任何理由不仅仅是使用开箱即用的 MSTest。

If it just a simple application, the MSTest framework built into VS2008 should tick all the boxes. It is not the same as NUnit, though a lot of people (mistakenly) refer to it as if they were the same thing.

I have never used it for mocking, so maybe someone else can elaborate on that. It generates a separate project in your solution that is just for the purpose of testing. This is a good explanation of how to use it.

alt text
(source: geekzone.co.nz)

Edit:
Doing a bit more digging, I came across this comparison of MSTest and NUnit.

Drawbacks of NUnit Framework:

  • Installation of NUnit comes in a separate MSI.
  • No Integration with Visual Studio.
  • Requires writing test cases manually.
  • No auto generation of code.
  • Requires opening separate window (NUnit Console or GUI) to execute test cases.
  • Ordering of test cases execution not available.
  • No inbuilt feature to debug test cases.
  • No inbuilt feature to enable/disable test cases.
  • No inbuilt feature to give additional information of test cases like stack trace, Trace Information etc.
  • No inbuilt feature to sort test cases based on Computer Name, Class Name and Host Type etc.

That's from the comparison article I linked to.

I have never used NUnit, only MSTest for C# and JUnit for Java, so I am kind of biased in that respect. MSTest has always worked really well for me for WinForms, with features like being able to run the tests individually, show really detailed reports (with individual trace logs) and autogenerate all the boilerplate test code and do all those kind of things that make VS2008 such a brilliant IDE. By the sounds of it, NUnit has worked well for others and they have their reasons why they like it.

Unless you have a particular reason to take the NUnit/Testdriven.NET approach, like you need a certain feature, or you just prefer that way of setting up tests and trying to integrate it back into VS, then I don't see any reason not to just use MSTest which works right out of the box.

好多鱼好多余 2024-08-14 05:55:31

我们从 MSTest 开始。它具有在 NUnit 中(稍微)难以复制的优点。它允许您访问类的私有成员。当您检查状态时,这是非常宝贵的。例如,假设我有一个函数,可以将条目放入未公开的字典中。我的理解是,使用 NUnit 你必须使用反射来访问字典,或者添加一个 getter 来进行测试。在这里,你可以查一下字典。非常简单而且非常干净。它还允许您测试我喜欢的私有函数(我知道有些人不相信这一点)。虽然我不喜欢 MSTest,但该功能使测试变得更加容易。

VS 集成也很好。

We've started with MSTest. It has an advantage that is (mildly) difficult to replicate in NUnit. It allows you to access private members of the class. This is invaluable when you are checking state. For instance, say that I have a function that puts entries into a dictionary that is not exposed. My understanding, is that with NUnit you have to use reflection to get to the dictionary, or add a getter for the sole purpose of testing. Here, you can just check the dictionary. Very easy and very clean. It also allows you to test private functions, which I love (I know some people don't believe in this). While I don't love MSTest, that feature makes testing much easier.

VS integration is also nice.

海风掠过北极光 2024-08-14 05:55:31

使用 MSTest。它内置于 VS2008 中,并在 IDE 中支持创建测试函数。如果您“不再需要”MSTest,那么您应该考虑 NUnit 或更现代的 xUnit。您可以通过使用 ExpectedException 属性进行修饰来预期 MSTest 中出现异常。

[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void Should_throw_ArgumentNullException_when_the_Order_is_null()
{
    OrderProcessor processor = new OrderProcessor();
    processor.ProcessOrder(null);
}

模拟框架独立于您选择的测试框架。模拟框架的热门选择是:MoqRhino 模拟

Use MSTest. It's built into VS2008 and has support in the IDE for creating test functions. If and when you "outgrow" MSTest, then you should look at NUnit or the more modern xUnit. You can expect exceptions in MSTest by decorating with the ExpectedException attribute.

[Test]
[ExpectedException(typeof(ArgumentNullException))]
public void Should_throw_ArgumentNullException_when_the_Order_is_null()
{
    OrderProcessor processor = new OrderProcessor();
    processor.ProcessOrder(null);
}

Mocking frameworks are independent of your choice of testing framework. Popular choices for mocking framework are: Moq and Rhino Mocks.

演出会有结束 2024-08-14 05:55:31

我想说,为了简单起见,从 Visual Studio 2008 中内置的那个开始。NUnit 非常棒,我经常使用它,所以一旦您熟悉了编写单元测试,您就可以继续使用 NUnit。

i would say, for simplicity, start with the one built into visual studio 2008. NUnit is fantastic and i use it alot, so you can probably move on to NUnit once your comfortable with writing unit tests.

凉月流沐 2024-08-14 05:55:31

您没有提到您是否要测试驱动代码。

  • 如果您不熟悉任何 xUnit 框架,请使用 NUnit。最小的学习曲线。它已经存在了更长时间。 Charlie Poole 活跃于 NUnit(开源),并且拥有良好的更新历史。
  • VS2008内部集成了MSTest。它与 NUnit 不一样。我从来没有理由从 NUnit 切换过来。我认为 NUnit 绝对拥有最大的用户群。
  • NUnit 为 Mocks 捆绑了一些东西,但我肯定会向您推荐 MoqRhino Mocks 已经成为领先者一段时间了,但 Moq 有一个更简单的学习曲线(来自在线文档)虽然它可能并不总是处理边缘场景......但恕我
  • 直言,两者都支持断言异常。

我建议买一本薄书 '实用使用 NUnit' 在 C# 中进行单元测试,并至少进行一次。

在 NUnit v MSTest 线上,我不适合发表评论。 MSTest 的飞行时间为 0。

  • 虽然我听说 VS2010 将提供诸如自动代码覆盖率报告之类的小玩意。如果您进行了 MSTest 测试。但这就是我不断听到的支持 MSTests 的论点……更多关于 IDE 支持和集成的内容。有一些很棒的插件,比如 Resharper 和 TestDriven,可以修补这个问题,但需要付出一定的代价。
  • 恕我直言,微软的单元测试工具有一种微妙的方式引导你走上黑暗的道路。所以要小心。请参阅我在此线程上对戴尔的评论。

You didn't mention if you were going to be test-driving code..

  • Go with NUnit if you're not familiar with any of the xUnit frameworks. Smallest learning curve. It has been around for longer. Charlie Poole is active on NUnit (open source) and has a good history of good updates.
  • VS2008 has MSTest integrated inside it. It's not the same as NUnit. I've never had reason to switch from NUnit. I'd argue that NUnit definitely has the biggest user-base.
  • NUnit has something bundled for Mocks but I'd definitely point you to Moq. Rhino Mocks has been the top dog for a while now but Moq has an easier learning curve (from online docs) although it may not always handle edge scenarios... yet IMHO
  • Asserting exceptions is supported in both.

I'd recommend getting a thin book 'Pragmatic Unit Testing in C# with NUnit' and going through it atleast once.

On the NUnit v MSTest line, I'm not fit to comment. 0 flying-time with MSTest.

  • Although I hear that VS2010 will offer trinkets like automated code-coverage reports.. only if you've MSTest tests. But that's the kind of argument I keep hearing in MSTests favor.. more on the lines of IDE Support and integration. There are great add-ins like Resharper and TestDriven that can patch that in but at a price.
  • Also IMHO, Microsoft's unit testing tooling has a subtle way of leading you down the dark path. so be careful. See my comment to Dale on this thread.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文