Table of Contents

Embedded tests (generated harness)

This is the layout we recommend. You write test function blocks in the PLC project. zkbuild-action and zkmake detect them from the interface they implement and generate the TcUnit harness. There is no separate test solution to maintain.

The same function blocks run locally in Test Explorer. Test Explorer is part of Zeugwerk Creator and is included with it. You can use it for free. No additional license has to be bought.

Other layouts, result files, and CI reporting: Unit tests.


Why this layout

  • Tests live next to the code. Test function blocks sit in the same project as the production code, so it stays obvious what is covered.
  • Release builds omit the tests. The build excludes function blocks that implement the test interface when it compiles the library you ship.
  • No extra PLC task in the library. A library does not need its own task just to execute tests. The generated harness provides that.
  • Protected members stay reachable. A test function block that extends the production function block can call protected members. Structured Text has no friend mechanism, so extension is the practical way to reach that state.
  • DataRows. The generator turns each DataRow attribute into its own named test case. One method covers many inputs, and a failure names the row.
  • One project to maintain. One .plcproj, one set of library references, one place for the implementation and its tests.
  • IDE and CI share the tests. Test Explorer in Creator runs them on your machine. The build tools run the same function blocks in the pipeline.

Run them in Test Explorer

Test Explorer ships with Zeugwerk Creator. A Creator license covers it. There is no separate Test Explorer product and no extra license to purchase.

  1. Install Creator (the Zeugwerk Development Kit includes a trial license).
  2. Open Tools > Zeugwerk Creator > Test Explorer.
  3. Click Run All Tests.

Test Explorer compiles the PLC, installs it as a local library, generates tests/Tests.sln, compiles that harness, and activates it on a target. The walkthrough, including screenshots, is on the Test Explorer page.

Name test methods with the prefix Test_ so Test Explorer lists them. The build tools detect a test by its signature, and Test Explorer also requires that prefix.

Supported interfaces

Interface Package Notes
ZCore.IUnitTest Zeugwerk Framework For projects that already use the Framework
Testbench.IUnitTest Testbench Lightweight alternative without the full Framework

Use one interface per project.

Test method signatures

Three signatures are recognized. Any method with one of them, on a function block that implements the test interface, becomes a test case.

Assertions. Pure logic that finishes in a single call:

METHOD Test_PositiveVelocity
VAR_INPUT
  assertions : ZCore.IAssertions;  // or Testbench.IAssertions
END_VAR

Context. Tests that need PLC cycles (timers, state machines, motion). ITestContext exposes cycle helpers and Assertions. While the test still needs cycles, set context.Busy := TRUE. The method is called again until Busy is FALSE.

METHOD Test_MoveCompletes
VAR_INPUT
  context : ZCore.ITestContext;  // or Testbench.ITestContext
END_VAR

Expected and actual. One comparison, without the assertion interface. The three outputs are expected, actual, and message. expected and actual share an IEC type (BOOL, INT, DINT, LREAL, ...).

METHOD Test_Clamp_Output
VAR_OUTPUT
  expected : DINT;
  actual : DINT;
  message : STRING(255);
END_VAR

DataRows

A DataRow attribute binds one set of input values to the method. The generator emits a separate named test case per row. Each row is its own entry in the JUnit XML, so a failure names the inputs.

DataRows attach to the assertions signature and to the context signature. Place the extra inputs after assertions or context.

{attribute 'DataRow(0, 0)'}
{attribute 'DataRow(100, 100)'}
{attribute 'DataRow(1000, 500)'}
{attribute 'DataRow(-100, -100)'}
METHOD Test_VelocityClamping
VAR_INPUT
  assertions : ZCore.IAssertions;
  input      : DINT;
  expected   : DINT;
END_VAR

assertions.EqualsDint(expected, velocity_controller.Clamp(input), 'unexpected clamped value');
END_METHOD

Example

FUNCTION_BLOCK MotionTest EXTENDS MotionController IMPLEMENTS ZCore.IUnitTest

METHOD Test_PositiveVelocity
VAR_INPUT
  assertions : ZCore.IAssertions;
END_VAR

assertions.EqualsDint(100, MotionController_instance.SetVelocity(100), 'velocity should be clamped');
END_METHOD

METHOD Test_MoveCompletes
VAR_INPUT
  context : ZCore.ITestContext;
END_VAR
VAR_INST
  cycles : INT;
END_VAR

cycles := cycles + 1;
MotionController_instance.Move(100);
context.Busy := NOT MotionController_instance.Done AND cycles < 200;

IF NOT context.Busy THEN
  context.Assertions.EqualsDint(100, MotionController_instance.Position, 'wrong final position');
END_IF
END_METHOD

Working copies of all three signatures are in the Testbench Examples PLC.

What the build does

  1. Scans the PLC for function blocks that implement IUnitTest.
  2. On the release compile, excludes those function blocks so they are absent from the shipped library.
  3. Builds a testable configuration of the library and generates a harness under tests/ (tests/Tests.sln, PLC name Tests).
  4. Deploys that harness to the configured target and writes JUnit XML.

--test-framework on zkmake build applies to this layout only.

Value Description
tcunit-zeugwerk Default. Zeugwerk fork of TcUnit used by the generator (DataRows, ITestContext).
tcunit Upstream TcUnit.

zkbuild-action selects the framework on the server. There is no matching action input.

If the library contains embedded tests, the build recreates tests/ for the harness. Keep a hand-written TcUnit solution in its own solution under tests/ only when the library has no IUnitTest function blocks.