Share via


Unit Tests for ASP.NET Web Services

There are some differences between unit tests for classes and those for ASP.NET Web services. Most important, your project must contain a default.aspx file. But it might also be interesting to know what happens in your test project when you create such a unit test.

This topic is about ASP.NET Web services. For more information about how to run tests on Web sites, see Working with Web Tests and for more information about unit tests for classes, see How to: Create and Run a Unit Test.

Make sure that there is a default.aspx file in your project

There must be a default.aspx file in the project that contains your ASP.NET Web service for a unit test to run. Without a default.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 without running the test.

Adding a default.aspx file

To add a default.aspx file

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

  2. In the Add New Item dialog box, under Templates, click Web Form and then click Add.

Two new files are added to your project: Default.aspx and Default.aspx.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 2008 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

Concepts

How to: Create and Run a Unit Test

Anatomy of a Unit Test

Reference

Microsoft.VisualStudio.TestTools.UnitTesting