08 May 2007 - 1.7.1 |
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/modify the Data-Driven example as follows:
Here we go!
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 seperated 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
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 avaialable Property Transfers and Requests are run as configured:
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" } }
The TestCase should now look as follows: ![]() | and running it with the following testdata.txt
coupland,150 king,150 shakespeare,150 shows the following result in the groovy log: ![]() |
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, ie once for each test value