{{ message }}
unittest: optimise for code size and memory use#1125
Open
dpgeorge wants to merge 4 commits into
Open
Conversation
Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Damien George <damien@micropython.org>
Note that using `if not expr: raise AssertionError(msg)` is pretty much equivalent in terms of bytecode to `assert expr, msg`. But the use of `raise AssertionError` is clearer and means that the code can now be compiled with -O3 and still function. Signed-off-by: Damien George <damien@micropython.org>
Member
Author
This was referenced Jun 25, 2026
projectgus
approved these changes
Jun 25, 2026
projectgus
left a comment
Contributor
There was a problem hiding this comment.
Changes look simple to me, that's great it reduces footprint significantly! Should churn the heap a lot less with the string formatting moved into the failure path, too.
Member
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Based on discussion and testing in micropython/micropython#16311, this PR applies some optimisations to
unittestto reduce its bytecode size, and reduce the amount of RAM it uses at runtime.msgif neededThe use of
raise AssertionErrorinstead ofassertalso means the module can now be compiled with -O3 and still function correctly (-O3 disables assert expressions!).Testing
Tested on ADAFRUIT_ITSYBITSY_M0_EXPRESS which has about 20k heap memory.
Prior to this change:
__init__.mpyhas size 5795 bytesimport unittestcosts 9648 bytes of heap RAM (measured viag.collect(); micropython.mem_info()before and after import)With this change:
__init__.pymhas size 5516 bytes (-279 byte change)import unittestcosts 9440 bytes (-208 byte change)Also, the current version of
unittestunconditionally does string formatting for the error message for each call toself.assert<Condition>(...), which is really expensive in terms of RAM use and computation. The changes here improve the situation so the string formatting ofmsgis only done if the assert fails. That should free up a lot of RAM during the execution.With these changes, ADAFRUIT_ITSYBITSY_M0_EXPRESS can successfully run the tests modified/added in micropython/micropython#16311, they now all pass or skip correctly.
Trade-offs and Alternatives
This module is no longer runnable under CPython due to removal of
tracebackmodule support. But IMO it's too restrictive to get this working under CPython, we need to be able to optimise it for running under MicroPython. The unittest test suite which runs under CI here anyway tests the behaviour, CPython is not needed for that.Generative AI
Not used.