views:

1293

answers:

11

Any suggestions? Using visual studio in C#.

Are there any specific tools to use or methods to approach this?

+23  A: 

Boy, that's a pretty general question. I'll do my best, but be prepared to see me miss by a mile.

Assumptions

  1. You are using ASP.NET, not plain ASP
  2. You don't really want to test your web pages, but the logic behind them. Unit testing the actual .ASPX pages is rather painful, but there are frameworks out there to do it. NUnitAsp is one.

The first thing to do is to organize (or plan) your code so that it can be tested. The two most popular design patterns for this at the time seem to be MVP and MVC. Both separate the logic of the application away from the view so that you can test the logic without the view (web pages) getting in your way.

Either MVP or MVC will be effective. MVC has the advantage of having a Microsoft framework almost ready to go.

Once you've selected a framework pattern that encourages testability, you need to use a unit testing tool. NUnit is a good starting point. Visual Studio Professional has a testing suite built it, but NUnit + TestDrive.NET also works in the IDE.

That's sort of a shotgun blast of information. I hope some if it hits. The Pragmatic Bookshelf has a good book covering the topic.

Brad Tutterow
Visual Studio 2008 comes with MSTest and allows for pretty seamless Unit Testing abilities. Also regarding ASP.NET MVC, when you create a new project, Visual Studio will ask you to build a Test Project for it as well.I've used MVP and some MVC before, makes testing much much easier.
Jack Marchetti
NUnitAsp is no longer supported: the fellow who wrote it makes the valid point that "folks in the know are using presentation layers to make ASP.NET so thin that a tool like NUnitAsp isn't helpful."
Jeremy McGee
+3  A: 

There was a screencast series a year or so ago on Polymorphic Podcast that did a pretty good intro walkthrough of an MVP implementation in ASP.NET. Implemented this way, unit tests fall into place much more naturally.

http://polymorphicpodcast.com/shows/mv-patterns/

Chris Farmer
A: 

Sorry, I should have been a little more specific. I am using ASP.Net 2.0 and was looking more for a tool like jUnit for Java. I took a look at NUnit and NUnitAsp and that looks very promising. And I didn't even know that Visual Studio Pro has a testing suite, so I'll look at all of these options (I've just started using Visual Studio/Asp.net/C# this summer).

Thanks!

Bryan Denny
+2  A: 

NUnit is the way to go for you.

Frog
+1  A: 

Take a look at http://selenium.openqa.org/ it offers a good automated way to build unit tests hooking into the browser. there is a nice firefox plugin for recording tests and can utilize almost any unit testing framework. We had a presentation/demo at our local user group meeting last month and it looked awesome.

jasonlaflair
A: 

Unfortunately they all suck for asp.net, that's why it's hard to find much about them. Not many people are successful in using them

Try Waitr

http://wtr.rubyforge.org/

EvilEddie
+4  A: 

WatiN is the best that I've found. It integrates into Visual Studio unit testing or nunit & you can do pretty much anything you need in the browser (click links, submit forms, look for text/images, etc.), plus it's written in .net so you don't need to have ruby installed (as you do for watir, which is an awesome tool none the less)

Glenn Slaven
I couldn't agree more. I've used Watir for quite a while, but I sometimes have issues with the ruby framework. It's much nicer to use WatiN. I don't have to have ruby installed and I get intellisense.
Ben Mills
WatiN is great, and it's an INTEGRATION testing tool. Unit Test != Integration test.
Jaco Pretorius
+3  A: 

These frameworks are useful for integration testing, but they can't provide unit testing, that is, testing the View isolated from persistence, business logic, whatever.

For unit testing Asp.Net Webforms, as well as MVC, you can use Ivonna. For example, you can mock your database access and verify that the mocked records are displayed in the datagrid. Or you can mock the membership provider and test the logged in scenario without having to navigate to the login page and entering your credentials, as with integration testing.

+1  A: 

WaitN looks good, but if I want this to be part of a build process, I don't want the thing actually running Internet Explorer.

Jack Marchetti
+2  A: 

Your best bet is separating the model logic from presentation and thoroughly unit testing the model with NUnit or similar. Testing the users interaction with the web page can be fiddly.

If you actually do want to unit test the users interaction with the web page some of the afformentioned tools such as waitn seem good, an addition to that which I've heard of is Selenium

RodH257
A: 

I tend to favor the approach of separating any logic out of the UI code. Here's an article that describes a unit test friendly pattern (Model-View-Presenter)

http://www.unit-testing.net/CurrentArticle/How-To-Use-Model-View-Presenter-With-AspNet-WebForms.html