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


Testing Web Sites and Web Services in Team System

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 examine 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 a Web site or Web service, as described in the following sections:

  • Testing Web Services By Using ASP.NET Development Server

  • Testing Web Sites By Using ASP.NET Development Server

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 by Using ASP.NET Development Server

To test Web services by using ASP.NET Development server on the local file system, mark the unit test method with the AspNetDevelopmentServer attribute. To identify the location of the Web site, you specify the path of 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 by Using 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 how to test Web sites by using 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.

    Note

    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 of the Web site in each test instead of using the %PathToWebRoot% variable. Important: hard-code this path only when testing locally, because doing this prevents you from sharing tests for this Web site more widely. Remember to change the path of 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

  • Setting %PathToWebRoot% in Other Shared Environments

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 operating system, value of PathToWeb is set to:

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

Setting %PathToWebRoot% in Other Shared Environments

In every case 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 locations:

  • The default location for new Web site projects. By default, the system uses the path of 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 that 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;

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

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

    WebServiceHelper.TryUrlRedirection(
                                       target, 
                                       TestContext,
                                       "HelloWorldServer"
                                       );

    string expected = "Hello World";
    string actual;

    actual = target.HelloWorld();

    Assert.AreEqual(
                    expected, 
                    actual,

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

See Also

Tasks

How to: Author a Unit Test
How to: Generate a Unit Test
How to: Parameterize a Web Server

Reference

Microsoft.VisualStudio.TestTools.UnitTesting.Web
AspNetDevelopmentServerAttribute
AspNetDevelopmentServerHostAttribute

Concepts

Testing Web Services
Web Servers in Visual Web Developer
Overview of ASP.NET Unit Tests