About test variables

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 you 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.

Note: 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.

Array variables

You create an array variable to add multiple values to a variable. If you created a secondary HTTP request, add complete paths of the requests in the array variable that can be used by custom code during playback.

Feedback