Skip to main content

Testing the TON Function Block

This example shows the test of the standard TON function block in the Standard library. The test code is configured with a test table.

Inserting the Test Table object for the TON testee

  1. Create a new standard project in CODESYS.

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

  3. Click Project → Add Object.

  4. Select the Test Table object.

    The Add Test Table dialog opens.

  5. In the Name input field, specify the name.

    Example: Test_TON

  6. In the Testee setting, click the _cds_icon_three_dots.png button.

    The Input Assistant dialog opens.

  7. Select the TON POU from the Standard library.

  8. Select the Insert with namespace prefix option.

  9. Click OK to exit the dialog.

    The Standard.TON function block is defined as the testee in the Insert test table dialog. Because the namespace of the library is prepended to the FB name, access to the POU is unique.

  10. Click the Scan Testee button.

    A list of all inputs and outputs of the function block is displayed in Select from available inputs and outputs.

  11. Activate all inputs and outputs.

    The enabled inputs and outputs are displayed as columns on the Tabular Editor tab in order to simplify the configuration of the input and output assignments. However, the conditional values for the inputs and the specified values for the outputs are missing.

  12. Click Add to confirm the dialog.

    The Test_TON test table with the TON testee has been inserted below the application.

    The Tabular Editor tab appears in the editor with the automatically configured columns IN, PT, Q, and ET. A column has been configured for the enabled inputs and outputs. Inputs have gotten the column type Assignment. The assignments of the conditional values are missing. Outputs have gotten the column type Testee. The test values are missing for the specified/actual comparisons.

Configuring a test table

Requirement: The application contains a test table.

  1. Double-click the test table object.

  2. In the editor, select the Tabular Editor.

  3. Select the row tm_icon_test_case.png <click to create a test case> and specify the name Test case 1.

    Test case 1 is inserted with a test step below it.

  4. Select the test step tm_icon_test_step.png <edit to create a test step> and specify the name Step1: Reset counter. Note that a test step name may contain spaces. It corresponds to a comment, not an identifier.

    The test step appears with a Single duration and enabled call. A blank test step appears below it as input assistance for another test step.

  5. Double-click in the IN column in the row of the test step.

    A list box is displayed which contains possible assignments for the column. Because IN is an input (type BOOL), you can select from a list of assignments of the values TRUE or FALSE.

  6. Select the := FALSE option.

    The input condition is configured. The value FALSE is assigned to the input during the test run.

  7. In the row of the test step, double-click in the Q column.

    A list box is displayed which contains possible assignments for the column. Because IN is an output (type BOOL), you can select from a list of tests of the values TRUE, FALSE, and their transitions.

    Another list box is displayed next to it which contains different test run scenarios (test mode), which are run when the output value is reached (example: Abort test case. The default is --. Then the next test step is executed (Not set).

  8. In the row of the test step, double-click in the ET column.

    A set of list boxes is displayed which contain possible assignments for the columns. Because ET is an input (type TIME), you can edit a comparison from a list of tests.

    The list box with different test run scenarios is the last one (test mode).

  9. Specify the testee as = T#0s.

  10. Assign the following values to the test step Step 1: Reset counter:

    • Duration: Single

    • Call: standard icon

    The counter POU is executed with the input assignments during the test and the output assignments are checked.

  11. Select the row in Step 1 tm_icon_test_step.png <edit to create a test step> and specify the name Check counter 1.

  12. In Step 2: Check counter 1, assign values also to the input variables and output variables. In the row of the test step, double-click in the corresponding column to do this. An input field corresponding to the data type of the column is displayed.

    • Duration: T#10s (Select the Time option and specify a value.)

    • Call: standard icon

    • IN:= TRUE Set true [t]

    • PT:=T#10s

    • Q = FALSE Is false [f]

    • ETIncreasing [i]

    When the test is executed, the test object is called for 10 seconds. During this time, the operation also tests whether the Q output remains at FALSE, and whether the ET output increments steadily.

  13. Assign values to the input and output variables. In the row of the test step, double-click in the corresponding column to do this. An input field corresponding to the data type of the column is displayed.

    • Duration: T#10s (Select the Time option and specify a value.)

    • Call: standard icon

    • IN: --- Skip []

    • PT:--- Skip []

    • Q: := TRUE Is true [t]

    • ET: = T#10s

    When the test is executed, the test object is called for 10 seconds. The operation tests whether the Q output is TRUE after 10 seconds and the ET output has the value T#10s.

Executing a unit test

Create a new test script.

  1. Click Tools → Test Manager.

    The Test Manager view opens.

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

    A new test script is added.

  3. Double-click the new test script.

    The test script opens in the editor.

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

    An _tm_icon_iec_unit_test.png IEC unit test is added to the test script.

  5. Click the Save + Run button.

    The test script is executed and saved.

    A test report is created with a summary of the test results.