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
Create a new standard project in CODESYS.
In the device tree, select the Application object.
Click .
Select the Test Table object.
The Add Test Table dialog opens.
In the Name input field, specify the name.
Example:
Test_TONIn the Testee setting, click the
button.The Input Assistant dialog opens.
Select the
TONPOU from theStandardlibrary.Select the Insert with namespace prefix option.
Click OK to exit the dialog.
The
Standard.TONfunction 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.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.
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.
Click Add to confirm the dialog.
The
Test_TONtest table with theTONtestee has been inserted below the application.The Tabular Editor tab appears in the editor with the automatically configured columns
IN,PT,Q, andET. 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.
Double-click the test table object.
In the editor, select the Tabular Editor.
Select the row
<click to create a test case> and specify the name Test case 1.Test case 1is inserted with a test step below it.Select the test step
<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.
Double-click in the
INcolumn in the row of the test step.A list box is displayed which contains possible assignments for the column. Because
INis an input (typeBOOL), you can select from a list of assignments of the valuesTRUEorFALSE.Select the
:= FALSEoption.The input condition is configured. The value
FALSEis assigned to the input during the test run.In the row of the test step, double-click in the
Qcolumn.A list box is displayed which contains possible assignments for the column. Because
INis an output (typeBOOL), you can select from a list of tests of the valuesTRUE,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).In the row of the test step, double-click in the
ETcolumn.A set of list boxes is displayed which contain possible assignments for the columns. Because
ETis an input (typeTIME), you can edit a comparison from a list of tests.The list box with different test run scenarios is the last one (test mode).
Specify the testee as
= T#0s.Assign the following values to the test step Step 1: Reset counter:
Duration: Single
Call:

The counter POU is executed with the input assignments during the test and the output assignments are checked.
Select the row in Step 1
<edit to create a test step> and specify the name Check counter 1.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:

IN
:= TRUE Set true [t]PT
:=T#10sQ
= FALSE Is false [f]ET ↗
Increasing [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.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:

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
TRUEafter 10 seconds and the ET output has the valueT#10s.
Executing a unit test
Create a new test script.
Click .
The Test Manager view opens.
Select the Test Scripts entry and click .
A new test script is added.
Double-click the new test script.
The test script opens in the editor.
Select the test script in the editor and click .
An
IEC unit test is added to the test script.Click the Save + Run button.
The test script is executed and saved.
A test report is created with a summary of the test results.