Example 5: Test case for addition with initial value of a local variable

This example illustrates how to create and execute a test for the function block testAddition_InitialValue provided by the following ST-code:

FUNCTION_BLOCK testAddition_InitialValue
VAR_INPUT
IN1 : INT;
IN2 : INT;
END_VAR
VAR_OUTPUT
OUT1 : INT;
END_VAR
VAR
VAR1 : INT := 20;
END_VAR
OUT1 := IN1 + IN2 + Var1;
END_FUNCTION_BLOCK

In contrast to example 3, the function block testAddition_InitialValue contains an →initial value for the local variable VAR1 that is to be considered during the test.
The procedure of how to test this function block is analogous to the previous examples . Hence, this procedure is not listed in this example again. Instead, this article concentrates on the action to be done in the test suite that are required for the initial value.

Likewise to example 3, the created test suite contains these pieces of data:

images/download/attachments/500236786/TF_TestsuiteExampleAddition5a-version-1-modificationdate-1662713875295-api-v2.png

  • The worksheet Defaults contains the following data:

    • VAR1 with value 0 and test mode useTestValue

    • IN1 and IN2 with value 0 and without a test mode (The test mode is not relevant for input variables.)

  • The worksheet for the test case contains these columns:

    • IN1

    • IN2

    • VAR1

    • OUT

For the initialization value 20 from the application to be taken into account during the test, you must adapt the test suite as follows:

images/download/attachments/500236811/TF_TestsuiteExampleAddition5b-version-1-modificationdate-1662714442467-api-v2.png


  1. In the worksheet Defaults, make these changes for Var1:

    1. Delete the predefined value so that the column Default Value does not contain any value for Var1.

    2. Enter this test mode: useImplValue

  2. In the worksheet for the test case, delete the column Var1.

As usual, also enter the test sequences and test data in the worksheet for the test case. Here, the expected result for the test sequences must take into account the initialization value 20 for Var1 (so that the test case is executed successfully).

If you execute this test case, the console will display PASS as a result of the test execution. Reason: Because of useImplValue (and the missing data for Var1), the initialization value 20 for VAR1 from the application is used in the addition – for both test sequences.
Hence, these calculations result are true:

  • images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/check.svg Addition: 1 + 2 + 20 = 23

  • images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/check.svg Addition: 1 + 3 + 20 = 24