Declaring and assigning test variables

When you declare a variable, you can create a container for it, initialize it to a string or a dataset value, and set its scope. Then, within the test, you can reassign another value to the variable.

About this task

If the data that you want to assign to a variable is only available after a specific test step, instead of initializing the variable, you need to add a variable assignment further down in the test, so that when the assignment occurs, the data that you need to use is available. Otherwise, when you try to initialize the variable (or do the assignment), the value that you want to use will not be available and will not show up as an option to select.

If a variable is initialized at various places such as test, compound test, schedule, or user group, the product uses the following order to initialize the value of the variable when running the test. The variable set in the variable table of the compound test editor takes the highest precedence followed by others:
  1. Compound test setting in the variable table UI
  2. Compound test specified in a var file
  3. User group setting in the variable table UI
  4. User group specified in a var file
  5. Schedule specified setting in the variable table UI
  6. Schedule specified in a var file
  7. Command line
Note: You must select All tests for this user from the Visible in drop-down list to take the precedence of variable initialization.

Procedure

To create, initialize, and assign a value to a test variable:
  1. In the Test Navigator, browse to the test and double-click it.
    The test opens.
  2. To create a container for the test variables that you create in a test:
    1. Open the test, and in theTest Contents area, click Test Variables.
    2. Select Add > Test Variable Container.
      A container named Test Variables is created for the user-defined variables.
    3. Select the container to rename it.
      The Test Element Details area opens for you to type a new name in the Name field.
  3. To declare or define a test variable:
    1. Open the test, and in the Test Contents section, click the user-defined container to contain the variable.
    2. To create a variable, select Add > Variable Declaration. To create an array variable, select Add > Array Variable Declaration.
    3. Type the name of the variable, and click OK.
      The variable is added as the last element in the container and the Test Element Details area opens.
    4. In the Test Element Details area, set the scope and initial value for the variable.

      Visible in: Select This test only to restrict data to the current test only. Even if another test has a variable with the same name, that variable will not change. Select All tests for this user to share the value of this variable when the test runs in a schedule. For the variable to be shared, both tests must have a variable with the same name and must have this option enabled.

      Check Value: Select When first used to check whether or not a variable is initialized only after the test execution reaches the first request that uses a variable. Select At test start to check whether or not a variable is initialized when starting the execution of the test. If the variable is not initialized, then an error message is displayed, depending on the behavior set.

      If not initialized, set to: Select Text to initialize the variable to a specific value whenever the test runs in the schedule. Select Dataset value and, in the Select Data Source window, select the dataset that will initialize the variable.

      Run-time error if variable not initialized: Select the action for the run when it encounters an uninitialized test variable. If you select Issue test log warning or Issue test log error, verify that the Test log page in the schedule sets errors, failures, and warnings to All, which is the default setting. If you select Exit the test, the schedule continues to run although the virtual users that have the uninitialized variable stop. If you select Do nothing, the test continues to run.

  4. To assign or initial a value to a test variable:
    1. Open the test, and in the Test Contents area, select a test element.
    2. Select Insert > Variable Assignment, which inserts the assignment before the selected element.
      The Test Editor window opens and lists the variables available to the test.
    3. Select the variable that you are assigning a value to and, in the Set to box in the Test Element Details area, set the value for the variable.
      You can set the value to a text string, to any data source that exists in the test before the assignment statement, or to Not initialized.
      A Set statement is added to the test, with the value you chose.