Skip to main content

PyPI Downloads Python License Platform Status

toggletest

Universal Python library for hardware toggle switch testing automation.

Supports Arduino (USB-Serial), Raspberry Pi (GIPO), and a software Mock backend all through the same API.

Built by: Vaishnavi Rathi | MS ECE @ Northeastern University


Install

pip install toggletest              # core only (mock backend)
pip install toggletest[arduino]     # + pyserial for Arduino
pip install toggletest[rpi]         # + RPi.GPIO for Raspberry Pi
pip install toggletest[all]         # everything including pytest

Arduino setup

Upload examples/arduino_firmware/toggle_bridge.ino to your Arduino before using ArduinoBackend.


Quick start

With Arduino (hardware)

from toggletest import Toggle, ArduinoBackend

with ArduinoBackend(port='/dev/ttyUSB0') as hw:
    t = Toggle(hw, pin=2, mode='INPUT', debounce_ms=50, label='power_switch')
    events = t.monitor(duration=10.0, poll_hz=100)
    print(t.summary())

With MockBackend (no hardware — great for CI)

from toggletest import Toggle, MockBackend

with MockBackend(initial_states={2: False}) as hw:
    t = Toggle(hw, pin=2, mode='INPUT', debounce_ms=50)
    hw.set_state(2, True)   # simulate switch press
    print(t.read())         # True
    print(t.change_count)   # 1

Driving an output toggle

with ArduinoBackend() as hw:
    t = Toggle(hw, pin=3, mode='OUTPUT', label='relay')
    t.write(True)
    t.pulse(duration_ms=200)
    t.toggle()

pytest integration

Add to conftest.py:

pytest_plugins = ["toggletest.pytest_plugin"]
from toggletest import assert_state, assert_changed, assert_no_bounce, assert_transition

def test_power_switch(mock_toggle, mock_backend, toggle_logger):
    toggle_logger.attach(mock_toggle)
    mock_backend.set_state(mock_toggle.pin, True)
    mock_toggle.read()
    assert_state(mock_toggle, True)
    assert_changed(mock_toggle, 1)
    assert_transition(mock_toggle, from_state=False, to_state=True)
    assert_no_bounce(mock_toggle, window_ms=50)

Run without hardware:

pytest --no-hardware

Run with Arduino:

pytest --arduino-port /dev/ttyUSB0 --toggle-pin 2

CLI

toggletest scan
toggletest ping --port /dev/ttyUSB0
toggletest read --port /dev/ttyUSB0 --pin 2
toggletest write --port /dev/ttyUSB0 --pin 3 --state high
toggletest pulse --port /dev/ttyUSB0 --pin 3 --duration-ms 500
toggletest monitor --port /dev/ttyUSB0 --pin 2 --duration 10 --output results.csv
toggletest report  --port /dev/ttyUSB0 --pin 2 --duration 10 --output-csv run.csv

Adding a new backend

from toggletest.backends.base import HardwareBackend

class MyCustomBackend(HardwareBackend):
    def connect(self): ...
    def disconnect(self): ...
    def configure_pin(self, pin, mode): ...
    def read_pin(self, pin) -> bool: ...
    def write_pin(self, pin, state): ...

Project structure


License

MIT

Metadata

Release files for toggletest-hw 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for toggletest-hw 1.0.1
File Size Uploaded
toggletest_hw-1.0.1.tar.gz 8.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for toggletest-hw 1.0.1
File Interpreter ABI Platform
toggletest_hw-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.8 kB

Release files / toggletest_hw-1.0.1.tar.gz

Download URL toggletest_hw-1.0.1.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
fbea35f82a9beb494adba4ede613628d2f18f840f434cc5cfb0c108d239870bc
BLAKE2b-256 checksum
How to use checksums
7a71878333736e9635e3550253024e9642b985474478a3727bc44791eb069ff1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.3

Release files / toggletest_hw-1.0.1-py3-none-any.whl

Download URL toggletest_hw-1.0.1-py3-none-any.whl
Size 8.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
26c496517769af8214f5a355d23257f56261f8d394d3e661ff3e1fab677dc4f8
BLAKE2b-256 checksum
How to use checksums
2d69c2adba0d6c8afe31a6e1166417ef2410e96b54da802351cf81f39ccd10ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.3

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page