Implementing TDD in PLC logic: translating Python asserts to SCL
Use a Python test harness while 2ot lowers the tests into native PLC test programs with auto-generated instance DBs and Arrange-Act-Assert structure.
Test-Driven Development in the OT world has been blocked on tooling, not philosophy. Engineers know how to write unit tests; they do not have a runtime that lets them. Hardware-in-the-loop rigs are expensive, vendor simulators are partial, and the SCL ecosystem does not ship a test harness anyone wants to use.
A Python test harness can stay as the interface the team already knows. 2ot sits underneath it: the transpiler reads the Python AST, recognizes the canonical Arrange-Act-Assert pattern, and lowers each test into a native PLC test harness that can run headlessly in automation.
The shape of a test
# tests/test_motor_start.py
def test_motor_starts_when_safe():
# Arrange
inputs = MotorInputs(emergency_stop=False, run_command=True)
# Act
result = motor_start(inputs)
# Assert
assert result.motor_run is True
assert result.fault is FalseThat file becomes a real PLC program. For every function under test, 2ot generates a dedicated Instance Data Block — the iDB acts as the memory sandbox for one test, isolating its state from siblings. Python data structures map cleanly onto PLC primitives: dataclasses become STRUCTs, lists become ARRAYs with their bounds materialized as ValuesLength metadata, numeric literals are typed against the target dialect.
Arrange · Act · Assert lowering
- Arrange — Python assignments populate the iDB inputs as SCL := assignments.
- Act — Python function calls become FB invocations against that iDB.
- Assert — Python assertions lower to strict boolean comparisons that emit diagnostic codes on failure.
Why this changes the loop
Because the generated harness is just SCL, it runs anywhere SCL runs — including a software PLC inside a container on a GitHub runner. A pull request that modifies a safety interlock can trigger the tests automatically, and the resulting status returns to the team's chosen workflow. The feedback loop that mainstream software has had for twenty years finally fits an automation team's workflow.