Unit Testing ASP.NET Applications

A common problem for developers prior to Team System was the challenge of unit testing ASP.NET applications. Most of the behavior of ASP.NET applications relies on having a web request context — for example, querystrings, cookies, and server variables. To effectively test this code, a web environment needed to be simulated. While some frameworks, such as NUnitASP existed, the task was still difficult and not integrated with the Visual Studio environment.

Team System enables developers to write unit tests that have access to an active ASP.NET context. The unit testing framework connects to the target ASP.NET application, executes a request, and provides the context of that request to the unit test.

ASP.NET unit test attributes

An ASP.NET unit test looks much like any other unit test, except that it is decorated with additional attributes to indicate details about the target ASP.NET application. The following table summarizes common ASP.NET unit testing attributes.

Open table as spreadsheet




Identifies the type of host that will run the unit test. Typically, this will be ASP.NET.


The URL that will be requested. The context of this request will be made available to the unit test via TestContext.


Specify this attribute when the host for the web application will be the built-in ASP.NET Development Server. The first argument is the physical directory of the web application. The second is the name of the application root.

These attributes exist in the Microsoft.VisualStudio.TestTools.UnitTesting.Web namespace, and, as with all unit tests, a reference to the Microsoft.VisualStudio.TestTools.UnitTestFramework assembly is required. Ensure that you add the associated using statements as well:

     using Microsoft.VisualStudio.TestTools.UnitTesting;     using Microsoft.VisualStudio.TestTools.UnitTesting.Web; 

Now that you have an understanding of the attributes for ASP.NET unit tests, we'll describe your options for creating them.

Creating ASP.NET unit tests

One way to create an ASP.NET unit test is to generate one from a class or method of your web application. You saw in the previous section how simple Team System makes this.

First, open your web application project. Then, open the class containing methods you wish to test. Right-click and choose Create Unit Tests. Select the specific methods for which you wish to generate unit tests and click OK.

The unit tests, already decorated with the appropriate attributes for your web application, are created in a test project in your solution. If you don't already have a test project, one will be created automatically for you.


ASP.NET unit test generation is only available to classes in your web application's App_Code folder. For other items, such as code-behind implementations, we suggest you copy the attributes from a previously generated ASP.NET unit test to another unit test for the code you wish to test.

Whether you generated your ASP.NET unit test or converted a unit test by adding attributes, you are ready to tap into the ASP.NET context in order to verify behavior. The TestContext object features a RequestedPage property. This is actually an instance of a System.Web.UI.Page object appropriate for your web application. Assign this to a local variable and you can use it to access controls of the page.

In order to read and set values of the controls on the page, you will need to obtain a reference to the control using the Page object's FindControl method.

Frequently, you will need to access nonpublic methods of the page. For example, most button submit methods are not public. As you saw earlier in this chapter, Team System provides a simple way to wrap objects in order to access their nonpublic members. To access such methods, use a PrivateObject pointing to the Page instance. Once you have that PrivateObject, use its Invoke method to call the target nonpublic method.

Here is an example ASP.NET unit test, using a local ASP.NET development server, which enters a stock ticker symbol and requests a quote:

     [TestMethod]     [HostType("ASP.NET")]     [AspNetDevelopmentServerHost("%PathToWebRoot%\\StockTicker", "/StockTicker")]     [UrlToTest("http://localhost/StockTicker&")]     public void VerifyCompanyTickerLookup()     {         Page page = TestContext.RequestedPage;         TextBox stockName = (TextBox)page.FindControl("txtStockSymbol");         stockName.Text = "MSFT";         PrivateObject po = new PrivateObject(page);         Button getCompanyName = (Button)page.FindControl("cmdGetCompanyName");         po.Invoke("cmdGetCompanyName_Click", getCompanyName, EventArgs.Empty);         Label result = (Label)page.FindControl("lblResults");         Assert.AreEqual("Microsoft", result.Text);     } 

The attributes indicate how to connect to the local StockTicker ASP.NET web application. First, the Page reference is obtained from the TestContext. Then, the value of the txtStockName control is set to MSFT. Access to the Submit button is not public, so a PrivateObject is used to access the Page. After invoking the button's click method, the txtResult control is read to ensure that the ticker MSFT was correctly returned as "Microsoft."

Professional Visual Studio 2005 Team System
Professional Visual Studio 2005 Team System (Programmer to Programmer)
ISBN: 0764584367
EAN: 2147483647
Year: N/A
Pages: 220

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net