Setup, TearDown, and Report Scripts

Applies to ReadyAPI 3.57, last modified on December 20, 2024

Use the Setup, TearDown, and Report scripts to enhance test items’ functionality. This topic describes each of these script types.

ReadyAPI uses a number of third-party libraries. It is quite possible that we will update some of these libraries or even remove them from ReadyAPI. If you use classes from these libraries, you will have to update your scripts. See a list of third-party libraries updated in ReadyAPI 3.57.0.

Setup and TearDown scripts

Setup and TearDown scripts are available in the different test items: project, test suites and test cases. Before the test engine performs each test item, it runs its Setup script. When the test item run finishes, the test engine runs its TearDown script.

The following scripting objects are available:

  • virtRunner – the object for controlling virtual services.

  • log – the object for interaction with the log.

  • context – the object contains different data of the current test (e.g., properties).

  • testRunner – the interface of the test runner.

  • Also, scripts access an object that represents a current test item (project, testSuite or testCase).

You can edit the scripts for the project, test suite and test case in the Setup and TearDown tabs (see Script Editors).

Setup Script tab

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.

Below you can find several examples of how to use setup and teardown scripts in your projects.

The following sample Setup script sets test case properties to default values at the start of a test case run. For demonstration purposes, we get the default value from a project property:

Groovy

// Get the default value from a project property
def value = testRunner.testCase.testSuite.project.getPropertyValue("defaultValue")

// Set a test case property to a default value
testRunner.testCase.setPropertyValue("my property", defValue)

// Clear a property value
testRunner.testCase.setPropertyValue("empty property", "");

The following sample Setup script sets test suite and test case properties to default values at the start of a test suite run. For demonstration purposes, we get the default value from a project property:

Groovy

// Get the default value from a project property
def value = runner.testSuite.project.getPropertyValue("defaultValue")

// Set a test suite property to a default value
runner.testSuite.setPropertyValue("my property", value)

// Set a test case property to a default value
runner.testSuite.getTestCaseByName("Test Case 1").setPropertyValue("my property", value)

// Clear a test suite property value
runner.testSuite.setPropertyValue("empty property", "")

// Clear a test case property value
runner.testSuite.getTestCaseByName("Test Case 1").setPropertyValue("empty property", "")

The following sample Setup script sets test suite and test case properties to default values when you run all functional tests from the Functional Tests node. For demonstration purposes, we get the default value from a project property:

Groovy

// Get the default value from a project property
def value = runner.project.getPropertyValue("defaultValue")

// Set a test suite property to a default value
runner.project.getTestSuiteByName("Test Suite 1").setPropertyValue("my property", value)

// Set a test case property to a default value
runner.project.getTestSuiteByName("Test Suite 1").getTestCaseByName("Test Case 1").setPropertyValue("my property", value)

// Clear a test suite property value
runner.testSuite.setPropertyValue("empty property", "")

// Clear a test case property value
runner.testSuite.getTestCaseByName("Test Case 1").setPropertyValue("empty property", "")

The following Setup script runs a virtual service at the start of a test run:

Groovy

//Start the virtual service
virtRunner.run("my virt")

The following TearDown script stops a virtual service at the end of a test run:

Groovy

//Stop the virtual service
virtRunner.stop("my virt")

Below is a sample TearDown script of a test suite. The script posts the list of failed test cases to the ReadyAPI log:

Groovy

for ( testCaseResult in runner.results )
{
    // Getting all test cases’ names from the suite.
    testCaseName = testCaseResult.getTestCase().name
    log.info testCaseName

    // Checking whether the case has failed.
    if ( testCaseResult.getStatus().toString() == 'FAIL' )
    {
        // Logging failed cases and test steps’ resulting messages.
        log.info "$testCaseName has failed"
        for ( testStepResult in testCaseResult.getResults() )
        {
            testStepResult.messages.each() { msg -> log.info msg }
        }
    }
}

Report script

The Report script is available for test suites and test cases. The test engine performs the Report script of a test item, when you generate a report for it.

The following scripting objects are available:

  • log – the object for interaction with the log.

  • report – the object that holds all data used to populate the report. Its members allow you to add or adjust data and subreport data that the report engine uses to fill the report template.

  • params – the object that contains all defined report parameters and their values.

  • Also, scripts can access an object which represents a current test item (testCase, testSuite or project).

You can edit the script for the test suite and test case in the Report tab (see Script Editors).

Report Script tab

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.

See Also

Scripting
Groovy Script Test Step

Highlight search results