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 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:
- The suite is attached to a cyclic task via
PRG_Tests. - Each case runs as
Setup→ assertions →TearDown. - Assertions write results into the case table in
GVL_Test. - When all suites finish, a done flag is set.
- The host polls the done flag, reads counts and cases, and generates a report.
xunit mapping
-
suite → an
FB_<Unit>_Testfunction block -
test → a
Test_*method on the suite -
assert → calls such as
ass.AssertEquals_*/AssertTrue -
fixture →
Setup/TearDownplus the suite'sVAR
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
- Project, CODESYS, and run-target versions must match.
- SoftMotion Win V3 x64 device type is
4102/0000 0004. -
runonly logs in; calldownloadto load new logic. -
PRG_Testsmust sit on a cyclic task andg_uiSuitesTotalmust match the suite count. - One
Assertrecords one case. - Prefer incremental asserts on multi-round runs.
- Never commit passwords; use
-UserFileor environment variables.

Top comments (0)