Example 3: Test case for addition with local variable, different test mode and stimuli
This example illustrates different test variants for the function block testAddition with the following ST-code:
FUNCTION_BLOCK testAddition
VAR_INPUT
IN1 : INT;
IN2 : INT;
END_VAR
VAR_OUTPUT
OUT1 : INT;
END_VAR
VAR
VAR1 : INT;
END_VAR
OUT1 := IN1 + IN2 + Var1;
END_FUNCTION_BLOCK
The procedure of how to test the above function block testAddition is analogous to the example Example 1: Test case for counter and Example 2: Test case for counter with global/external variable. Hence, this procedure is not listed in this example again. Instead this article concentrates on some of the possible differences within the test suite – in particular on the different test mode and specified stimuli for the local variable VAR1.
The worksheet for the test case contains the columns for the variables that deliver the input data as well as the column for the output (= expected results). These columns are:
IN1
IN2
VAR1 -- Actually, this column is only required for variant 1.
OUT
Test data is specified for these variables in all of the following variants. Starting with the 2nd test suite variant, a different test mode for the local variable will be used in the worksheet Defaults. And the 3rd variants even illustrates how to specify a stimuli for the local variable (this is kind of an initial value).
See "Structure of the Excel test suite, modifying the worksheets" about the data provided in the worksheet Defaults.
Here are the testsuite variants for the function block testAddition:
Variant 1 of test suite: Using the predefined test mode "useTestValue"
Variant 2 of test suite: Changing test mode to "useLastValue"
Variant 1 of test suite: Using the predefined test mode "useTestValue"
This variant uses the following test sequences and test data
:
The predefined data under Defaults has not been changed. As the v ariable Var1 is only read, the test mode useTestValue has automatically been entered.
If you execute this test case, the console will display PASS a result of the test execution. Reason: Due to useTestValue, the test values from the test case column
VAR1
are used. This is the test value 3 for the 1st test sequence and the test value 5 for the 2nd tes tsequence.
Hence, these calculations are true:
Addition: 1 + 2 + 3 = 6
Addition: 1 + 3 + 5 = 9
Variant 2 of test suite: Changing test mode to "useLastValue"
This variant uses the same test sequences and test data. But the
The data under Defaults has now been changed: The test mode has been changed to
useLastValue.
If you execute this test case, the console will display FAIL a result of the test execution. Reason: Due to useLastValue, the test values from the test case column VAR1 are not used anymore.
Hence, these calculations are not executed anymore for this test:
Addition: 1 + 2 + 3 = 6
Addition: 1 + 3 + 5 = 9
If you want that the test case is executed successfully, the expected values for OUT1 would have to be adjusted. This is done in the next variant – where stimuli for VAR1 is specified as well.
Variant 3 of test suite: Keeping test mode "useLastValue", changing stimuli, adjusting the expected values
The column VAR1 has been deleted in the test case (because its test values are not relevant anymore due to
useLastValue
). Moreover, stimuli for VAR1 has been changed to 5 under Defaults, the test mode
useLastValue is kept. Furthermore, the expected result for the test sequences has been adjusted (so that the test case is executed successfully again).
If you execute this test case, the console will display PASS as a result of the test execution. Reason: Stimuli (= initial value) 5 is used as value for VAR1 during the addition – for both test sequences.
Hence, these calculations are true:
Addition: 1 + 2 + 5 = 8
Addition: 1 + 3 + 5 = 9