Skip to content

Commit 2fa66b5

Browse files
test: replace subprocess calls with run_example_module helper (#7)
1 parent 01d59f3 commit 2fa66b5

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

tests/test_hello_esp32.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,11 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5-
import os
6-
import subprocess
7-
import sys
5+
from .utils import run_example_module
86

97

108
def test_hello_esp32_example() -> None:
11-
"""`python -m examples.hello_esp32.main` runs the hello_esp32 example and exits with 0."""
12-
13-
assert os.environ.get("WOKWI_CLI_TOKEN") is not None, (
14-
"WOKWI_CLI_TOKEN environment variable is not set. You can get it from https://wokwi.com/dashboard/ci."
15-
)
16-
17-
result = subprocess.run(
18-
[sys.executable, "-m", "examples.hello_esp32.main"],
19-
check=False,
20-
capture_output=True,
21-
text=True,
22-
env={**os.environ, "WOKWI_SLEEP_TIME": "1"},
23-
)
24-
9+
"""Async hello_esp32 example should run and exit with 0."""
10+
result = run_example_module("examples.hello_esp32.main")
2511
assert result.returncode == 0
2612
assert "main_task: Calling app_main()" in result.stdout

tests/test_micropython_esp32.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# SPDX-FileCopyrightText: 2025-present CodeMagic LTD
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
from .utils import run_example_module
6+
7+
8+
def test_micropython_esp32_example() -> None:
9+
"""MicroPython ESP32 example should run and print MicroPython banner."""
10+
# MicroPython boot can take a bit longer; give it a few seconds
11+
result = run_example_module("examples.micropython_esp32.main", sleep_time="3")
12+
assert result.returncode == 0
13+
# Expect a line from the injected MicroPython script
14+
assert "Hello, MicroPython! I'm running on a Wokwi ESP32 simulator." in result.stdout

tests/utils.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Test utilities for running example modules.
3+
4+
Provides a helper to execute `python -m <module>` with a short sleep to keep
5+
CI fast and shared environment handling (WOKWI_CLI_TOKEN, etc.).
6+
"""
7+
8+
from __future__ import annotations
9+
10+
import os
11+
import subprocess
12+
import sys
13+
from collections.abc import Mapping
14+
from subprocess import CompletedProcess
15+
16+
17+
def run_example_module(
18+
module: str, *, sleep_time: str = "1", extra_env: Mapping[str, str] | None = None
19+
) -> CompletedProcess[str]:
20+
"""Run an example module with a short simulation time.
21+
22+
Requires WOKWI_CLI_TOKEN to be set in the environment.
23+
Returns the CompletedProcess so tests can assert on return code and output.
24+
"""
25+
26+
assert os.environ.get("WOKWI_CLI_TOKEN") is not None, (
27+
"WOKWI_CLI_TOKEN environment variable is not set. You can get it from https://wokwi.com/dashboard/ci."
28+
)
29+
30+
env = {**os.environ, "WOKWI_SLEEP_TIME": sleep_time}
31+
if extra_env:
32+
env.update(extra_env)
33+
34+
return subprocess.run(
35+
[sys.executable, "-m", module],
36+
check=False,
37+
capture_output=True,
38+
text=True,
39+
env=env,
40+
)

0 commit comments

Comments
 (0)