NUnitASP

Extend Your Unit Testing to Include ASP.NET Web Forms

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, the open-source unit testing tool (see Find and Fix Bugs With NUnit). Although NUnit is a great tool for general .NET unit testing, it is not capable of testing ASP.NET pages from the viewpoint of a 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 ability to directly call and test ASP.NET Web pages. For the sake of clarity, NUnitASP is still only capable of testing .NET code such as ASP.NET server-side code. It is 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 think you ll find NUnitASP to be incredibly useful even if you ve already been using NUnit to test your business logic code.

 

NUnit Basics

NUnitASP is merely an extension of NUnit so if you re already familiar with NUnit, you ll find NUnitASP very easy to begin using. If you re new to unit testing, don t worry. NUnit is very easy to quickly become productive with and, by extension, so is NUnitASP. However, just to make sure we ve got the bases covered, here is a quick synopsis. NUnit actually has two components: the framework for defining unit tests in your code and the GUI and console tools for running those tests. A unit test is merely a method that makes some calls into the business logic code that you want to test. The unit test method can be designed to verify that a test passes by returning the correct values or the expected wrong values. A test can also be set up so that a specific exception must be thrown under the circumstances you define. The NUnit 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 its own tools, the creators of NUnitASP wisely chose to leverage the outstanding NUnit code base. Unit tests are created and run in exactly the same way. Inside the unit test methods is where NUnit and NUnitASP begin to diverge. With a typical NUnit test method you might instantiate some custom business object, set some properties, and execute a method. With NUnitASP, you might call a Web page, instantiate some test ASP.NET TextBoxes, set their Text properties, and call the Click event on an ASP.NET Button object. In effect, you are scripting out the steps that a user will take if they were sitting at a computer and interacting with the Web page in a browser.

 

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

 

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

<html>

<head>

 <title>Default</title>

 <script language="C#" runat="server">

 private void btnGo_Click(object sender, EventArgs e)

 {

   txt2.Text = txt1.Text;

   txt2.Enabled = true;

 }

</script>

</head>

<body>

<form id="DefaultForm" method="post" runat="server">

One <asp:TextBox ID="txt1" Runat="server"/>

<br>

Two <asp:TextBox ID="txt2" Runat="server" Enabled="False"/>

<br>

<asp:Button ID="btnGo" Text="Go" OnClick="btnGo_Click"

 Runat="server"/>

</form>

</body>

</html>

Figure 1: A sample Web form.

 

Figure 2 is the unit test that will ensure that the sample Web page functions correctly. If you are not familiar with NUnit, the first things to notice are the [TestFixture] and [Test] attributes that have been added to the class and method. NUnit uses these attributes to determine which members of an assembly represent testable pieces. Another item to note and this is where NUnitASP comes in is that the class inherits from NUnit.Extensions.Asp.WebFormTestCase. Inheriting from this class is significant because it provides the unit tester with access to the Browser and CurrentWebForm 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 void TestTextBox2Value()

   {

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

                     Default.aspx");

     TextBoxTester txt1 =

       new TextBoxTester("txt1", CurrentWebForm);

     TextBoxTester txt2 =

         new TextBoxTester("txt2", CurrentWebForm);

     ButtonTester btnGo =

         new ButtonTester("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 test class.

 

In the TestTextBox2Value method, the first step is to use the WebFormTestCase s Browser object to load a Web page that contains Web Controls and server-side code. The next step is to create mock Web Controls that allow you to interact with the real Web Controls on the server. As you can see in Figure 3, NUnitASP comes with some of the most common ASP.NET Web Controls and HTML Controls. In the example shown in Figure 2, all I need to create 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: NUnitASP Web Control Testers.

 

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

 

Conclusion

Although this example is very simple, you can see the potential power in the NUnitASP framework. The functionality of almost any kind of 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 test controls. In addition, because NUnitASP is open source, you can view and modify the framework any way you like to suit your needs.

 

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

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish