Listen to this Post

Introduction:
For decades, industrial automation engineers have accepted a painful reality: validating Programmable Logic Controller (PLC) code requires physical hardware or a simulator, manual input manipulation, and visual output verification. This process is not only time-consuming but also error-prone, often leading to commissioning surprises when logic fails under unanticipated conditions. The emergence of unit testing frameworks within modern IEC 61131-3 development environments, such as SIMATIC AX, represents a paradigm shift—bringing software engineering rigor to the industrial control world by enabling automated validation of logic execution entirely on a PC, long before code ever touches a controller.
Learning Objectives:
- Understand the concept of unit testing for PLC code and its benefits over traditional testing methods.
- Learn how to set up and execute automated tests in SIMATIC AX using the `apax test` command.
- Explore practical test scenarios for a temperature controller, including state transitions, hysteresis, and fault detection.
You Should Know:
- Setting Up Your Unit Testing Environment for PLC Code
To replicate the automated testing workflow, you must first establish a development environment that supports SIMATIC AX. Unlike TIA Portal, which relies on simulation or physical hardware, SIMATIC AX is built on modern toolchains. The core component is the Apax CLI, a package manager and build tool.
Step‑by‑step guide:
- Install SIMATIC AX: Download and install the SIMATIC AX environment from Siemens. This includes VS Code extensions and the underlying runtime.
- Verify Prerequisites: Ensure Node.js is installed (as Apax is Node-based). On Linux (Ubuntu/Debian) :
sudo apt update && sudo apt install nodejs npm
On Windows, download the installer from nodejs.org or use winget:
winget install OpenJS.NodeJS
- Create a New Project: Open VS Code, use the SIMATIC AX extension to create a new project. This generates the necessary `apax.yml` manifest and folder structure.
- Add a Test Library: In your
apax.yml, include the unit testing framework dependency (e.g.,@ax/simatic-ax-test) to gain access to assertion methods. - Write a Simple Test: Create a file `TemperatureController.test.cs` (in C-like ST syntax). A basic test structure:
using AX.Test;</li> </ol> [bash] public void When_Temperature_Is_Below_Setpoint_Heating_Turns_On() { // Arrange var controller = new TemperatureController(); controller.Setpoint = 70; controller.CurrentTemperature = 60; // Act controller.Update(); // Assert Assert.IsTrue(controller.HeatingOutput); }6. Run the Test: Open a terminal in your project root and execute:
apax test
This compiles and runs all tests, reporting results in the terminal.
2. Automating Test Scenarios Without PLCSIM
The core advantage is eliminating the manual “download to PLC, set watch table, toggle inputs, check outputs” cycle. Instead, you define scenarios that simulate real-world conditions.
Step‑by‑step guide for a temperature controller test suite:
- Scenario 1 – Heating ON: As shown above, verify that when temperature drops below setpoint minus hysteresis, the heating output activates.
- Scenario 2 – Heating OFF: When temperature exceeds setpoint plus hysteresis, heating deactivates.
- Scenario 3 – Hysteresis Band: Test that the output remains stable within the deadband, preventing rapid cycling.
[bash] public void Hysteresis_Prevents_Rapid_Cycling() { var ctrl = new TemperatureController(); ctrl.Setpoint = 100; ctrl.Hysteresis = 2;</li> </ul> // Start below lower threshold ctrl.CurrentTemperature = 97; // below 98 (100 - 2) ctrl.Update(); Assert.IsTrue(ctrl.HeatingOutput); // Increase temperature, but stay within deadband ctrl.CurrentTemperature = 99; // still within deadband (98-102) ctrl.Update(); Assert.IsTrue(ctrl.HeatingOutput, "Heating should stay ON inside deadband"); // Rise above upper threshold ctrl.CurrentTemperature = 103; // above 102 ctrl.Update(); Assert.IsFalse(ctrl.HeatingOutput); }– Scenario 4 – Fault Detection: Simulate a sensor error (e.g., invalid temperature reading) and verify that the controller enters a safe state, turning off heating.
[bash] public void Sensor_Fault_Sets_Output_To_Safe_State() { var ctrl = new TemperatureController(); ctrl.Setpoint = 100; ctrl.CurrentTemperature = -999; // invalid sensor value ctrl.Update(); Assert.IsFalse(ctrl.HeatingOutput); Assert.IsTrue(ctrl.FaultIndicator); }– Scenario 5 – Error Calculation: For PID or advanced controllers, test that the error term (setpoint – actual) is calculated correctly under different conditions.
3. From PC to Controller: Integration and CI/CD
Unit tests on your PC are the first line of defense. They ensure the logic behaves as designed. To maintain quality, integrate these tests into a continuous integration (CI) pipeline.
Step‑by‑step guide:
- Version Control: Store your SIMATIC AX project in a Git repository. This includes all `.cs` source files and test files.
2. CI Pipeline (GitHub Actions Example): Create `.github/workflows/test.yml`:
name: PLC Unit Tests on: [push, pull_request] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup Node.js uses: actions/setup-node@v4 with: node-version: '18' - name: Install Apax run: npm install -g @ax/apax - name: Run Unit Tests run: apax test
3. Automated Gatekeeping: Configure the pipeline so that any pull request with failing tests cannot be merged, ensuring only verified code reaches the hardware.
- Bridging the Gap: TIA Portal vs. SIMATIC AX Testing
For engineers accustomed to TIA Portal, the concept of writing tests as code can feel foreign. However, the benefits are tangible:
– Reproducibility: Run the same eight test scenarios (like the temperature controller example) on demand, every time you change the code.
– Speed: A test suite executes in seconds, versus minutes to download and manually configure PLCSIM.
– Documentation: Tests serve as living documentation for how the logic is supposed to behave under various conditions.
– Linux/Windows Agnostic: The `apax test` command works identically on both platforms, enabling consistent development across teams.5. Advanced Techniques: Mocking and External Dependencies
Real-world PLC code often interfaces with I/O modules, field devices, or other controllers. Unit testing allows you to mock these dependencies.
Step‑by‑step guide:
- Define an Interface: For a temperature sensor, create an interface
ITemperatureSensor. - Implement a Mock: In your test project, create `MockTemperatureSensor` that implements the interface and allows you to programmatically set readings.
- Inject the Mock: Pass the mock to your controller during test setup. This isolates the logic from hardware dependencies.
[bash] public void Controller_Responds_To_Mock_Sensor_Changes() { var mockSensor = new MockTemperatureSensor(); var controller = new TemperatureController(mockSensor); mockSensor.SetTemperature(50); controller.Setpoint = 100; controller.Update(); // Should turn heating ON Assert.IsTrue(controller.HeatingOutput); }
6. Mitigating Common Pitfalls
- Test Pollution: Ensure each test runs in isolation. Use setup methods (
[bash]) to reinitialize objects before each test. - Timing Issues: Unit tests are deterministic. Avoid using `WAIT` or `TON` timers in test scenarios—instead, simulate elapsed time by calling a method like
AdvanceTime(seconds). - Code Coverage: Aim for high coverage, especially on critical paths like fault handling and safety interlocks.
What Undercode Say:
- Key Takeaway 1: Unit testing in SIMATIC AX transforms PLC code validation from a manual, hardware-dependent chore into an automated, repeatable software engineering practice.
- Key Takeaway 2: The ability to run tests locally on a PC using a simple command (
apax test) significantly reduces commissioning risks and accelerates development cycles for industrial control systems.
The industrial automation sector is steadily adopting practices long standard in IT and software development. By embracing unit testing, control engineers gain the same confidence that software developers have—knowing instantly if a code change introduces a regression. This not only improves code quality but also shifts the focus from debugging on the factory floor to designing robust logic in the development environment. For organizations, it means fewer costly delays, safer commissioning, and a more agile response to process changes.
Prediction:
As industrial control systems continue to converge with traditional IT practices, the adoption of automated testing frameworks like SIMATIC AX’s unit testing will become mandatory for high-criticality applications. We predict that within the next five years, major automation vendors will integrate such testing capabilities into their mainstream offerings (like TIA Portal), and certification bodies (e.g., ISA/IEC 62443) will begin requiring evidence of automated testing for certain security and safety certifications. The future of PLC programming is one where code is verified in a CI/CD pipeline before it ever powers a physical controller—making industrial automation more resilient, secure, and agile.
▶️ Related Video (72% Match):
🎯Let’s Practice For Free:
IT/Security Reporter URL:
Reported By: Yuriy Mosiyenko – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅🔐JOIN OUR CYBER WORLD [ CVE News • HackMonitor • UndercodeNews ]
📢 Follow UndercodeTesting & Stay Tuned:


