Skip to content
20 changes: 3 additions & 17 deletions tests/test_hello_esp32.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,11 @@
#
# SPDX-License-Identifier: MIT

import os
import subprocess
import sys
from .utils import run_example_module


def test_hello_esp32_example() -> None:
"""`python -m examples.hello_esp32.main` runs the hello_esp32 example and exits with 0."""

assert os.environ.get("WOKWI_CLI_TOKEN") is not None, (
"WOKWI_CLI_TOKEN environment variable is not set. You can get it from https://wokwi.com/dashboard/ci."
)

result = subprocess.run(
[sys.executable, "-m", "examples.hello_esp32.main"],
check=False,
capture_output=True,
text=True,
env={**os.environ, "WOKWI_SLEEP_TIME": "1"},
)

"""Async hello_esp32 example should run and exit with 0."""
result = run_example_module("examples.hello_esp32.main")
assert result.returncode == 0
assert "main_task: Calling app_main()" in result.stdout
14 changes: 14 additions & 0 deletions tests/test_micropython_esp32.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# SPDX-FileCopyrightText: 2025-present CodeMagic LTD
#
# SPDX-License-Identifier: MIT

from .utils import run_example_module


def test_micropython_esp32_example() -> None:
"""MicroPython ESP32 example should run and print MicroPython banner."""
# MicroPython boot can take a bit longer; give it a few seconds
result = run_example_module("examples.micropython_esp32.main", sleep_time="3")
assert result.returncode == 0
# Expect a line from the injected MicroPython script
assert "Hello, MicroPython! I'm running on a Wokwi ESP32 simulator." in result.stdout
40 changes: 40 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Test utilities for running example modules.

Provides a helper to execute `python -m <module>` with a short sleep to keep
CI fast and shared environment handling (WOKWI_CLI_TOKEN, etc.).
"""

from __future__ import annotations

import os
import subprocess
import sys
from collections.abc import Mapping
from subprocess import CompletedProcess


def run_example_module(
module: str, *, sleep_time: str = "1", extra_env: Mapping[str, str] | None = None
) -> CompletedProcess[str]:
"""Run an example module with a short simulation time.

Requires WOKWI_CLI_TOKEN to be set in the environment.
Returns the CompletedProcess so tests can assert on return code and output.
"""

assert os.environ.get("WOKWI_CLI_TOKEN") is not None, (
"WOKWI_CLI_TOKEN environment variable is not set. You can get it from https://wokwi.com/dashboard/ci."
)

env = {**os.environ, "WOKWI_SLEEP_TIME": sleep_time}
if extra_env:
env.update(extra_env)

return subprocess.run(
[sys.executable, "-m", module],
check=False,
capture_output=True,
text=True,
env=env,
)