26 September 2007 - 1.7.6 | home user-guide eclipse jbossws intellij netbeans maven 1.X/2.X PDF files forums bugs sourceforge

Home
Getting Started
User Guide

- Overview
  - Tabbed Desktop
  - License Management
  - Preferences
  - XPath Selection
  - Script Library
- Workspaces
- Projects
- Interfaces
  - Interface Viewer
- Operations
- Working with Requests
  - Form Editor
  - Overview View
  - Outline Editor
  - Message Inspectors
  - Attachments/Inline Files
- Functional Testing
  - Getting Started
  - TestSuites
  - TestCases
    - Test Requests
      - Assertions
      - Editor Wizards
    - Property Transfers
    - Conditional Gotos
    - Groovy Scripts
      - Examples
      - GroovyUtils
    - Properties Step
    - Delay Step
    - Mock Response
    - DataSource
    - DataSource Loop
  - Property Expansion
- Load Testing
  - Getting Started
  - Limit & Strategies
  - Execution
  - Assertions
  - Diagrams
  - JMeter comparison
- Mocking
  - Getting Started
  - Mock Services
  - Mock Operations
  - Mock Responses
- Usage Scenarios
  - Data-Driven Testing
  - Template-Driven Testing
  - Interactive Testing
  - Surveillance Testing
- Tool Integrations
  - Code Generation
  - WSDL Generation
  - WS-I Tools
  - Apache Tcp-Mon
- CommandLine Tools
  - TestCaseRunner
  - LoadTestRunner
  - MockServiceRunner
  - ToolRunner
- Keyboard shortcuts

IDE/Tool Plugins
Search soapUI site
Project Documentation

- About soapUI
- Project Info
- Project Reports

## 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/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 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

### 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 avaialable 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: | 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

Next: Interactive Testing