Skip to main content

Testing the RIGHT Function and Detecting Errors

The example displays the test of the RIGHT string function in the Standard library. The test code is configured with a test table.

With test tables, you can test function blocks only. To test a function, implement a wrapper function block. You implement a function block which calls this function and maps the inputs and outputs of the function and passes this 1:1 to the function. If the wrapper function block is the testee, then the function is tested indirectly.

Creating a test project

  1. Use the project wizard to create a new standard project Project_RIGHT for the available controller.

    The project is created with an application, the PLC_PRG program, and a device.

  2. In the device tree, select the Application object.

  3. Click Project → Add Object.

  4. Select the POU object.

    The Add POU dialog opens.

  5. Specify the name FB_RIGHT.

    Select the Function block type and the Structured Text (ST) implementation language.

    Click Add.

    The function block is created.

  6. Specify the following declaration in the FB_RIGHT function block:

    VAR_INPUT
    strCompleteString : STRING(255);
    iSize: INT;
    END_VAR
    VAR_OUTPUT
    strResult : STRING (255);
    END_VAR

    Specify the following implementation in the FB_RIGHT function block:

    strResult := right(strCompleteString,iSize);

    The testee is created. The wrapper function block calls the function to be tested. The inputs and outputs are routed through.

Creating a test table

  1. In the device tree, select the Application object.

  2. Click Project → Add Object.

  3. Select the Test Table object.

    The Add Test Table dialog opens.

  4. Specify the name TestTable_RIGHT.

  5. Select a testee. Then click the _cds_icon_three_dots.png button.

  6. Select the POU FB_RIGHT.

  7. Click the Scan Testee button.

    A list opens with all inputs and outputs of the function block.

  8. Activate all inputs and outputs.

    The enabled variables are inserted automatically as columns in the test table on the Tabular Editor tab.

  9. Click the Add button.

    The test table is inserted below the application. The input and output assignments are missing.

Configuring a test table

  1. Click the Tabular Editor tab.

  2. Select the line <click here to create a test case> and specify the name TC 1: empty string.

  3. Select the row <edit to create a test step> and specify the name Test 1.

  4. Specify the following data in the test table:

    tm_img_testtable.png

    Test 1: The test step detects the last two characters of an empty string. The result is also an empty string. This test yields a positive result.

    Test 2: The test step detects the last five characters of the string 'hello world'. The result is 'world'. This test yields a positive result.

    Test 3: The test step detects the last eight characters of the string 'hello world'. The result is 'lo world'. This test yields a negative result.

Executing an IEC unit test

Tip

The test run, which is created in the test script, always includes preparatory test actions before calling an IEC unit test. The ApplicationTest script, which is installed with the package, provides an example of this. The preparatory test actions are combined there in the Prepare test case.

  1. Create a new test script.

  2. Click Tools → Test Manager.

    The Test Manager view opens.

  3. Select the Test Scripts entry and click Test Manager → Create Test Script.

    A new test script is added.

  4. Double-click the new test script.

    The test script opens in the editor.

  5. Select the test script in the editor and click Test Manager → Add IEC Unit Test.

    An IEC unit test is added to the test script.

  6. Insert the preparatory test actions before the IEC unit test (see example: IecUnitTest).

  7. Check the connection settings and start the Control Win V3 controller.

  8. Click the Save + Run button.

    The test script is executed.

  9. After the test script has been executed, the test report appears.

    tm_img_testreport.png
. The following steps are performed internally during the test run:
  • First the project is searched for test tables. The result is the TestTable_RIGHT test table. The FB_RIGHT function block is entered in this test table as the testee.

  • Then test POUs are instantiated for the testee. One test POU per test case: the TestTable_RIGHTTestee test POU for test case 1 and the TestTable_RIGHTTestee_1 test POU for test case 2.

    The test POUs are used for checking parameters.

  • According to the input and output assignments in the test table, the test POUs TestTable_RIGHTTesttable_RIGHTTC1Emptystring (FB) and TestTable_RIGHTTesttable_RIGHTTC1Validstring (FB) are instantiated for test cases 1 and 2. These function blocks execute the testee. Then the inputs are set and the outputs are checked for the result.

Important

Note the following before you run the test again: Delete the Backup application and remove the automatically inserted rows marked with comments in the PLC_PRG program.

Examine the testee with negative test results

The test report lists the result of the test run for each test step. If a test step is run with errors, then the expected and the actual value of the tested variable are listed in the report. After the test run, you can execute the generated test application and debug the error.

In the test report, a test POU with a failed check is marked in red as Failed. In this row, the name of the created test POU is also in this row (in the example TestTable_RightTestee_1).

Procedure. Debugging a test application
  1. Click Online → Login.

    Your system can communicate with the controller.

  2. In the POU view, open the FB_RIGHTTestee_1 (FB) function block.

  3. In the first line of the function block, set the cursor and press the F9 key.

    A breakpoint is set.

  4. Open the PLC_PRG program in the editor.

  5. In the declaration part, expand the variable instFB_RIGHTTESTTABLE_RIGHTTC2VALIDSTRING.

  6. Click in the Prepared Value field of the xExecute variable (in class ETrigA).

    TRUE is displayed as the prepared value.

  7. Click Debug → Write Value (Ctrl + F7).

    The variable is set (forced) to TRUE.

  8. Click Debug → Start (F5) to start the program.

    The program starts and processes until halting at the breakpoint in the Test_RightTestee_1 function block.

  9. Click Debug → Step Into (F8).

    The program flow skips to the FB instFB_RIGHTTESTTABLE_RIGHTTC2VALIDSTRING.

  10. Now you can step through the function blocks and observe the input and output values.