Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

Testing Web Sites and Web Services in a Team Environment

Visual Studio 2005

When you first test a Web site, you typically test it on your own computer. But you might also want to share your tests with teammates. Or, in a more formal environment, you might work on a project in which you check your production code and tests into source control, after which a build lab uses your tests to test your code. In each of these scenarios, unit tests must be able to identify the Web site they are testing, whether that site is on the local computer. To enable the test engine to identify the Web site, use the variable %PathToWebRoot%, as described in the following sections of this topic.

Server Choices

If you are developing a Web site or a Web service, you can run it by using either the ASP.NET Development server or a Web server such as IIS. This choice also determines how you test your Web site or Web service, as described in the following sections:

Related Testing: More Information

You can use unit tests to test the Web methods of a Web service, as described in Testing Web Services, and to test the business logic of a Web site, as described in Overview of ASP.NET Unit Tests. Additionally, you can use Web tests to test Web pages, as described in Working with Web Tests.

Testing Web Services with ASP.NET Development Server

To test Web services using ASP.NET Development server on your local file system, mark the unit test method with the AspNetDevelopmentServer attribute. To identify the location of the Web site, you specify the path to its root directory in a parameter of the AspNetDevelopmentServer attribute. To do this, use the %PathToWebRoot% variable, as explained in Setting pathToWebRoot. For more information, see Testing Web Services.

For more information about the use and syntax of the AspNetDevelopmentServer attribute, see AspNetDevelopmentServerAttribute and Testing Web Services.

Testing Web Sites with ASP.NET Development Server

When you generate an ASP.NET unit test to test a Web site on the file system of your computer, the test is marked with the AspNetDevelopmentServerHost attribute. This attribute requires the pathToWebApp parameter. By default, a generated ASP.NET unit test includes the %PathToWebRoot% variable in the pathToWebApp parameter. The value of this variable is set as described in Setting pathToWebRoot. For more information about testing Web sites with ASP.NET Development Server, see Overview of ASP.NET Unit Tests.

For more information about the use and syntax of the AspNetDevelopmentServerHost attribute, see AspNetDevelopmentServerHostAttribute and Overview of ASP.NET Unit Tests.

Setting pathToWebRoot

In every unit test that will be used to test a Web site or a Web service that is running on ASP.NET Development Server, you should specify the string %pathtowebroot%\\<WebSiteName> in the pathToWebApp parameter of the AspNetDevelopmentServer or AspNetDevelopmentServerHost attribute. Specify the parameter as follows:

  • Use the string %PathToWebRoot% verbatim. Use this string even if you are currently running your tests only on your own computer. This gives your tests the flexibility to be shared with others and run in Team Foundation Build.


    When you test multiple Web sites on your own computer and those Web sites do not share a common root directory, you might want to hard-code the path to the Web site in each test instead of using the %PathToWebRoot% variable. Important: hard-code this path only while testing locally, because doing this prevents you from sharing tests for this Web site more broadly. Remember to change the path to include the %PathToWebRoot% variable before having the Web site tested by others on your team, or in Team Foundation Build.

  • <WebSiteName> is the name of the Web site to be tested. Type this string exactly as the Web site's name appears in Solution Explorer.

For an example of the use of the pathToWebApp parameter, see Example Test Method.

Setting the Value of %PathToWebRoot%

The %PathToWebRoot% variable acquires its value in different ways in different environments. The two cases are as follows:

Setting %PathToWebRoot% in Team Foundation Build

When it is used in Team Foundation Build, the value of the %PathToWebRoot% variable is derived from information in the build type and is set by Team Foundation Build automatically.

For example when you run tests on 'Release' version of 'WebSite1' built for x86 platform, value of PathToWeb is set to:

<build directory>/binaries/x86/Release/_precompiled/WebSite1

Setting %PathToWebRoot% in Other Shared Environments

In all cases other than when it is used in Team Foundation Build, the value of the %PathToWebRoot% variable is set as follows. When the test is run, the system will seek the value of the %PathToWebRoot% variable in one or more of the following places:

  • The default location for new Web site projects. By default, the system uses the path to the location where Visual Studio creates new Web site projects. The default value for this is <drive>:\Documents and Settings\<user name>\My Documents\Visual Studio 2005\WebSites\.

  • The PathToWebRoot environment variable. If you have defined this environment variable on the computer where the tests run, its value is used and overrides any value from the default location for new Web site projects.

  • The value of the Web application root directory. To set this value, click Tools and then click Options, expand Test Tools, and then click Test Execution. If you have set this value, it overrides any values of the PathToWebRoot environment variable and the default location for new Web site projects.

Example Test Method

The following test method, which tests a Web service, is marked with the AspNetDevelopmentServer attribute. The AspNetDevelopmentServer attribute requires the pathToWebApp parameter. The use of the %PathToWebRoot% variable in the pathToWebApp parameter is indicated in bold in the following example.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using TestProject1.localhost;

[AspNetDevelopmentServer("HelloWorldServer", "%PathToWebRoot%\\WebSite1")]

public void HelloWorldTest()
    HelloWorldService target = new HelloWorldService();


    string expected = "Hello World";
    string actual;

    actual = target.HelloWorld();


"TestProject1.localhost.HelloWorldService.HelloWorld did not return the expected value."

See Also

Community Additions

© 2015 Microsoft