Repository navigation
Testing in CI
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.
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_benchstarts the bench from--canopen-bench FILEor the ini keycanopen_bench; without either, onegeneric-driveon node 1. -
remote_node(name_or_id, cls=…)andnetwork()need thecanopenpackage; the bench itself does not. -
bench.inject(...),bench.clear(...),bench.status()andbench.variables(node)work without a control port.
What to test with it: Fault injection recipes.
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: pytestFor 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-*.logwith 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 theubuntu-latestimage had nolinux-modules-extrapackage for its kernel, sovcancould not be loaded there. -
One bench per node ID on
vcan0. All steps of the job share the interface; do not runcanopen-sim servein the background with the same node IDs while pytest starts its own benches onvcan0. - The
candumplog 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")canopen-sim · MIT licensed · CANopen® and CiA® are registered trademarks of CAN in Automation (CiA) e.V.; canopen-sim is not certified by or affiliated with CiA.