views:

509

answers:

2

I'm working on a web site written using asp.net WebForms. I'd like two wrap test cases around some of the more interesting subroutines. How can I instantiate the class that comes from the .aspx file in my test project so I can manipulate it under nUnit?

A: 

You can instantiate it just like any other type:

YourPage page = new YourPage();

Now getting the lifecycle to run will be quite another matter.

Andrew Hare
+1  A: 

UPDATE Make sure you have setup your project to be an ASP.NET web project, not an asp.net web site. You can then mark your page class with the appropriate NUnit attributes and test the output dll for your project with NUnit.

Here is a Microsoft article that explains unit testing in asp.net: http://msdn.microsoft.com/en-us/library/ms404696(VS.80).aspx

John JJ Curtis
Sorry I know this isn't an NUnit solution, but hopefully it will work for you. Here's another article I found about using NUnit with ASP.NET: http://www.hanselman.com/blog/NUnitUnitTestingOfASPNETPagesBaseClassesControlsAndOtherWidgetryUsingCassiniASPNETWebMatrixVisualStudioWebDeveloper.aspx
John JJ Curtis
Unfortunately, that seems to require Visual Studio Team System, and we only have Professional Edition. I'll have to look closer at the Hanselman article.
Sean McMillan
Hanselman method is very nice for testing the Page as a whole, but I want to test the individual methods.
Sean McMillan
Have you setup your project to be an asp.net web project or an asp.net web site? If you build as a web project, you can unit test the .dll that gets created with nunit. You would just need to mark your page class and methods with the correct nunit attributes. You might have to manually call methods such as OnInit and OnLoad in order to simulate the asp.net page lifecycle too.
John JJ Curtis
Ah... It's currently configured as a web site.
Sean McMillan
If you have a web site, there is basically no way to reference the generated classes. If you have a web application project, you can create a "class library" project for your tests, and add a reference to the web application, and go from there.
Sean McMillan