简体   繁体   English

哪个单元测试框架以及如何入门(对于asp.net mvc)

[英]Which Unit test framework and how to get started (for asp.net mvc)

I'v never done unit testing before, but now I am willing to give it a try. 我以前从未做过单元测试,但现在我愿意尝试一下。

What framework is best for starters? 什么框架最适合初学者? Pros and Cons 优点和缺点

what should i read before i begin any coding? 在开始任何编码之前我应该​​阅读什么? Books/Articles/Code/Blogs 书籍/文章/代码/博客

is there any opensource "sample projects"? 是否有任何开源“示例项目”?

I will be usign it with asp.net mvc/C#. 我将使用asp.net mvc / C#来使用它。

If you have integrated Unit Testing in Visual Studio (I think it's part of Professional and better), start with that, because it's integrated. 如果你在Visual Studio中集成了单元测试(我认为它是Professional和更好的一部分),那么从这开始,因为它是集成的。 Downside is that to my knowledge, there is no test runner outside of Visual Studio or the Team Foundation Server which disqualifies it for automated testing, but I am not sure how current that information is. 根据我的知识,在Visual Studio或Team Foundation Server之外没有测试运行器取消了自动化测试的资格,但我不确定该信息的当前状态。

Other alternatives are xUnit.net , NUnit and mbUnit . 其他替代方案是xUnit.netNUnitmbUnit I can't really talk about the pros/cons due to lack of experience, but I use xUnit.net now because a) I know that there is a working ASP.net MVC Template since Version 1.1 and b) Assert.Throws is just sexy. 由于缺乏经验,我无法真正谈论优缺点,但我现在使用xUnit.net因为a)我知道自1.1版以来有一个有效的ASP.net MVC模板和b)Assert.Throws只是性感。 I use the free personal version of TestDriven.net as my Test Runner within Visual Studio. 我使用免费的个人版TestDriven.net作为Visual Studio中的Test Runner。

There is a "How to get started" Guide for xUnit: http://www.codeplex.com/xunit/Wiki/View.aspx?title=HowToUse xUnit有一个“如何入门”指南: http//www.codeplex.com/xunit/Wiki/View.aspx? title = HowToUse

Again, I can't really compare them due to lack of experience with NUnit and mbUnit, but I believe that all three are quite stable and usable. 再次,由于缺乏NUnit和mbUnit的经验,我无法真正比​​较它们,但我相信这三者都非常稳定和可用。

I use the one baked into Visual Studio with the addition of TestDriven.Net for the right-click menu items that allow me to run single tests with/without the debugger. 我使用添加了TestDriven.Net的 Visual Studio附带的右键菜单项,允许我使用/不使用调试器运行单个测试。 I started using NUnit. 我开始使用NUnit。 Either one is probably ok. 任何一个都可能没事。 I prefer to use the one in VS primarily so I don't have to support both tools, but there are some advantages to the integration especially if you use Team System. 我更喜欢在VS中使用它,所以我不必支持这两种工具,但是集成有一些优点,特别是如果你使用Team System。

I believe that TestDriven.Net supports a lot of different frameworks. 我相信TestDriven.Net支持许多不同的框架。 You'll also want to use a coverage analyzer to help you see what parts of your code have been covered by tests and help you determine other tests that may be needed. 您还需要使用coverage分析器来帮助您查看测试涵盖了代码的哪些部分,并帮助您确定可能需要的其他测试。 You may need Team system to get baked in coverage analysis for VS, but there is always NCover (comes with TestDriven.Net). 您可能需要在VS的覆盖率分析中获得团队系统,但总有NCover(TestDriven.Net附带)。

I'd suggest looking at Pragmatic Unit Testing in C# from the Pragmatic Programmers or Test Driven Development with C# . 我建议使用C#实用程序员测试驱动开发中查看C#中的 用单元测试 Both would have examples. 两者都有例子。

Rob Conery's StoreFront example is a good example of ASP.NET MVC. Rob Conery的StoreFront示例是ASP.NET MVC的一个很好的例子。

Rob Conery has created a serie of videos showing how to create an eCommerce app using MVC. Rob Conery创建了一系列视频,展示了如何使用MVC创建电子商务应用程序。 He's using a complete testdriven approach (using the built-in test framework in visual studio 2008) 他正在使用完整的测试驱动方法(使用visual studio 2008中的内置测试框架)

I recommend you look into the Gallio Automation Platform for .NET as it provides a neutral (and FREE) tool that can implement multiple unit testing frameworks for you under the one GUI tool. 我建议您查看Gallio Automation Platform for .NET,因为它提供了一个中立(和免费)工具,可以在一个GUI工具下为您实现多个单元测试框架。 It is created by the guys behind mbUnit. 它是由mbUnit背后的人创建的。

My main pro for any unit testing framework is that it gives you a great sense of confidence in your code, and practising TDD becomes a very natural thing to do. 我对任何单元测试框架的主要专业是它让你对代码充满信心,而实践TDD变得非常自然。

The main con is that ASP.NET webforms (which is what I tend to code in) is not easily unit tested, which is why ASP.NET MVC is so attractive to people. 主要的一点是ASP.NET webforms(我倾向于编写代码)不容易进行单元测试,这就是ASP.NET MVC对人们如此有吸引力的原因。 Another con is that you need to go into it realising that you will need to refactor tests as much as the code they exercise, if not more. 另一个问题是你需要进入它,意识到你需要像他们运用的代码一样重构测试,如果不是更多的话。

我唯一要补充的是a)给他们一个尝试,看看你喜欢什么[这是一个废话的答案,但每个人都有他们自己的喜欢和不喜欢'尝试'卡得到很多]和什么摩擦指向你遇到和b)检查XUnit CodePlex站点上主要单元测试框架的一个很好的比较

I'm not trying to confuse you, if you are a developer and want to test ASP.NET MVC projects then use XUnit, it's good and developed by Microsoft's employee. 我不是要混淆你,如果你是一名开发人员并且想要测试ASP.NET MVC项目然后使用XUnit,它很好并且由微软的员工开发。 It has console & gui runners which helps in automatic testing. 它有控制台和gui跑步者,有助于自动测试。 XUnit is now used by most of the developers. XUnit现在被大多数开发人员使用。

Let's start with some basics about this first, ok? 让我们先从一些基础知识开始,好吗? Unit testing refers to testing on the smallest piece of code possible, generally a function/procedure/method within the code. 单元测试是指对可能的最小代码进行测试,通常是代码中的函数/过程/方法。 I'd suggest taking a look at "Refactoring" by Martin Fowler as a good book about how unit tests play a role in refactoring. 我建议看看Martin Fowler的“Refactoring”作为一本关于单元测试如何在重构中发挥作用的好书。

Depending on how you set up your code, you may want to look into mocking frameworks like RhinoMocks, NMocks, .Net MockObjects for example so that you can set up test fixtures to handle the pre-execution conditions that the call may require. 根据您设置代码的方式,您可能需要查看模拟框架,例如RhinoMocks,NMocks,.Net MockObjects,以便您可以设置测试装置来处理调用可能需要的预执行条件。 There are likely more than a few mock object unit testing examples out there and tutorials on asp.net-mvc should have a testing example or two. 可能有不止一些模拟对象单元测试示例,而asp.net-mvc上的教程应该有一两个测试示例。

Don't forget that asp.net-mvc is still in beta and likely to change some more as various kinks get worked out on it. 不要忘记asp.net-mvc仍然处于测试阶段,并且随着各种扭结得到解决,可能会更改一些。

I'm using Selenium RC and Moq . 我正在使用Selenium RCMoq So far I haven't run into too many areas that I haven't been able to test and get good coverage for. 到目前为止,我还没有碰到太多我无法测试并获得良好报道的领域。

Take a look at the Selenium RC sample code . 看一下Selenium RC示例代码 It's pretty straight-forward and easy to follow... 它非常简单易懂......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM