C# 在 .NET 2.0 中实现 BDD/TDD 的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2214/
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-07-31 13:01:52  来源:igfitidea点击:

What's the best way to implement BDD/TDD in .NET 2.0?

提问by qrush

I'm looking to add a testing suite to my application, however I can't move to the newer testing frameworks for .NET 3.5.

我希望向我的应用程序添加一个测试套件,但是我无法迁移到 .NET 3.5 的较新测试框架。

Does anyone have a suggestion about good testing frameworks to use?

有没有人建议使用好的测试框架?

采纳答案by Dan

We use MbUnitand Rihno Mocksand they prove to work very well together. When doing TDD you will almost certainly need to do some form of dependency injection, while this can be done manually, its worth looking at an IoC container such as Castle Windsor.

我们使用MbUnitRihno Mocks事实证明它们可以很好地协同工作。在进行 TDD 时,您几乎肯定需要进行某种形式的依赖注入,虽然这可以手动完成,但值得查看 IoC 容器,例如Castle Windsor

It well worth looking at John Paul Bodhood's screen casts to get you started. JPB's Blog

值得一看 John Paul Bodhood 的屏幕截图以帮助您入门。JPB的博客

回答by Nick Berardi

NUnit is always a favorite of mine. However if you are using TFS as your source control I suggest you stick with the Microsoft Stack.

NUnit 一直是我的最爱。但是,如果您使用 TFS 作为源代码控制,我建议您坚持使用 Microsoft Stack。

回答by Rytmis

For a Mock Object library, I've found the BSD-licensed Rhino.Mocksto be rather pleasing.

对于 Mock Object 库,我发现 BSD 许可的Rhino.Mocks相当令人愉悦。

回答by Ronnie

NUnitis available at http://www.nunit.orgI would suggest this even when working on the MS stack - the support for non-MS frameworks is happening in the MVC previews which shows a definite movement in the right direction to allow us all to customise our stacks to fit.

NUnit可在http://www.nunit.org上获得, 即使在使用 MS 堆栈时,我也建议这样做 - MVC 预览中正在支持非 MS 框架,这表明朝着正确方向的明确运动允许我们所有这些都是为了定制我们的堆栈以适应。

回答by Jason Sparks

I've had great success using NUnitas well.

我也使用NUnit取得了巨大的成功。

I've also used NMockwhen the need arose for mock objects. As an added bonus, the factory for creating your mock objects is called the Mockery.

当需要模拟对象时,我也使用了NMock。作为额外的奖励,用于创建模拟对象的工厂称为 Mockery。

To facilitate the running of unit tests, I've used TestDriven.NETto run unit tests as I coded. Also, I've used Cruise Control .NETto watch SVN and check that every new commit builds and passes all unit tests.

为了便于单元测试的运行,我使用TestDriven.NET在我编码时运行单元测试。此外,我使用Cruise Control .NET来观察 SVN 并检查每个新提交是否构建并通过所有单元测试。

回答by fuzzbone

Using nUnit with TFS isn't too difficult. There's even a project on codeplex to implement this: NUnit for Team Buildwhich even "publishes" the results to the warehouse.

将 nUnit 与 TFS 结合使用并不太困难。甚至还有一个关于 codeplex 的项目来实现这一点:NUnit for Team Build甚至将结果“发布”到仓库。

I haven't tried it - but I would advise clients who have a large investment (or who have a strong preference for it over the MSTest tool) in nUnit who are interested in implementing TFS to continue with nUnit as opposed to trying convert all their existing tests.

我还没有尝试过 - 但我会建议在 nUnit 中有大量投资(或对它比 MSTest 工具有强烈偏好)并且有兴趣实施 TFS 的客户继续使用 nUnit,而不是尝试转换他们所有的现有的测试。

回答by fuzzbone

NUnit and Rhino suit well and the auto-mocking containermight be of interest.

NUnit 和 Rhino 非常适合,自动模拟容器可能会引起人们的兴趣。

If you're looking at BDD too then NBehaveis probably a good choice. If however you just mean the style of BDD that relates to unit testing (xSpec) though you can get away with adding a framework (though things like specunitdo add some synctactic sugar), but you might want to look at MSpecis also interesting.

如果您也在考虑 BDD,那么NBehave可能是一个不错的选择。然而,如果你只是指与单元测试 (xSpec) 相关的 BDD 风格,尽管你可以通过添加框架来逃避(尽管像specunit这样的东西确实添加了一些同步糖),但你可能想看看MSpec也很有趣。

回答by smaclell

I am going to have to put a shout out for Moq. It is clean light mocking framework that helps guide you into the pit of success.

我将不得不为Moq大声疾呼。它是一种干净的轻量级模拟框架,可以帮助您进入成功的陷阱。

The testing tools built into TFS are okay, they will get the job done but can often be a little cumbersome to work with. The generated reports, code coverage and a few other portions are particularly bad, they make you go bald at 22 rather than 50.

TFS 内置的测试工具还可以,它们可以完成工作,但使用起来通常有点麻烦。生成的报告、代码覆盖率和其他一些部分特别糟糕,它们让你在 22 岁而不是 50 岁时秃顶。

If you are really loving the testing, consider trying some Continuous Integration. You will feel the pain from regression quickly and potentially help you get to the end goal faster.

如果您真的很喜欢测试,请考虑尝试一些持续集成。你会很快感受到回归带来的痛苦,并可能帮助你更快地达到最终目标。

Regardless of what you do try out a few and see which one is the most natural, if you have time. Good luck and happy coding.

不管你做什么,如果你有时间,都可以尝试一些,看看哪个是最自然的。祝你好运,快乐编码。

回答by Michael Mann

This is probably a summary of what has already been said, but for TDD I personally use Rhino Mocks and MBUnit. Rhino Mocks is a mocking framework that is free and open source. The advantage of Rhino Mocks is we do not need to use magic strings in setting your expectations as you do in NMock.

这可能是对已经说过的内容的总结,但是对于 TDD,我个人使用 Rhino Mocks 和 MBUnit。Rhino Mocks 是一个免费且开源的模拟框架。Rhino Mocks 的优点是我们不需要像在 NMock 中那样使用魔法字符串来设置您的期望。

I like MBUnit because MbUnit has the concept of RowTests which allow you to vary your inputs to your test method. MBUnit is also freely available.

我喜欢 MBUnit,因为 MbUnit 有 RowTests 的概念,它允许您改变测试方法的输入。MBUnit 也是免费提供的。

You also want to make sure that whatever you choose for your unit testing framework is supported by your CI (Continuous Integration Server). Nunit is supported by default in Cruise Control.NET and you have to do a little extra work to get MBUnit to work in ccnet.

您还希望确保您的 CI(持续集成服务器)支持您为单元测试框架选择的任何内容。Cruise Control.NET 中默认支持 Nunit,您必须做一些额外的工作才能让 MBUnit 在 ccnet 中工作。

From an IDE standpoint you must have TestDriven.NET. TestDriven.NET allows you to right click and run tests in the IDE and it supports MBUnit and Nunit and others.

从 IDE 的角度来看,您必须拥有 TestDriven.NET。TestDriven.NET 允许您在 IDE 中右键单击并运行测试,它支持 MBUnit 和 Nunit 等。

NBehave is the BDD library I have used. I have not used any others so I could not compare and contrast them with you, but NBehave is supported by Gallio from the MBUnit team, which means you can run your BDD tests just as you would your unit tests with TestDriven.NET.

NBehave 是我用过的 BDD 库。我没有使用过其他任何产品,因此无法与您进行比较和对比,但 MBUnit 团队的 Gallio 支持 NBehave,这意味着您可以像使用 TestDriven.NET 进行单元测试一样运行 BDD 测试。

I would also highly recommend Resharper. You will find your productivity increase significantly with this refactoring and guidance tool. It will assist you with changing your code as you are developing your tests.

我也强烈推荐 Resharper。您会发现使用此重构和指导工具可以显着提高工作效率。它将帮助您在开发测试时更改代码。

Hope this helps

希望这可以帮助

回答by Michael Mann

I recommend the following:

我推荐以下内容:

TestDriven.NET- Unit Testing add on for VS that is fully integrated with all major unit testing frameworks including NUnit, MbUnit etc...

TestDriven.NET- VS 的单元测试插件,它与所有主要的单元测试框架(包括 NUnit、MbUnit 等)完全集成...

Typemock Isolator- A mocking framework for .Net Unit Testing

Typemock Isolator- .Net 单元测试的模拟框架

NUnit- An open source unit testing framework that is in C#.

NUnit- C# 中的开源单元测试框架。