A pytest plugin to test Arduino projects using pytest-embedded and arduino-cli
Project description
pytest-embedded-arduino-cli
A pytest plugin to test Arduino projects using pytest-embedded and arduino-cli.
Overview
pytest-embedded-arduino-cli is a small plugin that keeps pytest-embedded's generic DUT / serial / expect flow and replaces Arduino-specific build and upload with arduino-cli.
This package does not depend on pytest-embedded-arduino. It is intended to stay generic enough to work well for Arduino projects beyond ESP32-specific assumptions.
Design
- Build with
arduino-cli compile - Upload with
arduino-cli upload - Use
pytest-embeddedas the runtime foundation - Avoid
EspSerialand ESP-specific flashing services - Resolve sketch settings from
sketch.yamland--profile - Treat the test file directory as the sketch directory
Setup
uv init
uv add pytest-embedded-arduino-cli
uv sync
Runtime dependencies include:
pytestpytest-embeddedpytest-embedded-serialPyYAML
Requirements
arduino-cliavailable inPATH- Installed Arduino board core(s)
- A serial port accessible from the host when running hardware tests
When sketch.yaml declares platform or library versions, Arduino CLI resolves them through its local package and library indexes.
The indexes do not need to be refreshed on every test run, but they should be updated periodically or before CI/release verification.
If a build fails because a declared platform or library version cannot be found, try:
arduino-cli core update-index
arduino-cli lib update-index
Project Layout
The expected layout is one sketch directory per test app.
tests/
my_app/
sketch.yaml
my_app.ino
test_my_app.py
When pytest runs a specific .py file, this plugin treats that file's directory as the sketch directory. Build settings are resolved from the nearest sketch.yaml.
Usage
Build, upload, and run tests:
uv run pytest tests/my_app --port=/dev/ttyACM0
Select an Arduino CLI profile from sketch.yaml:
uv run pytest tests/my_app --profile esp32s3 --port=/dev/ttyACM0
Build only:
uv run pytest tests/my_app --run-mode=build
Force a clean Arduino CLI compile:
uv run pytest tests/my_app --clean
Upload and test against an already-built image:
uv run pytest tests/my_app --run-mode=test --port=/dev/ttyACM0
--run-mode=test skips compile, reuses the existing build output, uploads it, and then runs the test.
Run this package's own tests:
uv run pytest
Main Options
--run-mode=all|build|test--profile--peer-profile=NAME:PROFILE--peer-port=NAME:PORT--clean--arduino-test-timeout=SECONDS--arduino-test-artifact-dir=PATH--arduino-test-missing-config=skip|error
--clean passes --clean to arduino-cli compile.
It is useful when Arduino CLI's incremental build cache should be ignored.
It also removes the ArduTest artifact directory before running.
--arduino-test-artifact-dir selects the ArduTest artifact root.
The default is ardutest, resolved relative to pytest's rootdir.
The directory is created only when an artifact is saved.
--arduino-test-missing-config controls required ArduTest config that is not provided.
The default is skip; use error when missing config should fail the pytest run.
Use pytest-embedded standard options for runtime control, such as:
--port--flash-port--baud--embedded-services
pytest-embedded-serial is installed as a normal dependency so hardware tests can use the serial service without extra package installation.
If --embedded-services is not specified, this plugin enables serial by default.
For profile-specific serial ports, the plugin resolves ports in this order:
--flash-port--portTEST_SERIAL_PORT_<PROFILE>TEST_SERIAL_PORTprofiles.<PROFILE>.portinsketch.yaml, only when it is asocket://...URL
Because of how pytest parses arguments, options that take path-like values such as --port and --flash-port are safer when written with =, for example --port=/dev/ttyUSB0.
Depending on the environment, uv run pytest --port /dev/ttyUSB0 may cause that path to be interpreted as another base path.
If needed, uv run pytest --rootdir . --port /dev/ttyUSB0 is also a valid workaround.
For targets that run on the host machine and expose the DUT over TCP/IP, use the URL format supported by pytest-embedded-serial / pyserial.
If the selected sketch.yaml profile defines port: socket://localhost, --port=socket://localhost can be omitted.
uv run pytest tests/my_app --profile host
When the port number is specified, such as socket://localhost:56789, the DUT connects to that socket directly.
When the port number is omitted, such as socket://localhost, the plugin is expected to read port from *.host-arduino.json generated under the build output directory and then connect to socket://localhost:<port>.
This resolution should prefer the host-arduino information file instead of capturing upload stdout.
{
"pid": 21228,
"port": 56789
}
Host execution is an early, lightweight test path for pure logic and serial protocol checks without physical hardware.
Results may differ depending on the host OS, gcc or other toolchain versions, and the Serial class implementation provided by the host Arduino core, so this does not guarantee behavior on real hardware.
Use real hardware for peripherals, timing, interrupts, memory layout, Flash/NVS, and board-specific APIs.
Build success can also differ by board core and platform, so running build tests with the production board profile is still recommended.
For socket://... ports, this plugin batches serial reads to avoid the very slow one-byte-at-a-time redirect behavior that can otherwise appear with host Arduino cores.
Example:
export TEST_SERIAL_PORT_ESP32S3=/dev/ttyUSB1
uv run pytest tests/my_app --profile esp32s3
Profile resolution works as follows:
- If
--profileis specified, that profile is used - Otherwise, if
sketch.yamldefinesdefault_profile, that profile is used - Otherwise, if
sketch.yamlhas exactly one profile, it is selected automatically - Otherwise, pytest exits with an error because the profile is ambiguous
In practice, explicitly specifying --profile is recommended.
If you do not want to pass --profile, define default_profile in sketch.yaml.
The single-profile auto-selection is supported as a fallback, but it is better not to rely on it for regular project configuration.
Peer DUTs
Tests that need additional DUTs can place peer_<name>/ sketch directories next to the primary sketch.
The primary sketch remains available as dut; peer sketches are available through the peers fixture.
tests/
my_app/
sketch.yaml
my_app.ino
test_my_app.py
peer_echo/
sketch.yaml
peer_echo.ino
def test_with_peer(dut, peers):
echo = peers["echo"]
dut.expect_exact("MAIN_READY")
echo.expect_exact("ECHO_READY")
Peer DUTs are prepared only for tests that request the peers fixture.
If a test uses only dut, peer_* directories are ignored for that test.
Requesting peers enables all detected peer DUTs; peers["<name>"] is the mapping API for accessing the connected peer.
Startup order is fixed:
- the primary DUT is built and uploaded first
- peer DUTs are built and uploaded later, in peer name order
- peer DUTs are connected and exposed through
peers - the primary DUT is connected and exposed as
dut
On real serial hardware, short boot-time messages can be missed if a sketch prints them immediately after reset or upload. Host Arduino core socket runs often keep enough output for this not to matter, but hardware tests should use a startup delay, repeated READY message, or an explicit handshake from Python before relying on early output.
sketch.yaml is not extended for peer configuration.
Each peer directory is a normal sketch directory with its own .ino and sketch.yaml.
Peer profiles are resolved in this order:
--peer-profile <name>:<profile>default_profileinpeer_<name>/sketch.yaml- Skip the peer test if no profile is resolved
--profile is for the primary DUT only and is not inherited by peer DUTs.
Peer DUTs also do not use single-profile auto-selection.
If a peer should run without --peer-profile, define default_profile in that peer's sketch.yaml.
Peer ports are resolved in this order:
--peer-port <name>:<port>TEST_SERIAL_PORT_PEER_<NAME>_<PROFILE>TEST_SERIAL_PORT_PEER_<NAME>profiles.<PROFILE>.portin the peersketch.yaml, only when it is asocket://...URL- Skip the peer test if no port is resolved
--peer-profile and --peer-port may be specified multiple times.
Use one option per peer instead of comma-separated values.
uv run pytest tests/my_app \
--profile esp32 \
--peer-profile echo:host \
--peer-port echo:socket://localhost
For compile-time defines, place a build_config.toml in the sketch directory:
[defines]
TEST_WIFI_SSID = "WIFI_SSID"
TEST_WIFI_PASSWORD = "WIFI_PASSWORD"
[flags]
PYTEST_BUILD = true
ENABLE_TEST_HOOKS = true
In [defines], the left side is the environment variable name and the right side is the C/C++ define name.
For example, TEST_WIFI_SSID becomes -DWIFI_SSID="..." at compile time.
[flags] is for value-less defines.
Only true entries are passed, such as -DPYTEST_BUILD; false entries are omitted.
Set values before running pytest:
export TEST_WIFI_SSID=my-ssid
export TEST_WIFI_PASSWORD=my-password
uv run pytest tests/my_app --port=/dev/ttyACM0
You can also load these values from a dotenv file through uv run.
--env-file is a uv option, so put it before pytest:
uv run --env-file .env pytest tests/my_app --port=/dev/ttyACM0
If an environment variable is missing, the plugin still passes the define with an empty string value.
This allows the test or sketch code to decide how to handle missing settings.
The plugin does not add test flags such as PYTEST_BUILD automatically.
Projects that need them should declare them explicitly under [flags].
For command visibility, follow pytest's standard verbosity:
-vshows thearduino-cli compile/arduino-cli uploadcommand line-vvalso shows execution context such ascwd,sketch_dir,build_path,profile, andport
ArduTest Fixture
This package includes an experimental arduino_test fixture for sketches that use the separate Arduino-side ArduTest library.
ArduTest is expected to be declared by the sketch's sketch.yaml, with the library version pinned there for reproducible tests.
Detailed usage examples will be added under examples/ after the API and protocol settle.
def test_board(arduino_test):
arduino_test.run()
arduino_test.run() fails the pytest test automatically when ArduTest reports a failed or error result.
Use additional assertions only when you want to check collected logs, metrics, artifacts, or metadata.
The current fixture speaks ArduTest protocol version 1.
Use fixture methods for fixed test-local ArduTest values:
def test_sample_rate(arduino_test):
arduino_test.set_capability("measurement.current")
arduino_test.set_config("sample_rate", 1000)
arduino_test.run("test_sample_rate")
Use environment variables, .env, or CI variables for values that depend on the machine, board, lab setup, or secrets.
Example
def test_hello(dut):
dut.expect_exact("hello from arduino")
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("hello from arduino");
}
void loop() {}
Additional samples:
examples/01_basic- Minimal hello-world example
- Uses
esp32as the default profile and also supportsuno - Includes port resolution from
TEST_SERIAL_PORTandTEST_SERIAL_PORT_<PROFILE>
examples/02_env_define- Demonstrates compile-time defines from environment variables
- Uses Wi-Fi on ESP32-class targets to explain
build_config.toml
examples/03_dut_input- Demonstrates runtime input over serial through
dut.write(...) - Works on both
esp32anduno
- Demonstrates runtime input over serial through
examples/04_unity_basic- Demonstrates a minimal Unity-based test sketch for ESP32
examples/05_nvs_persistent- Demonstrates that ESP32
Preferences/ NVS data remains by default - Unsupported profiles are skipped before build because the example is specifically about ESP32 persistence
- Demonstrates that ESP32
examples/06_erase_flash- Demonstrates
EraseFlash=allfor resetting ESP32 persistent data before upload - Pairs with
05_nvs_persistent
- Demonstrates
examples/07_arduino_library_project- Demonstrates a practical Arduino library project with
tests/as theuvroot - Includes
run_wsl.shas a practical test workspace example
- Demonstrates a practical Arduino library project with
examples/08_arduino_ide_project- Demonstrates an Arduino IDE style sketch project with
tests/as theuvroot - Uses thin wrapper
#includefiles so the runner can reference sketch-side code that is not separated as a library
- Demonstrates an Arduino IDE style sketch project with
examples/09_host_arduino_core- Demonstrates a board core that builds and runs the Arduino sketch on the host machine
- Uses
port: socket://localhostinsketch.yamlto connect to the TCP/IP endpoint opened by the host executable - Useful for simple pure-logic and serial-protocol checks, not a replacement for real hardware tests or build tests with the real board profile
examples/10_build_flags- Demonstrates value-less compile-time defines with
[flags]inbuild_config.toml - Shows how a project can explicitly enable test flags such as
PYTEST_BUILD
- Demonstrates value-less compile-time defines with
examples/11_ardutest- Minimal examples for using the experimental
arduino_testfixture with the ArduTest Arduino library - Detailed protocol/API and artifact-saving tests live in the ArduTest test suite: https://github.com/tanakamasayuki/ArduTest/tree/main/tests
- Minimal examples for using the experimental
Execution guidance for examples/ is described in examples/README.md.
Warnings
You may see PytestExperimentalApiWarning: record_xml_attribute is an experimental feature.
This warning comes from pytest-embedded, not from this plugin. It is usually safe to ignore.
If you want to suppress it in your project, add a warning filter in pytest.ini, pyproject.toml, or a local config such as examples/pytest.ini.
What This Plugin Does Not Try To Be
- A drop-in replacement for
pytest-embedded-arduino - An ESP-specific flashing layer
- A board auto-discovery tool
Future Extensions
- Board-family-specific upload strategies
- Smarter artifact discovery
- Serial reset / monitor helpers
- TCP/IP connection helpers for host Arduino cores
- Multi-device support
- Optional
fqbnor sketch path overrides
Release
This repository uses GitHub Actions for releases.
Before triggering a release:
- Update the
## Unreleasedsection inCHANGELOG.md - Make sure
uv run pytest testspasses locally if needed
Release flow:
- Open GitHub Actions
- Run the
Releaseworkflow manually - Enter the release version such as
0.1.0 - Choose whether to publish to PyPI
The workflow will:
- Update versions in
pyproject.tomlandsrc/pytest_embedded_arduino_cli/__init__.py - Move
CHANGELOG.mdunreleased entries into## <version> - Run tests and build the package
- Commit the release changes and create tag
v<version> - Create a GitHub Release
- Publish to PyPI when enabled
PyPI publishing is configured for Trusted Publishing via GitHub Actions.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_embedded_arduino_cli-1.1.4.tar.gz.
File metadata
- Download URL: pytest_embedded_arduino_cli-1.1.4.tar.gz
- Upload date:
- Size: 78.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e8fe8efcece4f69c1e4b855d1de7710b706d499a2c59008248d56983201dfd7d
|
|
| MD5 |
cec235b681872b8d809ed01ac9564061
|
|
| BLAKE2b-256 |
07fafe2565c816d194c3a2b0db12bec07faca38869a986855aaf751f608ba3fd
|
Provenance
The following attestation bundles were made for pytest_embedded_arduino_cli-1.1.4.tar.gz:
Publisher:
release.yml on tanakamasayuki/pytest-embedded-arduino-cli
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_embedded_arduino_cli-1.1.4.tar.gz -
Subject digest:
e8fe8efcece4f69c1e4b855d1de7710b706d499a2c59008248d56983201dfd7d - Sigstore transparency entry: 1470967188
- Sigstore integration time:
-
Permalink:
tanakamasayuki/pytest-embedded-arduino-cli@9d4539fefe2ff8dd7bbd21f5ef4db9fb11663edb -
Branch / Tag:
refs/heads/main - Owner: https://github.com/tanakamasayuki
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@9d4539fefe2ff8dd7bbd21f5ef4db9fb11663edb -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file pytest_embedded_arduino_cli-1.1.4-py3-none-any.whl.
File metadata
- Download URL: pytest_embedded_arduino_cli-1.1.4-py3-none-any.whl
- Upload date:
- Size: 23.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02b1b0f1d5dd0048115ad82582934a4e3c5f5367150de73b74197e917be3d00a
|
|
| MD5 |
0113fd68aa321002a2886060d8c33782
|
|
| BLAKE2b-256 |
b162f49b081f8e17edc64b4b0bd985bf69a7fa5d78d128c52f184154365ba318
|
Provenance
The following attestation bundles were made for pytest_embedded_arduino_cli-1.1.4-py3-none-any.whl:
Publisher:
release.yml on tanakamasayuki/pytest-embedded-arduino-cli
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pytest_embedded_arduino_cli-1.1.4-py3-none-any.whl -
Subject digest:
02b1b0f1d5dd0048115ad82582934a4e3c5f5367150de73b74197e917be3d00a - Sigstore transparency entry: 1470967484
- Sigstore integration time:
-
Permalink:
tanakamasayuki/pytest-embedded-arduino-cli@9d4539fefe2ff8dd7bbd21f5ef4db9fb11663edb -
Branch / Tag:
refs/heads/main - Owner: https://github.com/tanakamasayuki
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@9d4539fefe2ff8dd7bbd21f5ef4db9fb11663edb -
Trigger Event:
workflow_dispatch
-
Statement type: