09 July 2009 - 3.0 |
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:
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 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.
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)
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:
You're all set! Open the TestCase editor and run the TestCase and open the Request Editor to see the results:
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: