Mock MicroPython libraries for CPython development and testing.
Project description
mockro
Mock MicroPython libraries for CPython development and testing.
mockro lets students write code that looks exactly like real MicroPython —
import machine, import network, and so on — while running and testing it on a
normal computer without any hardware attached.
Quick start
Install with uv:
uv add --dev mockro
Or with pip:
pip install mockro
Run a script with mocks
Once mockro is installed in the environment, plain Python invocations work:
uv run python main.py
# or
python main.py
You can also use the explicit wrappers:
mockro run main.py
# or
python -m mockro main.py
Run tests with mocks
mockro registers a pytest plugin. Just run:
pytest
and import machine will work in your tests and the code they import.
Note: by default
mockroonly installs MicroPython-specific module names such asmachine,network, andusocket. It does not shadow CPython stdlib modules likesocket,time,os,json, orasyncio, because those are needed by pytest and other tooling. Useusocket,utime,uos,ujson, anduasyncioin testable code, or run the code throughmockro run/python -m mockroto get the non-ualiases as well.
Override behavior for assignments
In conftest.py or test fixtures:
import mockro
mockro.patch("machine.Pin.value", return_value=1)
mockro.patch("network.WLAN.isconnected", return_value=True)
Or temporarily:
with mockro.override(machine_Pin_value=1):
...
Create a new assignment
mockro init my_assignment
This scaffolds a project with pyproject.toml, conftest.py, starter source,
tests, and .pyi stubs so editors know the mocked MicroPython APIs.
Mocked modules
mockro includes mocks for the full MicroPython standard library plus
common port-specific modules:
machine,network,bluetooth,framebuf,gc,micropythonusocket/socket,utime/time,uos/os,usys/sysujson/json,ubinascii/binascii,uhashlib/hashlibuerrno/errno,uheapq/heapq,uasyncio/asynciouctypes/ctypes,ucollections/collections,ustruct/structuselect/select,uzlib/zlibneopixel,dht,onewire,ds18x20esp,esp32,rp2,pyb,samd,zephyr
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mockro-0.1.0.tar.gz.
File metadata
- Download URL: mockro-0.1.0.tar.gz
- Upload date:
- Size: 35.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f37864954e30b55a6029ca6ee75a2374adc1e7289e87970880281766157677fb
|
|
| MD5 |
d06e94bcde0d9648fc950b0e1f11325d
|
|
| BLAKE2b-256 |
8e6bef648000484cd2484ba3fa69506bbb3f9b657de1c3c3915b2249a2ba3847
|
File details
Details for the file mockro-0.1.0-py3-none-any.whl.
File metadata
- Download URL: mockro-0.1.0-py3-none-any.whl
- Upload date:
- Size: 58.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/7.0.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bcbd08ea0ecfbea1a6587b16da661b7d0b12ee3fda14b73322f4d0595bd3a31f
|
|
| MD5 |
e8ad593b7c0f26cddabcb94520d886ba
|
|
| BLAKE2b-256 |
1ddabf66811ee4827ef9bf0e9173641eb0842e4b1b08dda80077dd24376ebb27
|