Monday, October 24, 2011

Data Driven Web Service Testing in soapUI


Data Driven Web Service Testing in soapUI
Data Driven testing is useful if you want to provide input for a test from an external source, for example from a database or properties file. This document will outline a standard approach with a complete example.
A simple approach to Data-Driven Testing in a TestCase is the following:
·         Create the Request steps you want to be data driven
·         Create a Properties Step and define the properties you will read from some external source
·         Create a Property Transfer Step that transfers these properties to their destination steps
·         Now you need to specify the actual reading of the external properties, the easiest is to specify in the "Properties Step" which source properties file to read from. A more flexible approach is to create a proceeding Groovy Step that reads the properties from some source and assigns them to the Properties Step. Both these approaches will be shown below
Below follows a complete example of the above using the Amazon Search Service and specifying the author and subscriptionId externally for a book search. You need to create a project and import the Amazon WSDL (http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl) before digging in..
Create The TestCase and TestRequest
Create a new TestCase and add a new Request Step containing the following request:
<soapenv:Envelope xmlns:ns="http://webservices.amazon.com/AWSECommerceService/2006-02-15"
               xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns:ItemSearch>
         <ns:SubscriptionId>?</ns:SubscriptionId>
         <ns:Request>
            <ns:SearchIndex>Books</ns:SearchIndex>
            <ns:Author>?</ns:Author>
         </ns:Request>
      </ns:ItemSearch>
   </soapenv:Body>
</soapenv:Envelope>
Add a "Schema Compliance" Assertion to validate the response and a "SOAP Fault" Assertion to catch unexpected errors.
Create Properties and Property Transfer
Insert a "Property Step" and add 2 properties named "SubscriptionID" and "Author" and give them some default value. After that insert a "Property Transfer" step and define 2 transfers each transferring the respective property to the target request. You should now have something like the following setup:

If you select the "Run" button in the Property Transfer Editor and open the Request editor for the search request you should see something like the following:

(the default values of the properties have been transferred to the request)
Reading the properties from an external properties file
If you have a standard properties file containing the input you want to use, specify its name in the Property Step Editors source file field. In this example we will create a file in the current directory containing the following:
SubscriptionID=.. your subscription id ..
Author=Douglas Coupland
Save this to a "test-input.properties" file and specify that file in the Property Step Editor:
Setting a property file
You're all set! Open the TestCase editor and run the TestCase and open the Request Editor to see the results:

Setting the properties with Groovy
Instead of reading the properties from a properties file we may need to get them "somewhere else", for example from a database. The Groovy Script step will provide any flexibility you may need in this regard.. here we will just create a Groovy Step that "manually" reads the same properties file as created above.
Begin by inserting a Groovy Script step in the beginning of the TestCase and opening the Groovy Script Editor. Enter the following script:
// init properties
def props = new Properties();
props.load( new FileInputStream( "test-input.properties" ));

// get target step
def step = testRunner.testCase.getTestStepByName( "Properties" );

// assign all properties
def names = props.propertyNames();
while( names.hasMoreElements() )
{
   def name = names.nextElement();
   step.setPropertyValue( name, props.getProperty( name ));
}
The script reads the properties file and assigns all its contained properties to the target "Properties" Step. When running this you should get the exact same result as above:


No comments: