Skip to main content

Universal Python library for hardware toggle switch testing automation

Project description

Python License Platform Status

toggletest

Universal Python library for hardware toggle switch testing automation.

Supports Arduino (USB-serial), Raspberry Pi (GPIO), 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

toggletest_hw-1.0.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

toggletest_hw-1.0.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file toggletest_hw-1.0.0.tar.gz.

File metadata

  • Download URL: toggletest_hw-1.0.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for toggletest_hw-1.0.0.tar.gz
Algorithm Hash digest
SHA256 47e2f5a1dc778a6849d598b534fe1a924aab7a2fbb7a77cde8bcead11b8026cb
MD5 a84bdd3bd7486f8fbe2b04d3da7a86be
BLAKE2b-256 e4494a6bd4628f5eed43dcb9c386bc742f75e9088feaedd2b1fe482999757ac7

See more details on using hashes here.

File details

Details for the file toggletest_hw-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: toggletest_hw-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for toggletest_hw-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f621174d7bf351d5251566ad468d1b795fb25614103a83ab086caa20e38941f0
MD5 ca60ea40e499c8f360d04643937f6405
BLAKE2b-256 79ee225edf51cf1db209237332ea9c7223b818f567fd78d164c47e8b0c953841

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page