I had a customer ask me recently for a way to auto-generate Unit Tests for Web Services based only on the WSDL using Visual Studio. Now, those of you who know me know I would call those “Integration Tests” (or perhaps “Deathly Slow Tests”), but a need is a need, and it’s actually pretty straightforward.
First, you need Visual Studio Team Suite (or at least VS for Developers or Testers) for the following. If you don’t have that, you can always download the 180-day trial.
What we are going to be taking advantage of is two things. First, when you add a reference to a web service (known as a “Web Reference”), a stub file is created that you would normally use to make the calls. Second, when a test project has a reference to another project, it adds an option to use a “Unit Test Wizard” which can autogenerate the test methods.
So, first, let’s fire up Visual Studio. I’m going to be using one of the Team Foundation Server web references here, but any web service should work.
Start by creating a new Project, and mark it as a Class Library. I named mine “TFSWebServiceLibrary” like so:
Now, let’s add our Web Reference. As I mentioned above, I’m going to be using a TFS Web Service, which can be found at http://yourtfsserver:8080/services/v1.0/ServerStatus.asmx. To add this as a web reference, right-click on the project and choose “Add Web Reference”:
Enter the name of your web service and then click “Add Reference”:
You’ll now see a new Web Reference in your project, called whatever you named it in the above screen:
It doesn’t look like much, but if you click the “Show All Files” icon near the top of your Solution Explorer, you’ll see there is much more than meets the eye:
Aha! See Reference.cs? That’s the stub file. If you take a glance at it, you’ll see lots of auto-generated goodness that does the magic of calling the web service. But, more importantly, we now have a code file that accesses the web service that we can generate tests against.
Generating the tests is actually a smidgen easier. Right-Click on your Solution, and add a new Test Project. I called mine TFSWebServiceTester:
Now for the magic. right-click on the project, and add a reference to the project we created above. (“Add Reference” -> Projects Tab -> and then choose the project we created in step 1). Now, right-click on the Project and choose “Add->Unit Test…”:
This will start the Unit Test wizard. Expand the nodes until you find the web service name you created when you added the web service:
If you expand that node, you’ll see the various methods you can call on that web service. May as well uncheck the properties, since we don’t care about testing those:
Now, Click Ok and watch the generation madness begin. When that finishes, you’ll now have a unit test capable of calling the web services. You’ll know the test because it will be shiny, new, and called WebServiceNameTest (for example, ServerStatusTest in our example). Now it’s up to you to modify the tests for the proper input/output variables. For example, I modified the GetSupportedContractVersion test to look like:
If you are going to be doing testing of web services, you definitely want to become familiar with this article which discusses various gotchas and how to test the web services locally instead of having to test on a remote server (which would be closer to a “unit test” – though the spinning up of a local web server would only reduce it to a “somewhat bearable” test).