pytest-embedded-idf Integration for ESP-IDF

2.7.0 · active · verified Fri Apr 17

pytest-embedded-idf is a pytest plugin that extends pytest-embedded to provide a robust testing framework specifically designed for ESP-IDF based firmware. It streamlines the process of building, flashing, and testing applications on Espressif hardware, integrating directly with `idf.py` tools. The current version is 2.7.0, and it follows the release cadence of `pytest-embedded` and ESP-IDF updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic `pytest-embedded-idf` test. It uses the `IdfDut` fixture to interact with an ESP-IDF device. The test asserts that the device outputs 'Hello world!'. Ensure your ESP-IDF environment (`IDF_PATH`) is set up and an ESP device is connected. Run with `pytest` from your ESP-IDF project directory.

# Save this as 'test_hello.py' in your ESP-IDF project directory (e.g., esp-idf/examples/get-started/hello_world)
# Requirements:
# 1. An ESP-IDF project directory.
# 2. ESP-IDF environment variable IDF_PATH correctly set.
# 3. A compatible Espressif development board connected via USB.
# 4. pip install pytest pytest-embedded pytest-embedded-idf

import pytest
from pytest_embedded_idf.dut import IdfDut
import os

# Example: Mark the test to run on an ESP32 target.
# You can change this to esp32s2, esp32s3, esp32c3, esp32c6, etc.,
# depending on your target and --target option if specified.
@pytest.mark.esp32
def test_hello_world_output(dut: IdfDut):
    """
    Tests that the ESP-IDF 'hello_world' example prints the expected output.
    The 'dut' fixture automatically handles building, flashing, and connecting.
    """
    print(f"\nConnected DUT target: {dut.target}")
    print(f"DUT COM Port: {dut.serial.port}")
    print(f"Application path: {dut.app.path}")

    # Expect 'Hello world!' from the device's serial output
    dut.expect(r'Hello world!')
    print("Successfully received 'Hello world!' from DUT.")

    # Example: Send a command (if your application supports it)
    # dut.write('get_heap')
    # dut.expect(r'Free heap: \d+ bytes')

view raw JSON →