Поделиться через


Overview of ASP.NET Unit Tests

You use ASP.NET unit tests to test methods that are part of ASP.NET sites or projects. For example, you can test the methods that represent the business logic of an ASP.NET project.

Note

If you are running Windows Vista, you must run Visual Studio as an administrator to run ASP.NET unit tests. If you run Visual Studio as a normal user, you can create a Web site and create and run ASP.NET tests, but the tests will fail. To run Visual Studio as an administrator, right-click Microsoft Visual Studio 2005 and then click Run As Administrator. If you are prompted to Continue, or to Allow the process to run, click Continue or Allow, respectively. If you are prompted to supply logon credentials for a user with administrator permissions, you must supply those credentials to successfully run ASP.NET unit tests. For more information about security in Windows Vista, see User Account Control Overview

The difference between an ASP.NET unit test and other unit tests is the process in which the tests are run. ASP.NET tests run in the same environment in which the production code will run. For this, you can choose IIS or you can choose the ASP.NET Development Server. When your tests run in the Web server process, you have access to all the environment associated with that process, such as the Page object.

You can create an ASP.NET unit test in two ways: by generating it and by configuring an existing unit test to run as an ASP.NET unit test. Both these methods are described in How to: Create an ASP.NET Unit Test.

You can also specify settings in a run configuration that will be used with unit tests that you run. The settings are used only when that run configuration is active. For more information, see How to: Create an ASP.NET Unit Test.

Using a Different Process Identity while Running an ASP.NET Unit Test

When you are testing ASP.NET code, the test code runs in either the IIS process or in an ASP.NET Development Server process. If your ASP.NET unit test is running in the IIS process, you can choose to have the ASP.NET process run as a non-default user, that is, with a different process identity, for security purposes.

For example, if the ASP.NET Web site must access resources on the computer such as folders, files, or a database, you can choose to run it so that its permissions on these resources are exactly enough, no more.

Note

The default identity depends on the version of IIS. In Windows Server 2003, for example, the default identity of the ASP.NET process, w3wp.exe, is "NETWORK SERVICE."

Unit test execution can fail in this situation. For example, the ASP.NET process that is running as a non-default user might have restricted permissions, but still might try to generate temporary files to the %WINDIR%\Temp folder. This would fail if the default permissions on %WINDIR%\Temp do not allow the process sufficient access to generate those files. In this case, for unit test execution to succeed, you would have to grant the process higher permissions on the %WINDIR%\Temp folder.

Unit test execution can also fail if the service account does not have access to the deployment root folder. If this occurs, Visual Studio displays a dialog box that explains the error and lets you get past it by clicking OK.

See Also

Tasks

How to: Create an ASP.NET Unit Test
How to: Specify a Test Run Configuration
How to: Debug while Running a Test in an ASP.NET Solution

Concepts

Test Deployment Overview
ASP.NET Unit Tests and Private Accessors

Other Resources

Creating Unit Tests