pytest-embedded-serial: Serial Port Testing for Pytest Embedded

2.7.0 · active · verified Thu Apr 16

pytest-embedded-serial is a pytest plugin that extends the core pytest-embedded framework, enabling robust testing of embedded devices over serial communication. It provides fixtures and functionalities to interact with serial ports, facilitating automated tests for embedded systems. Currently at version 2.7.0, it is part of the pytest-embedded ecosystem maintained by Espressif Systems, which typically follows a regular release cadence with semantic versioning.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to write a basic pytest-embedded test that utilizes serial communication. It uses the `dut` fixture, which is automatically enhanced by `pytest-embedded-serial` when installed. The example shows writing commands to the device under test (DUT) and expecting specific responses using `dut.write()` and `dut.expect_exact()`. The `serial` service is often automatically enabled when the package is installed, or can be explicitly enabled via `pytest --embedded-services serial` CLI option.

import pytest
from pytest_embedded import Dut
import os

# Assuming a device is connected via serial port
# You can specify the port via CLI: pytest --port /dev/ttyUSB0 (or COMx on Windows)
# Or set it via an environment variable or pytest.ini

def test_serial_communication(dut: Dut):
    # Ensure the 'serial' service is enabled, e.g., via CLI: pytest --embedded-services serial
    # or by default if pytest-embedded-serial is installed.

    # Example: Write data to the serial port and expect a response
    # Replace with actual device interaction logic
    dut.write('hello device')
    dut.expect_exact('echo: hello device', timeout=5)
    print("Device responded to 'hello device'.")

    # Accessing underlying serial object (for advanced use cases)
    # The dut.serial object is an instance of Serial from pytest_embedded_serial.serial
    # It's generally preferred to use dut.write and dut.expect for standard interactions.
    # serial_port = dut.serial
    # print(f"Connected to serial port: {serial_port.port} at baudrate: {serial_port.baud}")

    dut.write('get status')
    dut.expect(r'Status: (\w+)', timeout=5)
    status = dut.match.group(1).decode('utf-8')
    print(f"Device status: {status}")

    assert status == 'OK'

view raw JSON →