DEV Community

yohan
yohan

Posted on Originally published at cnblogs.com

CODESYS AI Practice — Unit Testing

Environment: CODESYS V3.5 SP17 | SoftMotion Win 64 | Windows + PowerShell 5.1

The previous post, CODESYS AI Practice — Online Connection, downloaded the program into the soft PLC so we could start/stop it and read/write variables.

This post lets AI write unit tests for CODESYS.

The goal is a one-sentence ask: the AI writes the unit tests itself, imports the project, produces a red/green report, and so on. The approach also supports regression and other standard unit-testing scenarios.

Choosing an approach

Unit testing in CODESYS is not a new problem. Existing solutions fall into a few families:

Approach What it tests Simulation / real hardware Cost
Host-side pytest driving read/write Black-box interface Either Assertions sit far from the field; timing is nondeterministic
Matiec offline compile checks Pure logic No runtime connection Does not cover axis control or online state
CoUnit / UniTest (library into the PLC) White-box Either Requires installing a library; most community libraries are unmaintained
CODESYS Test Manager (3S) White-box Either Commercial; often limited in OEM environments
Hand-rolled ST asserts + host collection White-box Either You maintain the protocol; PLC side has zero dependencies

I settled on: assertions live with the logic; no PLC-side dependencies; host gets a unified result stream; design modeled on xunit.

How it works

Test code compiles with the application; assertions run in the PLC cycle; the host reads results back

Test code is ordinary ST. It compiles and downloads with the application. Assertions and the logic under test run in the same cycle over the same variables. The host only starts a round and reads results back.

A single round:

  1. The suite is attached to a cyclic task via PRG_Tests.
  2. Each case runs as Setup → assertions → TearDown.
  3. Assertions write results into the case table in GVL_Test.
  4. When all suites finish, a done flag is set.
  5. The host polls the done flag, reads counts and cases, and generates a report.

xunit mapping

  • suite → an FB_<Unit>_Test function block
  • test → a Test_* method on the suite
  • assert → calls such as ass.AssertEquals_* / AssertTrue
  • fixture → Setup / TearDown plus the suite's VAR

Capabilities

Capability Notes
Test organization suite / test / assert / fixture
Assertions Numeric, boolean, integer; Skip supported
Run targets SoftMotion / Control Win, simulation and real hardware
Results Text report, JUnit XML, exit codes
Continuous runs Reset + read-back; multi-round runs
Targeting g_sFilter for one suite; g_sFirstFailure for first failure
Dependencies Zero on the PLC side; host reuses codesys-connect / codesys-sync

Caveats

  1. Project, CODESYS, and run-target versions must match.
  2. SoftMotion Win V3 x64 device type is 4102/0000 0004.
  3. run only logs in; call download to load new logic.
  4. PRG_Tests must sit on a cyclic task and g_uiSuitesTotal must match the suite count.
  5. One Assert records one case.
  6. Prefer incremental asserts on multi-round runs.
  7. Never commit passwords; use -UserFile or environment variables.

Top comments (0)