Skip to content
Jupiter documentation
Menu

Test framework

The fake devices, the simulated house and the closed-loop harness that the tests run the integration against, and how to add a case.

Four levels#

The suite tests the integration at four levels. Each level catches what the one before it cannot.

Fake devices#

tests/fakes.py holds FakeAc and FakeSwitch. Each is a real ClimateEntity or SwitchEntity, added to Home Assistant's own climate and switch integrations with add_devices(). A command the integration sends goes through Home Assistant's actions, as it does to a real device.

Each fake records the commands it received in calls. Three flags make it fail as real devices fail:

tests/ha/test_fakes.py holds the fakes to this table. A test built on a fake that drifts from Home Assistant would prove nothing.

Simulated house#

tests/sim/house.py is a room in plain Python with no Home Assistant import. It is richer than the model the integration fits, so the integration never sees its own equation back.

All randomness comes from one seeded generator. A run repeats exactly.

Closed-loop harness#

Harness in tests/sim/harness.py wraps each simulated device in a fake device, installs a real entry and runs the clock. Each minute it steps the house and fires due timers. Every 5 minutes it writes the sensors and runs one tick. The result is a Trace:

A Profile describes one run: the envelope, the weather, the ACs and their modes, a heater, the thermostat mode and extra settings. SensorFault and DeviceFault schedule failures by minute.

What the closed loop asserts#

tests/sim/test_closed_loop.py runs nine cooling profiles for 24 hours each, and each again with the room sensors failing for an hour.

  • No tick fails and no device refuses a command.
  • An AC starts at most 6 times in an hour and only after its restart delay.
  • The heater and an AC never run in the same minute.
  • After 3 hours the room is within 1.5 K of the target for 90 % of the minutes.
  • With every room sensor dead, each device is off within 20 minutes and stays off.

tests/sim/test_device_modes.py runs the device types that have not run on real hardware:

Two of these runs found defects when they were written. A cool-only AC was sent heat. An AC that raised on a command ended every tick, and the watchdog's stop then raised at the same device before it reached the other outputs.

Add a case#

  1. A fault a user reports about basic behaviour: add a test to tests/ha/test_basics.py first, then fix it.
  2. A device type or a mode: add a run to tests/sim/test_device_modes.py with a Profile.
  3. A way a device fails: add a flag to the fake, a test of the flag to tests/ha/test_fakes.py, and a DeviceFault kind.
  4. A new behaviour of a real device that the fake lacks: change the fake and its test in the same commit.

tests/ha/test_setup.py and tests/ha/test_settings_effect.py predate the fake devices. Most of their tests set a device's state directly and register their own action handlers. Use the fake devices for new tests in those files.

Source: belikh/hvac-horizon v0.8.0