Testing Web Services
You can test Web services by calling Web methods from unit tests. Testing Web services is much like testing other code by using unit tests in that you can use Assert statements, and the tests produce the same range of results. However, the Microsoft.VisualStudio.TestTools.UnitTesting.Web namespace of Team Edition for Testers provides attributes and methods specifically for testing Web services; they are described in Testing a Web Service Locally.
The following list describes two ways to test Web services with unit tests:
The Web service runs on an active Web server. There are no special requirements for testing a Web service that runs on a local or a remote Web server, such as IIS. To do this, add a Web reference and then call the Web methods of the Web service from your unit tests just as they would call the methods of a program that is not a Web service. For information about how to add a Web reference, see Add Web Reference Dialog Box. For information about how to create unit tests, see How to: Generate a Unit Test and How to: Author a Unit Test. For information about how to use a Web test to test a Web service, see How to: Create a Web Service Test.
The Web service is not hosted in an active Web server. As described in Testing a Web Service Locally, you can test a Web service that runs on your local computer and not in a Web server, such as IIS. To do this, you use an attribute provided by the Team System testing tools to start ASP.NET Development Server. This creates a temporary server at localhost that hosts the Web service that you are testing. For more information about ASP.NET Development Server, see Web Servers in Visual Web Developer.
Testing a Web Service Locally
This is the process for testing a Web service that runs on your local computer but not in IIS:
Create the Web service on the local file system. For more information, see Walkthrough: Creating an XML Web Service Using Visual Basic or Visual C#.
Generate unit tests against the Web service in the standard way for generating unit tests. For more information, see How to: Generate a Unit Test.
Add the AspNetDevelopmentServerAttribute attribute to the unit test. The arguments for this attribute class point to the site of the Web service and name the server. For more information, see Ensuring Access to ASP.NET Development Server.
Within the unit test, add a call to the TryUrlRedirection method to point the Web service object to the correct server. Verify that it returns true, and use and Assert statement to fail the test if the redirection fails. For more information, see Using the TryUrlRedirection Method.
Call the Web service or exercise it in any other way that you feel is necessary to test it thoroughly. For an example of this, see Example Web Service Test Method.
Ensuring Access to ASP.NET Development Server
If the site of the Web service is on your local file system, it uses ASP.NET Development Server and it is not an IIS site. In this case, the process of generating unit tests starts an ASP.NET Development Server for the Web service and adds a Web reference to the test project.
The ASP.NET Development Server is temporary , and the Web reference would fail after the server is stopped. Team System testing tools solve this problem by providing the AspNetDevelopmentServer attribute. This attribute class has two constructors:
AspNetDevelopmentServerAttribute(string name, string pathToWebApp)
AspNetDevelopmentServerAttribute(string name, string pathToWebApp, string webAppRoot)
The following parameters are used with this attribute:
name
is a user-defined name that is associated with the server.pathToWebApp
is the path on disk to the Web site you are testing.webAppRoot
is the virtual path at which the site appears on the server. For example, ifwebAppRoot
is set to /WebSite1, the path to the site is https://localhost:<port>/WebSite1. For the first constructor, the default is https://localhost:<port>/.
Note
The parameters pathToWebApp
and webAppRoot
are used the same way with AspNetDevelopmentServerAttribute
as they are for the AspNetDevelopmentServerHost
attribute, which is used for ASP.NET unit tests.
When you mark a test with the attribute AspNetDevelopmentServerAttribute
, an ASP.NET Development Server is started whenever the test is run. An entry that contains the URL of the site being tested is added to TestContext.Properties of the test class . The key for this entry is AspNetDevelopmentServer.<name>, where
<name> is the value held by the name
argument of the attribute. This mechanism makes sure that the Web service is always available at an ASP.NET Development Server when the test is run and that the URL is known at run time.
To test a Web service this way, you could generate unit tests, or you could write a unit test by hand and mark it with this attribute. Hand authoring requires that you have a Web reference in place so that you can reference the type of the Web service in the code of your unit test. Before you add the Web reference, you must start an ASP.NET Development Server by right-clicking the Web service project and choosing View in Browser.
Using the TryUrlRedirection Method
After you have a Web reference, you can create an instance of the Web service object in your test code, but this might fail at run time because the reference points to the URL of an instance of ASP.NET Development Server that may no longer be running. To solve this problem, use the TryUrlRedirection method to modify the Web service object so that it points to the ASP.NET Development Server that was started specifically for the running unit test.
TryUrlRedirection
is a static method of the WebServiceHelper class that returns a Boolean that indicates whether the redirection succeeded.
bool TryUrlRedirection(System.Web.Protocols.WebClientProtocol client, TestContext context, string identifier)
TryUrlRedirection takes three arguments:
client
is the Web service object to be redirected.context
is the TestContext object for the class.identifier
is the user-defined name for the server to which the Web service object is being redirected.
After calling this method, if it succeeds, you can then call Web methods on the Web service object. In this way, the Web service is accessed through the ASP.NET Development Server that was started when you started the unit test. You can use multiple AspNetDevelopmentServer attributes on a single unit test to start multiple servers, as long as you give them different names.
Unit test generation does not automatically add the AspNetDevelopmentServer attribute or the TryUrlRedirection method call. You must add these entities yourself. Both the attribute and the method are in Microsoft.VisualStudio.TestTools.UnitTesting.Web. Therefore, you will probably need a using
or Imports
statement, as shown in the following example.
Example Web Service Test Method
This is a simple test method that tests the HelloWorld()
Web method of a Web service:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using TestProject1.localhost;
[TestMethod]
[AspNetDevelopmentServer("HelloWorldServer", @"C:\Documents and Settings\user\My Documents\Visual Studio 2005\WebSites\WebSite1")]
public void HelloWorldTest()
{
HelloWorldService target = new HelloWorldService();
Assert.IsTrue( WebServiceHelper.TryUrlRedirection
(
target,
testContextInstance,
"HelloWorldServer"
),
"Web service redirection failed."
);
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: Generate a Unit Test
How to: Author a Unit Test
How to: Parameterize a Web Server
Reference
Microsoft.VisualStudio.TestTools.UnitTesting.Web
AspNetDevelopmentServerAttribute
TryUrlRedirection