Testing webservices using Unit Testing
While working on a little project, i found that i needed to do some web service testing. So, i found the WebMethod i wanted to test, right clicked it and then selected Create Unit Test. When i did this, i got the following as a comment where the WebService was instantiated:
backend target = new backend(); // TODO: Use [AspNetDevelopmentServer] and TryUrlRedirection() to auto launch and bind web service.
backend is the name of the webservice, and this [AspNetDevelopmentServer] confused me. So, after some reading, I found you have to add the following to your code, just below the [TestMethod()] section:
[AspNetDevelopmentServerAttribute("MyServerName", @"<insertPathToWebSite>", "/WebSiteAddress")]
You also need the following using statement:
There is some more info on the MSDN about this under Testing Web Services, and also Testing Web Sites and Web Services in a Team Environment. Handy stuff!
[update] I forgot one fairly important thing:
after you instantiate your webservice object, you need to call the following:
WebServiceHelper.TryUrlRedirection(target, testContextInstance, "WebServiceName");
This will tell Visual Studio to use the the ASP.NET test web server for testing.