简体   繁体   中英

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#.

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. 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.

Other alternatives are xUnit.net , NUnit and mbUnit . 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. I use the free personal version of TestDriven.net as my Test Runner within Visual Studio.

There is a "How to get started" Guide for 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.

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. I started using 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.

I believe that TestDriven.Net supports a lot of different frameworks. 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. You may need Team system to get baked in coverage analysis for VS, but there is always NCover (comes with TestDriven.Net).

I'd suggest looking at Pragmatic Unit Testing in C# from the Pragmatic Programmers or Test Driven Development with C# . Both would have examples.

Rob Conery's StoreFront example is a good example of ASP.NET MVC.

Rob Conery has created a serie of videos showing how to create an eCommerce app using MVC. He's using a complete testdriven approach (using the built-in test framework in 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. It is created by the guys behind 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.

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. 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. It has console & gui runners which helps in automatic testing. XUnit is now used by most of the developers.

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.

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. 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.

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.

I'm using Selenium RC and Moq . 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 . It's pretty straight-forward and easy to follow...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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