Udostępnij za pośrednictwem


Save Time by Unit testing your webservices

Ok folks here we go, the first post on my new blog. My current job at Microsoft is a Developer Tools Support Engineer supporting the new tools that ship in Visual Studio 2005 Team Edition. Ok enough about me.

 

There has been a big push for SOA (Service Orientated Architecture) and the new tools in VS 2005 Team Edition reflect that. Ok so now that you have created your web service how do you quickly test it on both your development box and then in production. One easy way to do this is to create a unit test for your web service. With the team edition for developers you can just right click in the class code for you web service and select “Create Unit Tests” from the popup menu.  You can then select the different methods that you want to test.

 

Ok so you have done that but you need to test with different values being passed for calls to each method. You could go into each unit test and change the code values you want, run the test and then do it all over again. However that gets old very fast. Not to worry you can place all of you data into a database table and then bind your unit tests to a table. When the test runs it will loop through all of the data in the table and call the webservice method once for each row in the table. Binding your unit test methods to a table can be done with a few clicks of the mouse of by adding the connection string as an attribute to your test method.

 

So now everything is working and you deploy your code out to production or pre-production. How do you quickly test to see if everything works in your production environment? While the answer is that you can just change the URL in the App.config file for your test project or the “URLToTest” attribute of your test methods. Which one of these your unit test uses will depend on if you used the new asp.net development server or IIS when you created your webservice. For example the section in your app.config file that you need to change the URL will look like the following:

 

<TestProject1.Properties.Settings>

            <setting name="TestProject1_localhost_MathServiceDemo" serializeAs="String">

                <value>https://localhost:21940/MathService/MathServiceDemo.asmx</value>

            </setting>

        </TestProject1.Properties.Settings>

 

So now everything is working but you would like to stress your webservice. Don’t worry if you have Team Edition for testers you can create a load test using your unit tests. This will allow you to quickly load test you web server using the unit tests you have already created.

 

Now you say, Brian I am sold how do I find out more. Well if you don’t have VS 2005 team editions you can get a trial version from the following site:

https://msdn.microsoft.com/vstudio/teamsystem/products/suite/

 

For more information on Unit testing and databinding a unit test see the following links:

https://msdn2.microsoft.com/en-us/library/ms182515.aspx

 

https://msdn2.microsoft.com/en-us/library/ms404708.aspx

 

So go enjoy and have fun.

 

Brian