How to: Create an ASP.NET Unit Test
You use ASP.NET unit tests to test methods that are part of ASP.NET projects. You can create an ASP.NET unit test in either of two ways:
By generating the ASP.NET unit test from an ASP.NET project. This is the most common scenario.
By configuring an existing unit test as an ASP.NET unit test.
You can also specify settings in a run configuration that correspond to the attributes used by ASP.NET unit tests. These procedures are described in the following sections.
When you run ASP.NET unit tests, do not use the ClassCleanupAttribute or ClassInitializeAttribute attributes on any method in a class that contains an ASP.NET unit test. Similarly, do not use the AssemblyCleanupAttribute or AssemblyInitializeAttribute attributes in the same assembly as an ASP.NET unit test. The result of using these attributes in these situations is undefined. However, you can use the TestInitializeAttribute and TestCleanupAttribute attributes for any unit tests.
Setup scripts and cleanup scripts run before and after test runs, regardless of the types of tests that are contained in those test runs. For more information about scripts that are run in conjunction with test runs, see Test Deployment Overview and How to: Specify a Test Run Configuration.
Generating an ASP.NET Unit Test
To generate an ASP.NET Unit Test, you first create an ASP.NET Web site within your Visual Studio solution. You then add a class to the Web site project, and finally, generate a unit test from that class.
To generate an ASP.NET Unit Test
To generate an ASP.NET unit test, start by creating an ASP.NET Web site. To do this, right-click your solution, point to Add, and then click New Web Site.
On the Add New Web Site dialog box, click ASP.NET Web Site.
Under Location, click File System to indicate ASP.NET Development Server.
You now have a new Web site.
Add a class to this project. To do this, in Solution Explorer, right-click the Web site and then click Add New Item.
In the Add New Item dialog box, click Class, and then click Add.
A Microsoft Visual Studio dialog box is displayed to ask about placing the new class in the App_Code folder. Click Yes.
You cannot generate tests from code in an .aspx file or in a folder other than the App_Code folder.
Generate an ASP.NET unit test. If the new class file is not already open, double-click it in Solution Explorer to open it.
Right-click the class in the class file, and then click Create Unit Tests.
The Create Unit Tests dialog box appears. For information about how to use this dialog box to generate unit tests, see How to: Generate a Unit Test.
Verify that the classes, methods, or namespaces for which you want to generate tests are selected.
(Optional) Accept the default Output project or select a new project.
When you are finished, click OK.
The new ASP.NET unit test is added to a file in your test project.
To see the unit test, open the test file and scroll to the end. The attributes that are necessary to run a unit test as an ASP.NET unit test have been automatically specified. For more information about these attributes, see the following procedure, Configuring an ASP.NET Unit Test.
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:
Because all unit tests require the
[TestMethod] attribute, this attribute will already be in place.
This is the URL that is tested when this unit test is run; for example, [UrlToTest(“http://localhost/WebSites/Default.aspx”)]
[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.
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:
[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:
Configuring ASP.NET Unit Tests Using Run Configuration
You can specify settings in a run configuration that correspond to the attributes used by ASP.NET unit tests. After you have specified these attributes in a run configuration, the settings will apply when you run any ASP.NET unit test, whenever that run configuration is active.
Only one set of settings for ASP.NET unit tests can be in effect: run configuration settings or attribute settings, but never a mixture of the two. Run configuration settings take precedence over attributes, if present. This means that even if you specify only one ASP.NET setting in run configuration, any ASP.NET settings specified as attributes are ignored.
To configure ASP.NET unit tests using run configuration
Open a run configuration file. For more information, see How to: Specify a Test Run Configuration.
On the Hosts page, set the Host Type to ASP.NET.
This displays additional choices, some of which correspond to attributes you can specify in code, such as Url to Test. These attributes are described in the previous procedure, "Configuring an ASP.NET Unit Test."
When you have finished setting the values on the Hosts page, click Save and then click OK.