Welcome to weblogs.com.pk Sign in | Join | Help

NUnitASP :: Unit Testing Web Based User Interfaces

Testing is an important defect removal technique. Unit testing is the most affective regression testing technique. NUnit is the mostly used CASE tool people uses around. NUnit helps us unit test class libraries. The missing part had been non availability of similar tools for the user interface layers, especially web application due to its wide use. But not anymore, as NUnitASP is available as an extension to NUnit.


To use NUnitASP, add reference to the NUnitASP assembly, derive your unit test class from WebFormTestCase base class. Access your web application using Browser property that you will get from the base class. Get web-controls test objects by providing the ids of your web controls on the web application page. Set and fire events of these web controls using the test web-controls. Get the values of the web-controls and use WebAssertion helper class that’s shipped with NUnitASP. Use the same NUnit GUI or console interfaces to open the assembly of your unit test class and you will see the list of your ASP.NET aware unit tests.


Here is one simple “Hello World” unit test class built using NUnitASP. It unit test a web form that has three controls, Button1, Label1 and TextBox. The web form simply set the label text equals to the text box text prefixed with word, Hello on the click of the button.



public class UnitTest : NUnit.Extensions.Asp.WebFormTestCase


       public UnitTest() {}


       public void HelloWorldTest()


              string strURL = @"http://localhost/WebApplication1/WebForm1.aspx";



              NUnit.Extensions.Asp.AspTester.ButtonTester Button1 = new ButtonTester("Button1", this.CurrentWebForm);

              NUnit.Extensions.Asp.AspTester.LabelTester Label1 = new LabelTester("Label1", this.CurrentWebForm);

              NUnit.Extensions.Asp.AspTester.TextBoxTester TextBox1 = new TextBoxTester("TextBox1", this.CurrentWebForm);


              string strValue = "World";

              TextBox1.Text = strValue;



              strValue = "Hello " + strValue;

              WebAssertion.AssertEquals(strValue, Label1.Text);



Published Thursday, January 27, 2005 12:07 AM by khurram
Filed under: ,


No Comments

New Comments to this post are disabled