NUnitASP

Extend Your Unit Testing to Include ASP.NET Web Forms

Ken McNamee

October 30, 2009

5 Min Read
ITPro Today logo in a gray background | ITPro Today

ToolKit

LANGUAGES: ALL

ASP.NET VERSIONS:ALL

 

NUnitASP

Extend Your Unit Testing to Include ASP.NET Web Forms

 

By Ken McNamee

 

Nearly two years ago I introduced you to NUnit, theopen-source unit testing tool (see Findand Fix Bugs With NUnit). Although NUnit is a great tool for general .NETunit testing, it is not capable of testing ASP.NET pages from the viewpoint ofa client browser. So now let me introduce you to NUnitASP (http://nunitasp.sourceforge.net),which is another open-source tool that extends NUnit and gives it the abilityto directly call and test ASP.NET Web pages. For the sake of clarity, NUnitASPis still only capable of testing .NET code such as ASP.NET server-side code. Itis not capable of testing client-side code such as JavaScript or verifying HTML other than HTML element properties that are also accessible server-side.However important you think that limitation is, I thinkyou ll find NUnitASP to be incredibly useful even if you ve already beenusing NUnit to test your business logic code.

 

NUnit Basics

NUnitASP is merely an extension of NUnit so if you realready familiar with NUnit, you ll find NUnitASP very easy to begin using. Ifyou re new to unit testing, don t worry. NUnit is very easy to quickly becomeproductive with and, by extension, so is NUnitASP. However, just to make surewe ve got the bases covered, here is a quick synopsis. NUnit actually has twocomponents: the framework for defining unit tests in your code and the GUI andconsole tools for running those tests. A unit test is merely a method thatmakes some calls into the business logic code that you want to test. The unittest method can be designed to verify that a test passes by returning thecorrect values or the expected wrong values. A test can also be set up so thata specific exception must be thrown under the circumstances you define. TheNUnit GUI tool is used to load the assembly containing the unit test methods,run them, and display the results.

 

NUnitASP Basics

Rather than being a separate testing framework with itsown tools, the creators of NUnitASP wisely chose to leverage the outstandingNUnit code base. Unit tests are created and run in exactly the same way. Insidethe unit test methods is where NUnit and NUnitASP begin to diverge. With atypical NUnit test method you might instantiate some custom business object,set some properties, and execute a method. With NUnitASP, you might call a Webpage, instantiate some test ASP.NET TextBoxes, set their Text properties, andcall the Click event on an ASP.NET Button object. In effect, you are scriptingout the steps that a user will take if they were sitting at a computer andinteracting with the Web page in a browser.

 

Take, for example, the sample ASP.NET Web page shown inFigure 1. It merely consists of two TextBoxes and a submit Button. The secondTextBox is disabled. When the button is clicked it sets the Text property ofthe second TextBox to the value of the first and also enables the secondTextBox. Pretty simple and easy to test.

 

<%@ Page language="C#" %>

 

 

 private voidbtnGo_Click(object sender, EventArgs e)

 {

   txt2.Text = txt1.Text;

   txt2.Enabled = true;

 }

One


Two


 Runat="server"/>

Figure 1: A sampleWeb form.

 

Figure 2 is the unit test that will ensure that the sampleWeb page functions correctly. If you are not familiar with NUnit, the firstthings to notice are the [TestFixture] and [Test] attributes that have beenadded to the class and method. NUnit uses these attributes to determine whichmembers of an assembly represent testable pieces. Another item to note andthis is where NUnitASP comes in is that the class inherits fromNUnit.Extensions.Asp.WebFormTestCase. Inheriting from this class is significantbecause it provides the unit tester with access to the Browser andCurrentWebForm objects, which allows for interaction with an ASP.NET Web page.

 

using System;

using NUnit.Framework;

using NUnit.Extensions.Asp;

using NUnit.Extensions.Asp.AspTester;

using NUnit.Extensions.Asp.HtmlTester;

namespace ToolKit

{

  [TestFixture]

 public class UnitTest :WebFormTestCase

 {

    [Test]

   public voidTestTextBox2Value()

   {

     Browser.GetPage("http://localhost/aspnetpro/

                     Default.aspx");

     TextBoxTester txt1 =

       new TextBoxTester("txt1",CurrentWebForm);

     TextBoxTester txt2 =

         new TextBoxTester("txt2",CurrentWebForm);

     ButtonTester btnGo =

         newButtonTester("btnGo", CurrentWebForm);

     AssertEquals(txt2.Enabled, false);

     Console.WriteLine(string.Format("txt2.Enabled = {0}",

         txt2.Enabled.ToString()));

     txt1.Text ="Halo";

     btnGo.Click();

     AssertEquals(txt2.Text, "Halo");

     Console.WriteLine(string.Format("txt2.Text = {0}",

         txt2.Text));

     AssertEquals(txt2.Enabled, true);

     Console.WriteLine(string.Format("txt2.Enabled = {0}",

         txt2.Enabled.ToString()));

   }

 }

}

Figure 2: A unit testclass.

 

In the TestTextBox2Value method, the first step is to usethe WebFormTestCase s Browser object to load a Web page that contains WebControls and server-side code. The next step is to create mock Web Controlsthat allow you to interact with the real Web Controls on the server. As you cansee in Figure 3, NUnitASP comes with some of the most common ASP.NET WebControls and HTML Controls. In the example shown in Figure 2, all I need tocreate are two TextBoxTester controls and the ButtonTester control.

 

System.Web.UI.WebControls

System.Web.UI.HtmlControls

Button

CheckBox

DataGrid

DropDownList

ImageButton

Label

LinkButton

ListBox

Panel

RadioButton

TextBox

UserControl

ValidationSummary

HtmlAnchor

HtmlInputCheckBox

Figure 3: NUnitASPWeb Control Testers.

 

The first real test I am making is to assert that theEnabled property of the txt2 TextBox has been set to false. Then the Textproperty of the txt1 TextBox is set to Halo and calling the Click method onthe btnGo object causes the form to be posted. Finally, the unit test verifiesthat the server-side code has been run correctly by checking that the value inthe second TextBox matches the value in the first, and that the second TextBoxhas been enabled.

 

Conclusion

Although this example is very simple, you can see thepotential power in the NUnitASP framework. The functionality of almost any kindof ASP.NET Web page can be tested using this tool. NUnitASP is also very extensible,providing an HtmlTag object that you can use to create your own custom testcontrols. In addition, because NUnitASP is open source, you can view and modifythe framework any way you like to suit your needs.

 

Ken McNamee is aSenior Software Developer with Vertigo Software, Inc., a leading provider ofsoftware development and consulting services on the Microsoft platform. Priorto this, he led a team of developers in re-architecting the Home ShoppingNetwork s e-commerce site, http://www.HSN.com,to 100% ASP.NET with C#. Readers can contact him at [email protected].

 

 

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like