Freigeben über


Unit Tests for ASP.NET Web Services

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

There are some differences between unit tests for classes and those for ASP.NET Web services. Most important, your project must contain an .aspx file. This topic explains how to do that and also describes what happens in your test project when you create a unit test for an ASP.NET Web Service.

For more information about how to run performance tests on Web sites, see Creating and Editing Web Performance Tests. For more information about unit tests for classes, see How to: Create and Run a Unit Test.

Prerequisites

Make sure that you have installed the following IIS components:

IIS 6 Metabase and IIS 6 Configuration Compatibility

Make sure that there is an .aspx file in your project

For a unit test to run, there must be an .aspx file in the project that contains your ASP.NET Web service. Without an .aspx file, you can seemingly create unit tests, but when you try to run them, you will receive an error that says that the Web request completed successfully. However, the test did not actually run.

To add an .aspx file

  1. In Solution Explorer, right-click the project that contains your ASP.NET Web service, click Add, and then click New Item.

  2. In the Add New Item dialog box, under Installed Templates, click Web.

  3. In the list of Web templates, click Web Form, and then click Add.

Three new files are added to your project: WebForm1.aspx, WebForm1.aspx.cs, and WebForm1.aspx.designer.cs. You do not have to modify or use these files.

What happens when you generate a unit test for an ASP.NET Web service

The following figure shows a test method that is generated for a simple ASP.NET Web service project. For information about more general changes that occur when you create a unit test, see Anatomy of a Unit Test.

ASP.NET unit test method

ASP.NET unit test method

  1. All unit tests are generated with a [TestMethod ()] attribute.

  2. The [Host Type ()] attribute specifies the host adapter the unit test will run in. You do not have to change this attribute.

  3. The [AspNetDevelopmentServerHost()] attribute specifies the settings to use when an ASP.NET Development Server is the host server for the test. This attribute is only included if your ASP.NET Web service is running on the file system instead of an IIS process. You do not have to change this attribute.

  4. The [UrlToTest()] attribute specifies the URL of the ASP.NET Web service of the unit test.

  5. The rest is the body of the test method. You will probably want to initialize the variables and edit the Asserts in the test method.

Note

For more information about the attributes described here, see

Microsoft.VisualStudio.TestTools.UnitTesting.

If you are running Windows Vista, run Visual Studio as an administrator

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 2010 on the Start menu, and then click Run As Administrator.

If you run IIS as a nondefault user, make sure that you enable all the necessary permissions

When you create an ASP.NET Web service, you choose a location of either File System or HTTP or FTP. If you choose File System, then the ASP.NET Development Server process is used. If you choose HTTP or FTP, the IIS process is used.

If your ASP.NET Web service is running in the IIS process, you can choose to run your unit test as a nondefault user for security. A nondefault user is a different process identity.

For example, if the ASP.NET Web service 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, and no more.

If the Web service requires permission that the nondefault user does not have, the unit test might fail. For example, the ASP.NET process that is running as a nondefault 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.

See Also

Reference

Microsoft.VisualStudio.TestTools.UnitTesting

Concepts

How to: Create and Run a Unit Test

Anatomy of a Unit Test