
Configuring an ASP.NET Unit Test
You can turn an existing unit test into an ASP.NET unit test by configuring it, that is, by assigning values to certain of the test's custom attributes. You set these values in the code file that contains the unit test.
Before setting the custom attributes, you should first add a reference to the namespace that supports the custom attributes; this is the Microsoft.VisualStudio.TestTools.UnitTesting.Web namespace. With this reference in place, IntelliSense can help you set the attribute values.
Note When you generate an ASP.NET unit test, these attributes are set automatically.
To configure an ASP.NET unit test
Open the code file that contains the unit test.
Set the following attributes for the unit test:
[TestMethod]
Because all unit tests require the [TestMethod] attribute, this attribute will already be in place.
[UrlToTest()]
This is the URL that is tested when this unit test is run; for example, [UrlToTest(“http://localhost/WebSites/Default.aspx”)]
[HostType()]
Use [HostType(“ASP.NET”)]. Tests are typically run under the VSTest host process, but ASP.NET unit tests must run under the ASP.NET host process.
Examples
Example 1. If you are running your Web site with the ASP.NET Development Server, the attributes and values you set for an ASP.NET unit test might resemble the following:
[TestMethod()]
[HostType("ASP.NET")]
[UrlToTest("http://localhost:25153/WebSite1")]
[AspNetDevelopmentServerHost("D:\\Documents and Settings\\user name\\My Documents\\Visual Studio 2005\\WebSites\\WebSite1", "/WebSite1")]
Example 2. To test a Web site running under IIS, use only the attributes TestMethod, HostType, and UrlToTest:
[TestMethod()]
[HostType("ASP.NET")]
[UrlToTest("http://localhost:25153/WebSite1")]