pytest-embedded-serial-esp
pytest-embedded-serial-esp is a pytest plugin that extends the pytest-embedded framework to work specifically with Espressif target boards via serial communication. As part of the broader pytest-embedded ecosystem, it enables automated testing, flashing, and interaction with ESP32, ESP8266, and other Espressif SoCs. The library is actively maintained by Espressif Systems, with version 2.7.0 being the latest, and undergoes frequent updates, often including new features and bug fixes.
Common errors
-
error: invalid command 'bdist_wheel'
cause The `wheel` package is not installed or pip is outdated, preventing the creation of a wheel distribution.fixRun `python -m pip install -U pip` to upgrade pip, then `python -m pip install wheel` to install the wheel package. -
Package requirements (dbus-1 >= 1.8) were not met: No package 'dbus-1' found
cause Missing system-level D-Bus development libraries, common on Linux distributions.fixOn Ubuntu/Debian, install with `sudo apt-get install libdbus-glib-1-dev` or `sudo apt-get install libdbus-1-dev`. For other distributions, consult their package manager. -
AttributeError: 'EspSerial' object has no attribute 'stub'
cause Attempting to access the deprecated `stub` property on the `EspSerial` object after upgrading to `pytest-embedded` v2.x.fixReplace `dut.stub` with `dut.esp` in your test code. -
TypeError: use_esptool() got an unexpected keyword argument 'no_stub'
cause Using the `no_stub` parameter with the `use_esptool()` decorator, which was removed in `pytest-embedded` v2.x.fixRemove the `no_stub` parameter from your `use_esptool()` decorator calls.
Warnings
- breaking pytest-embedded v2.0.0 (which pytest-embedded-serial-esp v2.x depends on) dropped support for Python 3.7, 3.8, and 3.9. It now requires Python 3.10+.
- breaking With pytest-embedded v2.0.0, the `esptool` requirement was updated to `>=5.1.dev1,<6`. Also, the `EsptoolArgs` class was removed from `pytest-embedded-serial-esp`, and the `stub` property on `EspSerial` was deprecated in favor of `esp`. Additionally, `hard_reset_after` and `no_stub` parameters were removed from the `use_esptool()` decorator.
- gotcha When using `dut.expect()` or `dut.expect_exact()`, be mindful of regular expression special characters (e.g., parentheses, square brackets) in your expected strings, as `expect()` uses regex by default. Use `dut.expect_exact()` for literal string matching.
- gotcha The `dut.write()` function might not work as expected when the target application's console is configured to use `ESP_CONSOLE_USB_SERIAL_JTAG` instead of UART. `dut.expect()` generally works regardless of the console interface.
Install
-
pip install pytest-embedded-serial-esp -
pip install -U pytest-embedded~=2.0 pytest-embedded-serial-esp
Imports
- EspSerial
from pytest_embedded_serial_esp.serial import EspSerial
- Dut
from pytest_embedded import Dut
Quickstart
import pytest
from pytest_embedded import Dut
@pytest.mark.parametrize('embedded_services', ['serial', 'esp'], indirect=True)
def test_basic_expect(dut: Dut):
# Example: wait for a specific output from the ESP device
# The 'serial' service provides basic serial communication.
# The 'esp' service enables esptool-specific functionalities like auto-flashing.
dut.expect_exact('Hello world!', timeout=10)
print(f"Device output: {dut.before.decode()}")
# Additional interaction, e.g., sending commands or expecting more output
# dut.write('command\n')
# dut.expect('response')