Testing PLC programs with AI: unit tests and simulation in PLCSIM
A PLC program is often only really tested for the first time on the machine. Errors are then hunted down under pressure and in front of the customer. Simulation in PLCSIM can move a large part of testing to the desk, but few people have time to write tests by hand. We describe how to split testing between the AI and the programmer: block unit tests, scenarios, a virtual FAT and what simulation will not reveal.
Why test a PLC program before commissioning
A logic error found at the desk costs a few minutes. The same error on the machine costs the whole team's time and sometimes material. A test in simulation can also be repeated after every program change, which a trial on the machine usually does not allow. Even so, PLC programmers write few tests, because preparing test data, test blocks and a report takes time. This is exactly where AI helps most.
Three levels of PLC program tests
- Block unit test. One FB or FC, given inputs and expected outputs. It verifies calculations, conversions, protections and edge cases.
- Scenario on a simulated PLC. A sequence of steps: set an input, wait and check an output within a given time. It verifies how blocks work together and the timing.
- Virtual FAT. Tests of the whole process based on the objects in the project: motors, valves, analog measurements, PID controllers, alarms, the I/O list and sequences.
All three levels run in S7-PLCSIM Advanced. Nothing is downloaded to a real PLC.
Block unit tests in TIA Portal
You describe a unit test as test cases: a name, input values, expected outputs and, if needed, the number of cycles and a tolerance. From these TIA MCP generates a test FB named UT_ plus the block name, its instance DB and an OB UT_Runner that runs the tests. It compiles the test block straight away.
When you run the tests, TIA MCP downloads the program to PLCSIM Advanced, switches the simulation to RUN and reads the results: how many cases passed, how many failed and how each one turned out. The AI explains the result and, for a failure, shows which value the block returned instead of the expected one.
Here the AI mainly saves typing. From the block interface and its code it suggests cases including edge cases: zero, a negative value, the upper limit of the range, start and stop active at the same time. The programmer reviews them and fills in the expected values according to the specification.
Scenarios and a virtual FAT in PLCSIM
A scenario is a list of steps. In each step, tags are set, the given time or number of cycles elapses and the expected value is checked, with a tolerance for analog values. A scenario can also be written in Excel: TIA MCP prepares a template the process engineer can fill in, and after the run it saves a report with the set, expected and actual values.
A virtual FAT goes further. From global data blocks it recognises motors, valves, analog measurements and PID controllers and suggests tests: motor start and stop, loss of feedback, an analog limit or a setpoint step. During the run, simple process models in a closed loop read the PLC outputs and return feedback. The result is a report in PDF, Excel and JSON and, optionally, a recording of tag values to CSV.
TIA MCP does not invent what cannot be assigned unambiguously. It marks such a case as needing review and does not run it until you complete it.
The Tests page in the TIA MCP web interface
Since version 2.19.0 the TIA MCP web interface has a Tests page. In one place you select the PLC and the PLCSIM Advanced instance, download the program to the simulator and work in tabs: block unit tests with a test case editor, virtual FAT, PLCSIM scenarios, TIA Test Suite and HMI test reports. The page warns about missing prerequisites, for example when PLCSIM is not running, its version does not match TIA Portal or the CPU is not in RUN. Write actions require confirmation and are disabled in read-only mode.
If you use TIA Test Suite, you can run the style guide check and application tests through TIA MCP. This requires TIA Portal V21 and a Test Suite licence.
Example requests for the AI
Suggest unit tests for FC_Scale in PLC_1: input 0 to 27648, output 0 to 100 %. Add edge cases and out-of-range values. Show me the cases in a table and generate the test only after my approval.
Run the unit tests of FB_Pump in the PLCSIM Advanced instance PLC_1_SIM and explain every failed case. Do not fix the block code, just suggest a fix.
Prepare a virtual FAT plan for PLC_1 for motors and valves only. Show the cases marked as needing review and write what is missing in them.
Create a scenario: set Start to TRUE; within 2 s Motor_Run must be TRUE. Then set Porucha to TRUE; within 500 ms Motor_Run must be FALSE and Alarm_Motor TRUE. Run it in simulation and save the report to Excel.
What to watch out for
- Expected values come from the specification, not from the code. If the AI derives the expected result from the code under test, the test passes even with a bug. Check the values against the specification or the agreement with the process engineer.
- Test blocks do not belong in the customer's PLC. Remove the UT_ blocks and the UT_Runner OB from the project before handover.
- Simulation is not the machine. PLCSIM does not simulate real I/O, drives or bus behaviour. Tests do not replace the trial on the machine; they only shorten it.
- Prerequisites. You need PLCSIM Advanced in a version matching TIA Portal and simulation support enabled for block compilation in the project. TIA MCP can switch it on.
- The safety program. It is validated according to the standards by a qualified person. Tests in simulation are an aid, not a validation.
Summary
AI connected to TIA Portal lowers the main obstacle to testing a PLC program, which is preparation time. It suggests test cases, generates test blocks, runs them in PLCSIM Advanced and explains the results. The programmer supplies what the AI cannot know: the correct expected values and the judgement of what still has to be tried on the machine.
You will find details on the tools on the page Testing PLC programs in PLCSIM with AI.
Frequently asked questions
How do I write a unit test for a Siemens PLC?
For an FB or FC you prepare test cases: inputs and expected outputs. TIA MCP generates a test FB and OB from them, downloads the program to PLCSIM Advanced and reads the results. The AI helps to suggest the cases.
Is standard S7-PLCSIM enough, or do I need PLCSIM Advanced?
TIA MCP can download the program to both S7-PLCSIM and PLCSIM Advanced. Unit tests, scenarios and the virtual FAT, however, need PLCSIM Advanced, because TIA MCP reads and writes tag values through its interface.
Will the AI download tests to a real PLC?
No. The test tools work only with the simulator. Downloading to a real PLC is disabled by default in TIA MCP and requires double confirmation.
Does a virtual FAT replace a real FAT?
No. A virtual FAT checks the program logic against simple process models and prepares a report. A real FAT checks the machine, the wiring, the I/O and the safety functions.
Related articles and pages
- AI review of a PLC program before FAT: code review in TIA Portal
- Converting STL (AWL) to SCL with AI: procedure and pitfalls
- AI and WinCC Unified: what can and cannot be automated on screens
- TIA Portal project documentation in minutes: what AI can do and what a human must check
- Testing PLC programs in PLCSIM with AI
Test your program in simulation
TIA MCP connects ChatGPT, Claude, Copilot or Gemini to TIA Portal V17–V21 on Windows. A PRO licence costs €295 per year per computer excluding VAT, and the first 7 days are free.
Siemens, SIMATIC, TIA Portal, WinCC and S7 are trademarks of Siemens AG. TIA MCP is an independent product of Benešl Automation, is not a Siemens product and works with TIA Portal through the official TIA Portal Openness interface.