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
2009-06-04 19:53:34
+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
2009-06-04 19:55:02
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
2009-06-04 19:58:57
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
2009-06-08 16:39:51
Hanselman method is very nice for testing the Page as a whole, but I want to test the individual methods.
Sean McMillan
2009-06-08 17:19:11
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
2009-06-08 20:14:22
Ah... It's currently configured as a web site.
Sean McMillan
2009-06-08 20:38:17
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
2009-06-09 17:23:31