About test variables


Search Tips   |   Advanced Search



A test variable is a user-defined name-value pair that stores and refers to information throughout a test and between tests.

Sharing variables among tests

A variable is declared in the test variables section of the test, but the variable can be used throughout the test as a reference for any field that can be substituted. Substituting data from a test variable is done using the Test Variables page of the Test Data Source view. A variable is given a default value when declared. The value can also be changed using a Set Variable statement. Set Variable statements are created with the Add and Insert menus of the Test Editor. Variables can be set to a hard-coded value or to a value retrieved from a data source, such as datapool or reference that appears before the Set statement.

In order to share variables between tests, both tests must contain a variable with the same name and both variables must have the Visible in option set to All tests for this user. When these conditions are met and multiple tests have been placed in a schedule, then data set in the variable by one test can be used in the other test.

A common reason to share data between tests is to perform data correlation. With data correlation, a variable is set to a response that comes from a request in one test and is used in requests performed in a different test. Assume that you are testing an employee database. The Create Employee test creates an employee record and the Modify Employee test modifies an employee record. When a new record is created, it is assigned a record ID. Variables can be used to pass the record ID from a response in the Create Employee test to the Modify Employee test.

A user-defined variable is not shared among different virtual users. The variable is shared only among the different tests of the same virtual user. Also, if set the Visible in to This test only, then data set by one test is not available to another test, even if both tests contain a variable with the same name.

Using variables to access datapools

You can define variables so that they share data from a datapool throughout tests. This is done by having the value field of a Set Variable statement substituted from a datapool. This way the first test, which appears in the schedule, can set the variable from a datapool and share it with the other test in a schedule.

Assume that you have two tests that log in to an application using a user ID from a datapool. The first test can set the value of a variable from the datapool, and both tests can use the variable, instead of directly using the datapool. In this case they both use the same record from the datapool.

This is similar to the fetch-only-once-per-user behavior of a datapool. However, fetching once means that during playback a virtual user will get only one record from the datapool. The one-record limit holds even if the tests are in a loop and are run several times by the virtual user. By using the user-defined variables, the virtual user retrieves a new record each time through the loop, and both tests can use the same record.

Assignment (set) operators can not only have a variable value substituted from a datapool, but also in the declaration of a variable. In fact, you can substitute the assignment operator and variable value from any data source, and thus that value can be shared between tests as well.

Access variables from custom code

When a shared variable is set with a Set Variable statement, its value is stored in the user data area, IDataArea.VIRTUALUSER. For a custom code example of accessing the user data area, see Storing and retrieving a string.

The convention for keys stored in the data area map is variable_name _ field_name. myGenericVar is accessed as follows:

IDataArea userDataArea = tes.findDataArea(IDataArea.VIRTUALUSER);

// Get the variables value field.
String value = userDataArea.get("myGenericVar_Value").toString();

// Set the variables value field.
userDataArea.put("myGenericVar_Value", "some data");

If the share variable between test flag is not enabled, then the variable is not related to the user data area and must be explicitly passed into custom code as an argument.

To set such a local variable, use a Set Variable statement, which can use the custom code return value as a reference.

Related tasks

  • Declare and assign test variables
  • Add test elements
  • Edit Kerberos tests
  • Search within tests
  • Disable portions of a test or schedule
  • Run test elements in random order
  • Rename test assets
  • Delete test assets
  • Provide tests with variable data (datapools)
  • View binary data
  • Correlate response and request data