Evaluate the AutomationView desktop suite free for 30 days. No credit card required. Claim trial key →
arrow_back Back to Articles

How to Build Automated Unit Tests for PLC Sequences in AutomationView

calendar_month
person Carvalho Raphael

How to Build Automated Unit Tests for PLC Sequences in AutomationView

AutomationView
%%{init: {'theme':'dark', 'themeVariables': { 'background': '#001c38' }}}%%
---
title: AutomationView Unit Testing Architecture
---
flowchart TD
    classDef test fill:#3b82f6,stroke:#2563eb,color:#ffffff
    classDef sfc fill:#10b981,stroke:#059669,color:#ffffff
    classDef report fill:#f59e0b,stroke:#d97706,color:#ffffff
    A["Python Test Runner (PyTest)"]:::test --> B["Mock I/O Variables"]:::test
    B --> C["Execute SFC Step/Transition"]:::sfc
    C --> D{"Assert Outputs == Expected"}
    D -- Pass --> E["Generate Coverage Report"]:::report
    D -- Fail --> F["Flag Error & Halt CI/CD"]:::report
AutomationView Icon AutomationView

In modern software engineering, pushing code to production without running automated unit tests is considered professional malpractice. Yet, in the industrial automation world, engineers routinely download thousands of lines of ladder logic into critical infrastructure relying solely on manual simulation and blind faith. AutomationView changes this paradigm entirely by introducing a native Python-based unit testing framework specifically designed for Sequential Function Charts (SFCs).

Key Takeaways

  • Test-Driven Development (TDD): Write tests for your PLC sequences before you write the logic.
  • Python Native: Leverage standard Python testing libraries like pytest directly against your SFC models.
  • I/O Mocking: Simulate limit switches, sensor feedback, and drive faults without actual hardware.
  • CI/CD Integration: Automatically run tests on every Git commit before exporting to TIA Portal or Studio 5000.

The Problem with Manual PLC Simulation

Most modern PLC IDEs include a simulator (e.g., PLCSIM). While these are excellent tools, testing a sequence requires an engineer to manually toggle boolean bits to simulate sensors advancing the state machine. If an engineer modifies step 50 in a 100-step sequence, they rarely have the time to manually re-test steps 1 through 49 to ensure they didn’t introduce a regression.

AutomationView’s test runner automates this entirely. Because AutomationView sequences compile down to a Python-based execution engine before they are exported to IEC 61131-3 XML, you can interact with the state machine programmatically.

---
title: Time Spent on Regression Testing
---
pie
    "Manual Toggling (PLCSIM)" : 85
    "AutomationView PyTest" : 15

Writing Your First AutomationView Unit Test

AutomationView exposes your SFC states and variables as a Python object. You can write a test script that sets inputs, calls the .cycle() method to execute one scan of the sequence, and then uses assertions to verify the outputs.

Example: Testing a Cylinder Advance Sequence

Imagine a simple SFC where Step 1 commands a valve to open, and the transition to Step 2 requires a limit switch (I_Cyl_Extended) to be true.


import pytest
from automationview.testing import SFCModel

def test_cylinder_advance_sequence():
    # Load the sequence model
    seq = SFCModel.load("PickAndPlace.avsfc")
    
    # Initialize state
    seq.start()
    assert seq.current_step == "Step_1_Advance"
    assert seq.outputs["Q_Valve_Open"] == True
    
    # Simulate a failed sensor (Timeout)
    seq.inputs["I_Cyl_Extended"] = False
    for _ in range(500):  # Simulate 500 scans
        seq.cycle()
        
    assert seq.current_step == "Alarm_Timeout"
    
    # Reset and test successful sensor
    seq.reset()
    seq.start()
    seq.inputs["I_Cyl_Extended"] = True
    seq.cycle()
    
    assert seq.current_step == "Step_2_Retract"

Integrating with Git and CI/CD

The true power of this framework is unleashed when combined with AutomationView’s native Git integration. You can configure a CI/CD pipeline (such as GitHub Actions or GitLab CI) to run your entire test suite every time an engineer pushes a commit.

If an engineer accidentally modifies a transition condition that breaks an edge-case scenario, the PyTest suite will fail the build, preventing the broken sequence from ever being exported to the physical PLC.

Conclusion

By bringing standard software engineering practices to the automation world, AutomationView’s unit testing framework drastically reduces commissioning time on the factory floor and prevents costly regressions. Stop manually toggling bits and start automating your automation.

To try the test runner yourself, download the latest version from the AutomationView Store.

FAQ

Do I need to know advanced Python to write tests?

No. Basic knowledge of Python syntax and the assert statement is all that is required. AutomationView provides a boilerplate generator that stubs out tests for every step and transition in your sequence automatically.

Can these tests run on the physical PLC?

These unit tests run in the AutomationView simulation environment on your PC or CI server, prior to export. Once the logic is exported to Siemens or Rockwell via XML, it is executed natively on the PLC firmware.

Share this article

Stay Updated with AutomationView

Get the latest articles and news delivered directly to your inbox.

Log in to Subscribe

You must be registered and logged in to manage subscriptions.

Recommended for you

5 Ultimate Ways AutomationView Simulation Accelerates PLC Commissioning

AutomationView
%%{init: {'theme':'dark', 'themeVariables': { 'background': '#001c38' }}}%%
flowchart LR
    A["Design Sequence"] --> B["Python Simulation"]
    B --> C["Virtual Testing"]
    C --> D["Faster Commissioning"]
    style A fill:#0ea5e9,color:#fff
    style B fill:#3b82f6,color:#fff
    style C fill:#6366f1,color:#fff
    style D fill:#8b5cf6,color:#fff
AutomationView Icon AutomationView
calendar_month

5 Ultimate Ways AutomationView Simulation Accelerates PLC Commissioning

Introduction to AutomationView Simulation Every automation engineer knows the dread of powering up a new machine for the first time. The pressure is always on to cut project timelines, but skipping rigorous testing usually means finding critical sequence faults right on the factory floor—while managers tap their watches. AutomationView simulation completely flips this dynamic. By […]

Read Article arrow_forward

How to Auto-Generate IEC 61131-3 Code from AutomationView SFCs

AutomationView
%%{init: {'theme':'dark', 'themeVariables': { 'background': '#001c38' }}}%%
flowchart LR
    A["AutomationView SFC"] -->|IEC 61131-3 export| B["PLC-Open XML"]
    B --> C["Siemens TIA"]
    B --> D["Rockwell Studio 5000"]
    B --> E["CODESYS"]
    style A fill:#003b73,stroke:#fff,stroke-width:2px,color:#fff
    style B fill:#005b96,stroke:#fff,stroke-width:2px,color:#fff
    style C fill:#6497b1,stroke:#fff,stroke-width:2px,color:#000
    style D fill:#6497b1,stroke:#fff,stroke-width:2px,color:#000
    style E fill:#6497b1,stroke:#fff,stroke-width:2px,color:#000
AutomationView Icon AutomationView
calendar_month

How to Auto-Generate IEC 61131-3 Code from AutomationView SFCs

Key Takeaways: AutomationView acts as a vendor-neutral design layer before hardware commitment. IEC 61131-3 export standardizes Sequential Function Charts (SFC) into PLC-Open XML. This workflow eliminates manual transcription errors when moving to Siemens, Rockwell, or CODESYS environments. One of the biggest friction points in industrial automation projects is vendor lock-in. Engineers spend weeks designing complex […]

Read Article arrow_forward