In the past several years, the landscape of web development has been changing. There has been a shift away from a relatively small number of large applications toward a relatively large number of small applications. Web services were not fast enough to be interesting. More developers are performing testing, early and more frequently in development instead of quality assurance (QA). The QA job has become more intellectual and less rudimentary because of the asynchronous nature of business process services. Testing was the last stop: beta, beta, beta, with little alpha paternal support for his little beta brats. Fortunately, testing has become a continuing activity in the process of development. Testing web services ensures the functional quality of the product.
Testing Web Services
Web services are application components that communicate using open protocols. They are self-contained and self-describing. They can be discovered using UDDI and can be used by other applications with HTTP and XML as the basis.
One way you can test web services is by calling web methods from unit tests. It is much like testing other code by using unit tests, using Assert statements. The same range of results is produced. There are two ways to test web services with unit tests:
- The web service runs on an active web server. Testing a web service that runs on a local or remote web service, such as IIS, has no special requirements. Simply add a web reference and call the web methods of the web service from your development solution.
- The web service is not hosted in an active web server. You can test a web service that runs on your local computer and not in a web server, such as IIS. Just use an attribute provided by the Team System testing tools to start ASP.NET Development Server, which creates a temporary server that hosts the web service you are testing.
Testing a Web Service Locally
Follow this process to test a Web service that runs on your local computer, but not in IIS:
- On the local file system, create the Web service.
- Using the standard way to generate unit tests, generate the against the Web service.
- Add the AspNetDevelopmentServerAttribute to the unit test.
- Add a call to the TryUrlRedirection method, within the unit test. Verify that it returns true, and use an Assert statement to fail the test if the redirection fails.
- Call the Web service and test thoroughly in any way you deem necessary.
Unit Tests for ASP.NET Web Services
Unit test for ASP.NET need to contain an .aspx file. Make sure you have the IIS 6 Metabase and IIS 6 Configuration Compatibility.
To add an .aspx file
Do not skip this step. Your unit test with not run unless an .aspx file is in the project that contains your ASP.NET Web service. You can create unit tests without this file, but the test will not actually run, even though it will alert you that it did.
- In Solution Explorer, right-click the project that contains your ASP.NET Web service.
- Click Add, and then click New Item.
- In the Add New Item dialog box, under Installed Templates, click Web.
- In the list of Web templates, click Web Form, and then click Add.
Three files have been added to your project: WebForm1.aspx, WebForm1.aspx.cs, and WebForm1.aspx.designer.cs. Do not use or modify these files.
When generating a unit test for an ASP.NET Web service
- All unit tests are generated with a [TestMethod ()] attribute.
- The [Host Type ()] attribute specifies the host adapter that the unit test will run in.
- 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.
- The [UrlToTest()] attribute specifies the URL of the ASP.NET Web service of the unit test.
- The remainder is the body of the test method. You should initialize the variables and edit the Asserts in the test method.
- If you are still using Windows Vista, run as an administrator. To run Visual Studio as an administrator, right-click Microsoft Visual Studio 2010 on the Start menu, and then click Run As Administrator.
How to Create a Web Service Test
You can test Web services by using a Web performance test. You will need Visual Studio Ultimate to perform this process.
To test a Web service
- Create a new Web performance test. As soon as the browser opens, select Stop.
- In the Web Performance Test Editor, right-click the Web performance test and select Add Web Service Request.
- In the Url property of the new request, type the name of the Web service.
- Open a different session of the browser and type the URL of the .asmx page in the Address toolbar. Select the method that you want to test and examine the SOAP message.
- In the Web Performance Test Editor, right-click the request and select Add Header.
- In the Name property, type SOAPAction.
- In the Value property, type the value in SOAPAction.
- In the String Body node, enter text/xml in the Content Type property.
- Return to the browser from step 4, select the XML portion of the SOAP request from the Web service description page and copy it to the clipboard.
- Return to the Web Performance Test Editor and then select the ellipsis in the String Body property. Paste the contents of the clipboard into the property.
- In order for the test to pass, replace any placeholder values in the XML with valid values.
- Right-click the Web service request and select Add URL QueryString Parameter.
- Assign the query string parameter a name and value.
- Run the test. In the top pane of the Web Performance Test Results Viewer, select the Web service request.
- In the bottom pane, select the Web Browser tab. The XML that is returned by the Web service, and the results of any operations, will be displayed.