Test coverage displayed within the editor

This article informs how the information on the test coverage is displayed within the appropriate editor. See "Executing the test" how you generate the information on the test coverage.

Displaying the test coverage within the editor

How to display the test coverage:

  1. Open the tested POU as usual with the default editor (e.g. an ST-object within the ST-editor).

  2. Within the ST- or FBD-editor, open the context menu and select the command Toggle Test Coverage. Alternative: Press Alt+Ctrl+C.
    Within the LD-editor, click the element images/download/thumbnails/484246111/ShowTestCoverage-version-1-modificationdate-1641218429399-api-v2.png within the palette .
    Result: The information on the test coverage is displayed within the editor:

    1. representation within the ST-editor

    2. representation within the FBD-editor

    3. representation within the LD-editor

Representation within the ST-editor

The background color of a line within the ST-editor informs about the test coverage of the code within the line:

images/download/attachments/414779271/TF_CodeCoverageExampleST-version-1-modificationdate-1534426594179-api-v2.png

Meaning of the background color:

  • green = fully covered

  • yellow = partially covered

  • red = not covered

If one statement consists of several lines as follows, the test coverage is only displayed for the first line of the statement:
images/download/attachments/453673668/CodeCoverageExampleST2-version-1-modificationdate-1605109445539-api-v2.png

More examples: See "Examples for a coverage in ST-editor".

It is possible that the information on the test coverage is not correctly generated for certain scenarios within the ST-editor. See "Restrictions on representing the test coverage within the ST-editor".

You can change these colors in the preferences: menu Window, Preferences, group General, Editors, Text editors, Annotations.

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/information.svg If you want details on the test coverage, best practice is to point onto the line or onto the icon left of the code.

Examples for a test coverage in ST-editor

Scenario for the examples: A POU with the input variable inputForTest is declared. There is a test case for this POU. This test case specifies a test sequence with the input value 0 for inputForTest.

The following examples result for test execution with test coverage:

ST statement

Representation

Explanation

IF statement

images/download/thumbnails/414779276/TF_CodeCoverageExampleIF-version-1-modificationdate-1534426615585-api-v2.PNG

The full test coverage for an IF statement results from 2 branches:

  1. The first statement after THEN has been executed at least once.

  2. The first statement after ELSE has been executed at least once.

Line with yellow background color – cause for the partial test coverage: The ELSE branch has not been executed.
Solution for the full coverage: In the test case, specify an additional test sequence with a value not matching the condition (e.g. 1).

Line with green background color – cause for full test coverage: The →assignment has been executed.

CASE statement

images/download/thumbnails/414779281/TF_CodeCoverageExampleCASE-version-1-modificationdate-1534426636030-api-v2.PNG

The full test coverage for a CASE statement results from 2 branches:

  1. All labels (after OF) have been reached and the first statement within a label has been executed at least once.

  2. The first statement after ELSE has been executed at least once.

Line with yellow background color – cause for the partial test coverage: The ELSE branch has not been executed. Moreover, not all of the labels have been reached (due to the line with the red background color).
Solution for the full coverage: In the test case, specify an additional test sequence with a value not matching the labels (e.g. 1). Moreover, specify an additional test sequence with the value 55.

Line with green background color – cause for full test coverage: The statements following the label 0,3,15..22 have been executed fully because the input value 0 is specified in the test sequence. Statements within a label are considered as fully covered, as soon as the statements have been executed at least once – even though additional values are specified within the label (here: 3,15..22).

Line with red background color – cause for no test coverage: The statements following the label 55 has not been executed because only the input value 0 has been specified within a test sequence.

WHILE statement

images/download/attachments/414779286/TF_CodeCoverageExampleWHILE-version-1-modificationdate-1534426655904-api-v2.PNG

The full test coverage for a WHILE statement results from 2 branches:

  1. The first statement after DO has been executed at least once.

  2. The statements after DO have not been executed any longer because the condition (after WHILE) has not been true anymore.

Line with yellow background color (with WHILE) – cause for the partial test coverage: Branch 1 has been executed, but branch 2 has not been executed due to the EXIT statement.
Solution for the full coverage: In the test case, specify an additional test sequence with a value not being 0 (e.g. 1).

Restrictions on representing the test coverage within the ST-editor

An incorrect test coverage is generated for the following scenarios. The result is the representation of a test coverage that does not match the actual test execution.

Scenario 1: The specified input value is used for the CASE level and for the condition within the included IF statement.

If a test is executed for the following function block FB1 and its test case specifies the input value 2 for Var1, a full test coverage is displayed for the line IF Var1 = 2 THEN.
But this representation of the full test coverage is not correct. The branch 1, 2, 33..44 of the CASE statement is only partially covered. The test case would have to contain an additional test sequence with a value ≠ 2 so that the CASE and IF statement are really fully covered.

FUNCTION_BLOCK FB1
VAR_INPUT
Var1 : INT;
END_VAR
VAR
x, y : INT;
END_VAR
 
CASE Var1 OF
1, 2, 33..44 :
x := 3;
IF Var1 = 2 THEN (* This line is displayed as fully covered in case of a test sequence with input value '2'. This display is incorrect. *)
y := 666;
END_IF;
END_CASE;
END_FUNCTION_BLOCK

Scenario 2: The specified input value is used for the CASE level, an IF statement is included within the CASE statement.

If a test is executed for the following function block FB2 and its test case specifies the input value 1 for Var2, a full test coverage is displayed for the line IF z = 3 THEN.
But this representation of the full test coverage is not correct. As the ELSE branch has not been executed, this IF statement is only partially covered. Even if the test case contains an additional test sequence with another value, the IF statement will never be fully covered because of the ST-logic.

FUNCTION_BLOCK FB2
VAR_INPUT
Var2 : INT;
END_VAR
VAR_OUTPUT
Result : INT;
END_VAR
VAR
x : INT;
z : INT := 3;
END_VAR
 
CASE Var2 OF
1, 2 :
x := 4;
IF z = 3 THEN (* This line is displayed as fully covered in case of a test sequence with input value '1'. This display is incorrect. *)
x := 6;
END_IF;
ELSE
x := 15;
END_CASE;
END_FUNCTION_BLOCK

Representation within the FBD-editor

Only the →calls of blocks are relevant for the test coverage within the FBD-editor. A partial coverage is only possible for calls of SEL blocks or of MUX blocks (see under Select functions) and for calls with a connected input EN.

images/download/attachments/414779291/TF_CodeCoverageExampleFBD-version-1-modificationdate-1534426683147-api-v2.png

An icon inform about the test coverage of the call:

  • images/download/thumbnails/414779296/TF_CoverageFull-version-1-modificationdate-1534426723981-api-v2.png = fully covered

  • images/download/thumbnails/414779301/TF_CoveragePartial-version-1-modificationdate-1534426742942-api-v2.png = partially covered

images/s/b2ic8e/9012/1ca6q62/_/images/icons/emoticons/information.svg A red icon for "not covered" is not applied in the FBD-editor.

See "Examples for a test coverage in FBD-editor".

Examples for a test coverage in FBD-editor

The call of the NOT block is fully covered.

images/download/attachments/414779306/TF_CodeCoverageExampleFBD1-version-1-modificationdate-1534426763028-api-v2.png

Representation within the LD-editor

The contacts and the →calls of blocks (analogously to the representation within the FBD-editor) are relevant for the test coverage within the LD-editor.

An icon informs about the test coverage within the LD-editor as well:

  • images/download/thumbnails/414779296/TF_CoverageFull-version-1-modificationdate-1534426723981-api-v2.png = fully covered

  • images/download/thumbnails/414779301/TF_CoveragePartial-version-1-modificationdate-1534426742942-api-v2.png = partially covered

  • images/download/thumbnails/484246119/TF_CoverageNo-version-2-modificationdate-1641220137589-api-v2.png = not covered