When it comes to writing unit tests for your ASP.NET pages, there isn’t much help out there. I experimented with a few open source testing tools and found some major limitations.
Both NUnitAsp and WaitN, for instance, are “client-side” tools. In other words, you have to write your tests against the actual HTML output. For example, to get the value of a textbox
, you have to specify the actual HTML id of the textbox
. That’s painful! Especially, since ASP.NET ends up assigning long and complicated IDs to your controls. Plus NUnitAsp is no longer being maintained or supported.
Unlike NUnitAsp and WaitN, Visual Studio ASP.NET Unit testing lets you examine the actual HttpRequest
object. What this means is that you can call methods on your Page and get access to the controls within the page. Visual Studio ASP.NET is a pretty decent tool and may be the answer for you. IF you don’t need to use TypeMock, that is. But if you do, then tough luck because Visual Studio ASP.NET unit tests don’t work with TypeMock. If you give it a try, you’ll get the following exception:
Test method AzAsh.WebApp.Tests.DefaultTest.LoginNotRequiredTest threw exception:
TypeMock.TypeMockException:
*** Typemock Isolator is not currently enabled.
To enable do one of the following:
* To run Typemock Isolator as part of an automated process you can:
- run tests via TMockRunner.exe command line tool
- use 'TypeMockStart' tasks for MSBuild or NAnt
* To work with Typemock Isolator inside Visual Studio.NET:
set Tools->Enable Typemock Isolator from within Visual Studio
For more information consult the documentation (see 'Running' topic).
Check the enable property as they have suggested and you’ll notice that Typemock is enabled! So, what gives? I have no idea. But I do know that Ivonna – an ASP.NET testing tool that is being developed in partnership with TypeMock WILL let you work in conjunction with TypeMock. Like Visual Studio, it allows you to examine the intrinsic objects, such as the Page object. In addition, it’s got another neat feature that lets you inject setup code and assertions into your page’s lifecycle event handlers – very handy especially during type mocking. The only drawback is that it’s a little slow. The unit tests take a while to run.
So if you’ve been scratching your head trying to figure how to develop ASP .NET tests that can work with TypeMock, Ivonna is probably the tool you’ve been waiting for!
Reddit