views:

954

answers:

8

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

+2  A: 

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.

tvanfosson
+4  A: 

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.

Michael Stum
I would start with anything except the visual studio unit test framework, because of the lack of integration out of the box with CI servers and other third party tools
Paco
If you want to get started, then CI may not be the best thing to do. I now exclusively use xUnit, but the learning curve was quite steep.
Michael Stum
I second Paco's point - I havent used MSTEST but have seen the results. Also, I was influenced in a bad direction by Roy Osherove's post on xUnit.net away from it. You won't regret using xUnit.net over NUnit and MSTEST - while the initial friction is less, the quality of your tests and thinking will pay in spades. I'll remove my -1 When this post has an appendage at the end of the first para indicating that this is not universally held view
Ruben Bartelink
+3  A: 

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)

l3dx
+2  A: 

The only thing I would add is to a) give them all a try and see what you like [that is a crap answer but everyone has their own likes and dislikes so that 'try it' card gets played alot] and what friction points you encounter and b) check out a nice comparison of the primary unit test frameworks on the XUnit CodePlex site.

MotoWilliams
A: 

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.

JB King
+2  A: 

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.

Falkayn
A: 

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

Peter Bernier
+1  A: 

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.

Shahnawaz Khan