Using conditional statements and loops in TestCases

In Tosca, you can define IF, DO and WHILE statements if you would like to run TestSteps with branches or repeatedly. These statements can be applied to any nested structures.

If the number of repetitions for a TestStep is known beforehand, use the property Repetition of a TestStepFolder as described in chapter "Running TestSteps repeatedly - Repetitions".

Conditional statements and loops can be created via the context menu of TestCases or TestStep folders or via the option Create Object in the dynamic menu TestCases:

  • IF Statement

  • DO Statement

  • WHILE Statement

A Conditions object is created for all three options. In this folder you define the condition that must be fulfilled so that the next step can be taken. The next step depends on the type of the statement.

Test results of conditions do not affect the total result of the test execution.

Statement options

Creating IF-THEN statements

When you create IF statements, Tosca will automatically create a THEN statement along with the Conditions object. You can also create an ELSE statement via the context menu of IF statements if required, or by left-clicking the IF statement and selecting Create Object->ELSE Statement from the dynamic menu TestCases.

If the condition is fulfilled, the TestSteps are executed once in the THEN statement. If the condition is not fulfilled however, the TestSteps are run once in the ELSE statement.

Order of conditions:

  • IF

  • Condition (within the IF statement)

  • THEN (within the IF statement)

  • ELSE (optional)

  1. In the first TestStep the buffer A including the value 1 is created (Module TBox Set Buffer).

  2. The Repetition column defines that the TestCase should be run twice.

  3. The condition set in the Condition object defines that the value of the buffer A must be greater than 0 (Module TBox Evaluation Tool).

  4. Since the buffer value is greater than 0, Tosca runs the Then TestStep in the first run. As a result, the value of the buffer A is changed to 0.

  5. In the second run, the same verification is performed. Since the buffer value includes the value 0 after the first run, the TestStep is then run in the Else statement. The value 1 is written to buffer A.

IF-THEN-ELSE Statement

The test result looks as follows:

Test result

Creating DO statements

When you create DO statements, Tosca will automatically create a Loop object along with the Conditions object.

TestSteps within the Loop object are run repeatedly until the condition is no longer fulfilled. The result of the last repetition is thus negative.

DO statements contain the property MaximumRepetitions in order to avoid infinite loops. The Value column shows the maximum number of attempts for a TestStep to be run. The default value is set to 30.

MaximumRepetitions property

Order of conditions:

  • DO

  • Loop (within the DO statement)

  • Condition (within the DO statement)

  1. In the first TestStep the buffer A including the value 0 is created (Module TBox Set Buffer).

  2. The TestStep within the Loop object uses a dynamic expression which defines that the value of the buffer A should be increased by 1 (see chapter "Perform calculations").

  3. The condition set in the Condition object defines that the value of the buffer A must be less than 10 (Module TBox Evaluation Tool).

    The TestStep Set Buffer for tests is run repeatedly until the buffer A has the value 10.

Do statement

The test result looks as follows:

Test result for the DO statement

Creating WHILE statements

When you create WHILE statements, Tosca will automatically create a Loop object along with the Conditions object.

If the condition is fulfilled, TestSteps within the Loop object are run repeatedly until the condition is no longer fulfilled. The result of the last repetition is thus negative.

WHILE statements contain the property MaximumRepetitions in order to avoid infinite loops. The Value column shows the maximum number of attempts for a TestStep to be run. The default value is set to 30 (see Illustration "MaximumRepetitions property").

Order of conditions:

  • WHILE

  • Condition (within the WHILE statement)

  • Loop (within the WHILE statement)

  1. In the first TestStep the buffer A including the value 0 is created (Module TBox Set Buffer).

  2. The condition set in the Condition object defines that the value of the buffer A must be less than 10 (Module TBox Evaluation Tool).

    Since the buffer A contains the value 0, Tosca runs the TestStep in the Loop object.

  3. The TestCase within the Loop object uses a dynamic expression which defines that the value of the buffer A should be increased by 1 (see chapter "Perform calculations").

    The TestStep is run repeatedly until the buffer A has the value 10.

WHILE statement

The test result looks as follows:

Test result for the WHILE statement