Example 2: Test case for counter with global/external variable

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

PROGRAM Counter
  VAR
    val1 : INT := 5;
    val2 : INT := 10;
    result : INT;
    myFB1 : myFB;
  END_VAR
  VAR_GLOBAL
    count : INT;        
  END_VAR   
  myFB1(in1 := val1, in2 := val2, out => result);
END_PROGRAM
 
FUNCTION_BLOCK myFB
  VAR_INPUT
    in1 : int;
    in2 : int;
  END_VAR
  VAR_EXTERNAL
    count : INT;
  END_VAR
  VAR_OUTPUT
    OUT : INT;
  END_VAR
 
  if (count >= 0) THEN
    OUT := in1 + in2;
  ELSE
    OUT := in1 * in2;
  END_IF;
END_FUNCTION_BLOCK

In case of count ≥ 0, the result of myFB is the sum of both input values. In case of count < 0, the result is the product of the both input values.
In contrast to example 1, here the function block myFB contains an →external variable that is to be tested. The corresponding →global variable is located in the program Counter that is not tested.

How to test the above function block myFB:

  1. Create the test suite for myFB by using the command Create Test Suite. Details: See "Creating a test suite".
    Result: The test suite file myFB.test and the Excel file myFB.xlsm are created in the same folder as the ST-object.

  2. Open the Excel file, enter the test data in the existing worksheet test myFB and save the modified Excel file.
    The worksheet already contains the columns for the variables delivering the input data. Here these columns are in1in2 and count. OUT is the only column for the outputs (= expected results).
    You decide how many test sequences and which test data you define for the test. This example uses the following test sequences and test data:

    (info) The Excel file also contains other worksheets that are not used in this example. See "Structure of the Excel test suite, modifying the worksheets" for information on these worksheets.

  3. Transfer all test data from the Excel file to the test suite file myFB.test by importing the Excel file. Details: See "Create the test suite based on the Excel file for the test execution".

  4. Have the tests executed, e.g. run a SiL-test with coverage. Details: See "Executing the test".
    Result of the test execution: The console displays the progress and the result of the test execution. 

    (info) If error messages are displayed, a different version of Neuron Power Engineer might be started and/or the application might have already been loaded onto the PLC. See "Executing the test" for the necessary steps.

  5. Check the test execution. Details: See "Checking the test execution".
    In case of a SiL-test with test coverage, you are able to have the test coverage displayed within the editor. The following test coverage is displayed for the example:

    (info) Actually, 2 test sequences in the test would have been sufficient for a full test coverage of the IF statement. See "Test coverage displayed within the editor" for details.