Groovy Data Source

Applies to ReadyAPI 3.10, last modified on September 23, 2021

The Groovy data source generates property values by executing a groovy script.

Tip: To learn how to set up a data source loop and refer to data source properties in your tests, see this topic.

You can use data sources of the Groovy type in the DataSource test step in functional tests. They are not available in virtual services.

Configuration

Specify the Groovy code that will generate the desired data.

The Groovy data source

Click the image to enlarge it.

Tip: If the font of the editor is not comfortable for you, change it by using Ctrl + mouse wheel.

To get an output value, use the built-in result object. It is an associative array, keys of which match the specified properties. You can use any string to name properties. For example, the following codesnippet assigns a string to the Sample property property:

Groovy

result["Sample property"] = "value"

You can use the following objects in your scripts:

  • context – Provides a scripting interface to the properties specific to the current test run context.

  • results – An associative array, keys of which match the data source properties.

  • testRunner – Provides a scripting interface to the test runner object that is executing the current test case and test step.

  • log – Provides a scripting interface to the ReadyAPI test log.

Generating multiple rows per iteration

ReadyAPI runs the data source script as many times as you set in the Rows Per Iteration option. At each iteration, it generates values for several rows. To get a value from a specific row, use the following syntax:

${DataSource-Test-Step#Property-Name::Row-Index}

To learn more, see Property Expansion.

Example

The following script returns a list of files in the home directory.

Note: The script returns values to the File and Size properties. Make sure you create them before you run the script.

Groovy

// Import required class
import groovy.io.FileType

// Get current row
def row = testRunner.testCase.testSteps["Data Source"].currentRow;

// Get the home user directory
def directoryName = System.getProperty("user.home");

// Declare an array that will contain file names
def fileNames = [];

// Declare an array that will contain file names
def fileSizes = [];

// Get a list of files in the directory
new File(directoryName).eachFile(FileType.FILES)
{
    // Add file information to the arrays
    file -> {
        fileNames.add(file.name);
        fileSizes.add(file.size().toString());
    } }

// Check that the current row number does not exceed the array size
if (row < fileNames.size) {
    // Return the name to data source's "File" and "Source" properties
    result["File"] = fileNames[row];
    result["Size"] = fileSizes[row];
}

Here is a sample of the script run:

Sample Groovy run

Click the image to enlarge it.

See Also

Basic Concepts of Data-Driven Testing
DataSource Test Step
DataSource Loop Test Step
Excel Data Source
Data Source Types

Highlight search results