From 27110b7b62c0c065472d923de67fc5acf28283c5 Mon Sep 17 00:00:00 2001 From: Matthias Blankertz Date: Sat, 23 Aug 2025 13:04:04 +0200 Subject: [PATCH] Add infrastructure for pytest and mypy Make mypy run with proper type stubs for micropython RP2. Add basic structure for pytest testing. Signed-off-by: Matthias Blankertz --- .gitignore | 3 +++ DEVELOP.md | 11 +++++++++++ software/mypy.ini | 9 +++++++++ software/pytest.ini | 3 +++ software/src/main.py | 2 +- software/src/utils/buttons.py | 2 +- software/tests/mocks/machine.py | 2 ++ software/tests/mocks/micropython.py | 2 ++ software/tests/mocks/rp2_sd.py | 5 +++++ software/tests/requirements.txt | 1 + software/tests/test_dummy.py | 3 +++ software/tests/utils_test/__init__.py | 0 12 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 DEVELOP.md create mode 100644 software/mypy.ini create mode 100644 software/pytest.ini create mode 100644 software/tests/mocks/machine.py create mode 100644 software/tests/mocks/micropython.py create mode 100644 software/tests/mocks/rp2_sd.py create mode 100644 software/tests/requirements.txt create mode 100644 software/tests/test_dummy.py create mode 100644 software/tests/utils_test/__init__.py diff --git a/.gitignore b/.gitignore index c05765c..a7360b5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ hardware/tonberry-pico/tonberry-pico-backups/ *.kicad_sch-bak *.kicad_sch.lck software/build +software/typings compile_commands.json .dir-locals.el .cache +\#*# +__pycache__ diff --git a/DEVELOP.md b/DEVELOP.md new file mode 100644 index 0000000..6b60080 --- /dev/null +++ b/DEVELOP.md @@ -0,0 +1,11 @@ +# Developer notes + +## How to setup python environment for mypy and pytest + +```bash +cd software +python -m venv test-venv +. test-venv/bin/activate +pip install -r tests/requirements.txt +pip install -U micropython-rp2-pico_w-stubs --target typings +``` diff --git a/software/mypy.ini b/software/mypy.ini new file mode 100644 index 0000000..0744a67 --- /dev/null +++ b/software/mypy.ini @@ -0,0 +1,9 @@ +[mypy] +platform = linux +mypy_path = $MYPY_CONFIG_FILE_DIR/src:$MYPY_CONFIG_FILE_DIR/typings +custom_typeshed_dir = $MYPY_CONFIG_FILE_DIR/typings +follow_imports = silent +exclude = "typings[\\/].*" +follow_imports_for_stubs = true +no_site_packages = true +check_untyped_defs = true \ No newline at end of file diff --git a/software/pytest.ini b/software/pytest.ini new file mode 100644 index 0000000..d3458fa --- /dev/null +++ b/software/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +pythonpath = tests/mocks src +testpaths = tests \ No newline at end of file diff --git a/software/src/main.py b/software/src/main.py index 98ac1aa..c5862cf 100644 --- a/software/src/main.py +++ b/software/src/main.py @@ -1,7 +1,7 @@ # SPDX-License-Identifier: MIT # Copyright (c) 2024-2025 Matthias Blankertz -import aiorepl +import aiorepl # type: ignore import asyncio import machine import micropython diff --git a/software/src/utils/buttons.py b/software/src/utils/buttons.py index 12109fa..140ffc6 100644 --- a/software/src/utils/buttons.py +++ b/software/src/utils/buttons.py @@ -17,7 +17,7 @@ if TYPE_CHECKING: class Buttons: - def __init__(self, cb: ButtonCallback, pin_volup=17, pin_voldown=19, pin_next=18): + def __init__(self, cb: "ButtonCallback", pin_volup=17, pin_voldown=19, pin_next=18): self.VOLUP = micropython.const(1) self.VOLDOWN = micropython.const(2) self.NEXT = micropython.const(3) diff --git a/software/tests/mocks/machine.py b/software/tests/mocks/machine.py new file mode 100644 index 0000000..7c14b67 --- /dev/null +++ b/software/tests/mocks/machine.py @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: MIT +# Copyright (c) 2025 Matthias Blankertz diff --git a/software/tests/mocks/micropython.py b/software/tests/mocks/micropython.py new file mode 100644 index 0000000..7c14b67 --- /dev/null +++ b/software/tests/mocks/micropython.py @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: MIT +# Copyright (c) 2025 Matthias Blankertz diff --git a/software/tests/mocks/rp2_sd.py b/software/tests/mocks/rp2_sd.py new file mode 100644 index 0000000..e1498d3 --- /dev/null +++ b/software/tests/mocks/rp2_sd.py @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: MIT +# Copyright (c) 2025 Matthias Blankertz + +class SDCard(): + pass diff --git a/software/tests/requirements.txt b/software/tests/requirements.txt new file mode 100644 index 0000000..e079f8a --- /dev/null +++ b/software/tests/requirements.txt @@ -0,0 +1 @@ +pytest diff --git a/software/tests/test_dummy.py b/software/tests/test_dummy.py new file mode 100644 index 0000000..c4c0d4c --- /dev/null +++ b/software/tests/test_dummy.py @@ -0,0 +1,3 @@ +def test_dummy(): + # This is just a dummy test to make pytest not fail because no tests are defined + pass diff --git a/software/tests/utils_test/__init__.py b/software/tests/utils_test/__init__.py new file mode 100644 index 0000000..e69de29