Unit & Integration Testing with Pytest #17527
Replies: 1 comment 1 reply
-
I too have an interest in automated testing with micropython, thanks for sharing! I find myself using the test runners in particular the multi-test system from micropython itself a fair bit; I've extended the docs for it in #17301 (pending review) maybe something could help. Separately a while back I worked on a minimal port of pytest for micropython itself in https://github.com/andrewleech/micropython-pytest/ - not sure if that would help or not? At work we've got a more comprehensive mock-machine package to extend what can be tested with the unix port; we've previously been granted permission internally to release this open source I just haven't had time to clean it up enough to push anywhere, I'll try to take another run at that soon! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Context
I've been developing MicroPython packages for an irrigation project, which led me going down the rabbit hole on how best to create and manage these types of packages. I was also looking for a way to better automate and streamline unit tests and run integration tests on devices using pytest.
The repo and the code referenced here are at:
There is extra and more in depth documentation in the repo. Hopefully people find this useful or can give extra insight based on their experience.
Package Structure
I use Astral uv package manager, along with uv workspaces and a Python namespace package structure, which can replicate the extension package concept outlined in @ micropython-lib. This also allows the package to be installed locally in the uv
.venv
, enabling easier testing.Testing
I ran into the same issues as others, in that the unix port does not have certain modules such as device and network and cannot be relied on for testing all packages. As others have found, mocking or patching these modules and the interfaces they expose is an acceptable way to test the logic.
Unit Tests
There's definitely room for improvement with my conftest fixtures, which patch and mock the
machine
andnetwork
modules and thenetwork.WLAN
interface. I'm still working out how best to go about it and I'm keen to hear the solutions of others.These unit tests are in the /tests/unit/ repo directory.
Integration Tests
Having experimented with
mpremote
, I found a way to connect to a micro-controller in a script context, rather than through the CLI. Through a programmatic connection to the REPL, it is possible to send commands that utilise and test the package on the device, returning any output as a string for assertions within pytest functions.These strings can be JSON, which might be useful if you had for example, an object that tracks its state, which could be printed in JSON and loaded into the test function for assertions as a dict.
A
serial_connection
fixture with 'module' scope ensures all integration tests use the sameSerialTransport
instance, which is used to send commands over the raw REPL. If the fixture raises an exception or all tests have skipped/failed/completed, cleanup and teardown of the connection is carried out by the fixture.These integration tests are in /tests/integration/test_connection_and_package.py
I've only gotten as far as testing my network config logic, but I will be adding more integration tests that get a WLAN connection and connect to WiFi in STA or create an AP as per the interface my package exposes. I currently only have RPi Pico W devices to test on.
Beta Was this translation helpful? Give feedback.
All reactions