Skip to content

Run MicroPython's tests in the simulator - #145

Merged
microbit-matt-hillsdon merged 11 commits into
mainfrom
micropython-test-suite
Nov 11, 2026
Merged

microbit-matt-hillsdon merged 11 commits into
mainfrom
micropython-test-suite

Conversation

@microbit-matt-hillsdon

@microbit-matt-hillsdon microbit-matt-hillsdon commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Runs MicroPython's own tests in the simulator as a new micropython-tests Playwright project. Each test file runs as main.py and its serial output must match the test's .exp file or, where it has none, CPython's output, as MicroPython's run-tests.py does.

This would have caught the miscompiled for loop fixed in #144: 70 of the basics tests crashed on that build. It also found the three bugs fixed here.

What runs

The basics, extmod, float, import, micropython, misc, stress and unicode directories: 838 tests, of which 569 pass, 195 skip themselves by printing SKIP for a missing feature (139 of those in extmod), and 74 are skipped by micropython-tests/skip.json with reasons. Tests in import have the files alongside them uploaded too.

The skip list breaks down as:

  • native code emitters not enabled (39)
  • no packages, as the filesystem has no directories (8)
  • no io module (8)
  • other port configuration: f-strings, reverse operators, warnings, single precision floats, emergency exception buffer, bytearray slice assignment, help() and mem_info() output (14)
  • needs files or paths from the host (3)
  • heap too small to reach the limit under test (1)
  • a documented simulator limitation, writing memory via machine.mem (1)

Expected output

  • micropython-tests/update-expected.py generates the CPython output, which is checked in under micropython-tests/expected, so test runs don't depend on the local Python.
  • MicroPython v1.18's CI used Python 3.8, so the script insists on that version. It is a constant to review when MicroPython is updated.
  • The script records the MicroPython commit, and a test fails if the submodule has moved on without regenerating. The README's upgrade steps cover this.

Simulator fixes

  • Serial output was cut off at the first NUL character in a write, so print('\x00abc') printed nothing. Found by basics/string_strip.
  • C recursion overflowed the browser's call stack before MicroPython's stack check fired, giving RangeError: Maximum call stack size exceeded as an internal error. Recursive generators and printing a container that contains itself did this. The check now stops at 64 KB of the Emscripten stack so they raise RuntimeError. That is still far more permissive than the device, which allows 7,680 bytes. The overflow point was measured in Chrome only. Found by stress/recursive_gen.
  • A program that ended with the heap locked via micropython.heap_lock() left the REPL unable to allocate and exited the runtime. Found by two of the micropython/heapalloc_* tests.

Audio

  • Sound expressions were scheduled one buffer (under 12 ms) ahead, so a brief main-thread stall left a click, usually in the first 50 ms. They are now scheduled 100 ms ahead. An expression counts as active until its scheduled audio has played, and stopping it or starting another silences what is scheduled. This was the cause of the SoundEffect test failing when run in parallel.
  • The audio tests hold their program until the test recorder is running, as under load it missed the start of sounds made straight away.
  • With those fixed, CI uses two Playwright workers.

Other changes

  • The three iteration e2e tests added in Fix firmware crashes seen via the Python Editor #144 are removed as the upstream tests cover them.
  • Each Playwright worker loads the simulator once for the MicroPython tests and flashes it per test, which roughly halves their run time with one worker. A test that times out or hits a simulator error closes it so the next starts clean.
  • The Playwright step's timeout goes from 10 to 20 minutes.

UTF8ToString stops at the first NUL even when given a length, so
print('\x00abc') lost the rest of that write.
Each test from MicroPython's basics, float and unicode directories runs
as main.py, and its serial output is compared with the test's .exp file
or with CPython's output, as MicroPython's run-tests.py does. This
would have caught the miscompiled for loop: 70 of the basics tests
crashed on that build.

The CPython output is generated by update-expected.py and checked in,
so test runs don't depend on the local Python. MicroPython v1.18's CI
used Python 3.8, so the script insists on that. A test fails if the
checked-in output is for a different MicroPython commit.

Tests that don't apply to the port are listed with reasons in skip.ts.

The Playwright step's timeout goes up as the run is longer.
@github-actions

Copy link
Copy Markdown

Comment thread README.md Resolved
MicroPython's stack check measured against the 1 MB Emscripten stack in
linear memory, but C recursion also uses the browser's own call stack,
which ran out first. Recursive generators and printing a container that
contains itself ended with "RangeError: Maximum call stack size
exceeded" as an internal error. In Chrome that happened with about
128 KB of the linear stack in use on the debug firmware and 260 KB on
the release one.

Limit the check to 64 KB so these raise RuntimeError, as on the device.
Python function recursion is unaffected as the Python stack bounds it
first.
A program that finished with the heap locked via micropython.heap_lock()
left the REPL unable to allocate, which reached nlr_jump_fail and exit(1).
After an uncaught exception it also couldn't build the traceback.
Adds the extmod, import, micropython, misc and stress directories. Most
of extmod skips itself as the port lacks those modules, but what runs
exercises the simulator's time and random number support.

Tests in import need the files alongside them, so those are uploaded
too.

The skip list moves to JSON so the generator can read it and not write
expected output for skipped tests. One of those prints an absolute path.
@microbit-matt-hillsdon microbit-matt-hillsdon changed the title Run MicroPython's core language tests in the simulator Run MicroPython's tests in the simulator Nov 11, 2026
Loading the simulator took longer than most of these tests. Flashing
one that is already loaded roughly halves the run with a single worker.
A test that times out or hits a simulator error closes it so the next
starts clean.
Each buffer from the sound expression synth is under 12 ms, and the next
was only requested when one finished, so playback ran one buffer ahead.
Any brief stall on the main thread left a gap of an audio render quantum,
heard as a click, usually in the first 50 ms while the program starts.

Keep 100 ms scheduled. The synth is pure JavaScript so it can be asked
for more straight away. To keep the timing the firmware sees:

- an expression counts as active until its scheduled audio has played,
  not just until the synth has generated it;
- stopping an expression, or starting another, silences what is
  scheduled, as does stopping the board for all buffered audio.
The recorder runs on the main thread, which is busy while the simulator
starts. Under load it missed the start of sounds made straight away, so
music.pitch occasionally measured short.
The audio tests no longer fail when run alongside others.

@microbit-robert microbit-robert left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@microbit-matt-hillsdon
microbit-matt-hillsdon merged commit 4369765 into main Nov 11, 2026
1 check passed
@microbit-matt-hillsdon
microbit-matt-hillsdon deleted the micropython-test-suite branch November 11, 2026 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants