01 March 2010 - 3.5 user guide eclipse intellij netbeans maven download nightly forum bugs blog sf.net eviware


Eviware Logo

Template Driven Testing

Template Driven testing is an extension to Data Driven testing where the same TestCase is run for a variable number of input values read from some external source. The example given here extends the Data Driven Testing example to read a list of authors and their expected minimum books from a text file and then run the TestCase for each author and validate the number of books found.

To achieve this, we need to extend and modify the Data Driven example as follows:

  • Create an initial Groovy step that reads the external values and stores them in the current TestRunContext together with an index telling which author is currently tested
  • Create another Groovy step that assigns a set of values to the Properties Step for each TestCase run
  • Create a Groovy step at the end of the TestCase that checks the number of hits and loops back to the beginning if there are more test values to test

Here we go!

Read external values

The following example script reads all lines of the "testdata.txt" file into a list stored in the TestRunContext. Each line contains an author name and a number of minimum hits separated by a comma, for example "King,230"

def list = []
new File( "testdata.txt" ).eachLine { line -> list.add( line ) }

log.info( "Read " + list.size() + " test values.." )

context.setProperty( "values", list )
context.setProperty( "index", 0 )

After reading the file into the list, it is stored in the TestRunContext together with an index telling which "row" is currently tested

Init properties with test values

This script reads the current row and assigns that rows author to the Properties Steps "Author" property

def values = context.getProperty( "values" )
def index = context.getProperty( "index" );

def str = values[index]
def ix = str.indexOf( "," )
def author = str.substring( 0, ix )

def props = testRunner.testCase.getTestStepByName( "Properties" )
props.setPropertyValue( "Author", author )

log.info( "set author to [" + author + "]" )

After this, the previously available Property Transfers and Requests are run as configured:

  • The first transfer copies the author and subscription id to the search request
  • The request performs the book search
  • The second transfer copies the number of hits to the "ResultCount" property

Validate and move to next

This step checks the number of hits against the provided text value and fails if the number of hits is not sufficient. Otherwise, the script advances the test index to the next row and loops back if that row exists

def values = context.getProperty( "values" )
def index = context.getProperty( "index" );

def str = values[index]
def ix = str.indexOf( "," )

def props = testRunner.testCase.getTestStepByName( "Properties" )
def resultCount = props.getPropertyValue( "ResultCount" )
def count = str.substring( ix+1 )
def author = props.getPropertyValue( "Author" )

if( count > resultCount )
{
   throw new Exception( "not enough hits for author [" + author + 
                        "], expected " + count + ", got " + resultCount )
}
else
{
   log.info "got " + resultCount + " hits for [" + author + "], required " + count
   if( ++index < values.size() )
   {
      context.setProperty( "index", index )
      testRunner.gotoStepByName( "Init Run" )
   }
   else
   {
      log.info "Finished TestCase, tested " + values.size() + " values"
   }
}

Running the TestCase

The TestCase should now look as follows:
Template Driven Web Service Test
and running it with the following testdata.txt
coupland,150
king,150
shakespeare,150

shows the following result in the Groovy log:

Groovy Log in Template Driven test for SOAP

The following image shows a simple LoadTest which ran the TestCase 5 times, as you can see each step in the loop was called 15 times in total, i.e. once for each test value

Load Test based on a Template Driven Test


Next: Interactive Web Service Testing