Skip to content

Testing in CI

Ralf Steinert edited this page Nov 22, 2026 · 1 revision

Two ways to run canopen-sim in automated tests: the pytest plugin on the virtual bus (every OS, nothing to set up) and, on Linux runners, a real vcan0 for code that has to open SocketCAN itself. There is no Docker image: SocketCAN interfaces belong to the host's network namespace, so a container would not help.

pytest plugin

pip install canopen-sim registers two fixtures. Each bench runs on its own virtual channel, so tests do not see each other's frames.

def test_drive_answers(canopen_bench):                  # one bench per test
    drive = canopen_bench.remote_node()                 # canopen RemoteNode from the EDS
    assert drive.sdo.upload(0x1000, 0) == bytes.fromhex("92010200")


def test_two_buses(canopen_bench_factory):              # as many as you like
    line_a = canopen_bench_factory("generic-encoder")   # a bundled profile …
    line_b = canopen_bench_factory("benches/line-b.yaml")  # … a bench file (relative to the rootdir) …
    io = canopen_bench_factory({"nodes": [{"profile": "generic-io", "id": 5}]})  # … or a dict
  • canopen_bench starts the bench from --canopen-bench FILE or the ini key canopen_bench; without either, one generic-drive on node 1.
  • remote_node(name_or_id, cls=…) and network() need the canopen package; the bench itself does not.
  • bench.inject(...), bench.clear(...), bench.status() and bench.variables(node) work without a control port.

What to test with it: Fault injection recipes.

GitHub Actions: virtual bus

Nothing special — install and run pytest, on any runner:

jobs:
  test:
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-python@v7
        with:
          python-version: "3.13"
      - run: pip install canopen-sim canopen pytest
      - run: pytest

GitHub Actions: vcan0

For a master that opens socketcan itself, or to record the traffic with candump. This is the job canopen-sim's own CI runs:

  vcan:
    runs-on: ubuntu-24.04        # see below
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-python@v7
        with:
          python-version: "3.13"
      - run: pip install canopen-sim canopen pytest
      - name: Virtual CAN interface and can-utils
        run: |
          sudo apt-get update
          sudo apt-get install -y can-utils
          sudo modprobe vcan || {
            sudo apt-get install -y "linux-modules-extra-$(uname -r)"
            sudo modprobe vcan
          }
          sudo ip link add dev vcan0 type vcan
          sudo ip link set up vcan0
      - name: Record the bus
        run: nohup candump -L -l vcan0 > /dev/null 2>&1 &
      - run: pytest --canopen-bench bench-vcan.yaml
      - if: always()
        run: pkill candump || true
      - uses: actions/upload-artifact@v7
        if: always()
        with:
          name: candump-vcan0
          path: candump-*.log

with a bench file that puts the nodes on the interface:

# bench-vcan.yaml
bus: {interface: socketcan, channel: vcan0}
nodes:
  - {profile: generic-drive, id: 3, name: axis, heartbeat_ms: 500}

Notes from running it:

  • Pin ubuntu-24.04. In November 2026 the ubuntu-latest image had no linux-modules-extra package for its kernel, so vcan could not be loaded there.
  • One bench per node ID on vcan0. All steps of the job share the interface; do not run canopen-sim serve in the background with the same node IDs while pytest starts its own benches on vcan0.
  • The candump log is a second view of the traffic for when a test fails; it is uploaded as an artifact, not asserted.

Tests that need vcan0 can skip themselves elsewhere:

import socket
import sys

import pytest


def has_vcan0():
    if not sys.platform.startswith("linux"):
        return False
    try:
        socket.if_nametoindex("vcan0")
    except OSError:
        return False
    return True


needs_vcan = pytest.mark.skipif(not has_vcan0(), reason="needs vcan0")

Clone this wiki locally