NUnitASP
Once you start religiously using unit tests, you will find that user interfaces are really hard to unit test. Yes, of course you can use reflection to call all the control's Event Handlers but this is not adequate because most UIs have some sort of progression to their functionality. For instance, it is common for UIs to disable a button until some other dependent action is performed. There is screen-recording software available to "record" your clicks and play them back in an automated fashion, but if you are using an ASP.NET Webform for your application, there is an easier wayNUnit ASP. As an extension to NUnit, NUnitASP's functionality will already be somewhat familiar. Running the tests from the NUnit UI or within NAnt is exactly the same, and so is the results report. NUnitASP currently supports the following ASP.NET controls:
- Button
- CheckBox
- DataGrid
- DropDownList
- Label
- LinkButton
- Panel
- RadioButton
- TextBox
- UserControl
- ValidationSummary
Listing 6.6 shows how easy it is to link test code to a control on your page.
Listing 6.6. NUnitASP Test
This tests that a LinkButton was clicked twice. By using this model, you can test the progressions of your user interfaces. The functionality of the button on the tested page is only to change its text to the amount of times it was clicked. The Assert ensures that the button truly was clicked twice.NUnitASP is not only a great study in extending NUnit but also a very useful tool in ASP.NET testing and development.
[Test]
public TestASP()
{
LinkButtonTester linktester = new LinkButtonTester("linkButton1",
CurrentWebForm);
Browser.GetPage("http://localhost/examples/link.aspx");
linktester.Click();
AssertEquals("Clicked once.", linktester.Text);
linktester.Click();
AssertEquals("Clicked twice.", linktester.Text);
}