Skip to main content

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 mockro only installs MicroPython-specific module names such as machine, network, and usocket. It does not shadow CPython stdlib modules like socket, time, os, json, or asyncio, because those are needed by pytest and other tooling. Use usocket, utime, uos, ujson, and uasyncio in testable code, or run the code through mockro run / python -m mockro to get the non-u aliases 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, micropython
  • usocket / socket, utime / time, uos / os, usys / sys
  • ujson / json, ubinascii / binascii, uhashlib / hashlib
  • uerrno / errno, uheapq / heapq, uasyncio / asyncio
  • uctypes / ctypes, ucollections / collections, ustruct / struct
  • uselect / select, uzlib / zlib
  • neopixel, dht, onewire, ds18x20
  • esp, esp32, rp2, pyb, samd, zephyr

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mockro-0.1.0.tar.gz (35.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mockro-0.1.0-py3-none-any.whl (58.8 kB view details)

Uploaded Python 3

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

Hashes for mockro-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f37864954e30b55a6029ca6ee75a2374adc1e7289e87970880281766157677fb
MD5 d06e94bcde0d9648fc950b0e1f11325d
BLAKE2b-256 8e6bef648000484cd2484ba3fa69506bbb3f9b657de1c3c3915b2249a2ba3847

See more details on using hashes here.

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

Hashes for mockro-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bcbd08ea0ecfbea1a6587b16da661b7d0b12ee3fda14b73322f4d0595bd3a31f
MD5 e8ad593b7c0f26cddabcb94520d886ba
BLAKE2b-256 1ddabf66811ee4827ef9bf0e9173641eb0842e4b1b08dda80077dd24376ebb27

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page