Skip to content

Testing ​

rpi-controller has three test suites. All of them run on a laptop with no hardware, no broker and no controller attached, and CI runs all three on every pull request into development, staging and main.

SuiteFolderWhat it proves
Unittests/, excluding api/ and integration/Each module decides correctly and records state truthfully, against mocks
Integrationtests/integration/Features do not contradict each other, against the real API and real service loops
APItests/api/The database API endpoints and the migrations

The split matters. A unit test can prove a service makes the right decision from the state it reads, but not that the state was written where it expects. The integration suite catches that.

Running the tests ​

Everything runs from the root of rpi-controller. pytest.ini sets the pythonpath entries every suite needs, and it is only found from there.

In a virtualenv ​

bash
python3 -m venv .venv
. .venv/bin/activate
pip install pytest pytest-cov requests paho-mqtt \
  fastapi sqlalchemy "pydantic>=2" httpx pytz alembic
bash
python -m pytest tests/ --ignore=tests/api --ignore=tests/integration -q
DATABASE_PATH="sqlite:///:memory:" python -m pytest tests/integration/ -q
DATABASE_PATH="sqlite:///:memory:" python -m pytest tests/api/ -q

With nix ​

The Makefile wraps the same commands in shell.nix and shell-api.nix.

TargetSuite
make test, make test-qUnit
make test-integration, make test-integration-qIntegration
make test-api, make test-api-qAPI
make coverageUnit, with a per file report of missing lines
make coverage-apiAPI, with a per file report of missing lines

The integration and API suites need shell-api.nix, since they stand up the FastAPI app. It builds .venv-api/ on first use.

A single test ​

bash
python -m pytest tests/integration/test_precedence.py -q
python -m pytest tests/ -k freeze -q
python -m pytest tests/api/test_schedules.py --pdb

How the suites are built ​

Unit ​

FolderCovers
tests/cfp_shared/The shared library, including every RS485 driver, the framing, transport and port resolution
tests/mqtt-sub-pubs/Every publisher and subscriber
tests/services/The scheduler, tether, themer, freeze protection and display driver

tests/conftest.py stubs paho.mqtt before anything imports it and sets the broker and API environment for every test. Some modules read their environment at import time, so the fixture patches those module attributes as well.

tests/fixtures.py holds canonical devices, device types and topics, with real UUIDs taken from a live controller.db. Factory functions such as make_pump() and make_valve() return deep copies, so a test can change them freely. Use these rather than building device dicts by hand.

Integration ​

The integration harness runs the real API over a real in memory database, drives the real service decision loops, and routes publishes through an in process broker. It exists to catch features that are each correct alone but wrong together, such as a schedule and a tether both driving the same device.

FileCovers
test_precedence.pyWhich feature wins when several want the same device
test_tether_and_schedule.pyA tether and a schedule on the same pump, through the API the scheduler reads
test_theme_and_user.pyThe rest of the precedence table, including the handover when a theme ends

API ​

tests/api/conftest.py stubs the Docker SDK, points the database at SQLite in memory, and imports the app, which creates the tables and seeds the defaults. Tests share one TestClient for the session.

test_migrations.py fails when alembic has more than one head. Two migrations with the same down_revision create two heads. Fix it with a merge migration whose down_revision is a tuple of both, never by re-parenting one of them, or a database already at one head skips the other.

Adding tests ​

  • Put a test beside the others for the same module, in the matching folder.
  • Build devices from tests/fixtures.py.
  • A new rule that interacts with another feature gets an integration test as well as a unit test.
  • A new endpoint gets an API test.
  • A new migration needs no test of its own. test_migrations.py covers the head count.

Hardware ​

The suites prove logic, not hardware. Anything that depends on real I²C, RS485 or display behaviour still needs a run on a dev Pi, and debug-scripts/rs485-bench/ covers RS485 units on the bench. See Onboard development.

Mobile app ​

mobile-controller has no test suite. Lint before pushing:

bash
yarn check:eslint

A pre-commit hook runs eslint on staged .ts and .tsx files.