Testing IoT Devices: Strategies for Hardware-Software Integration Testing

Testing IoT Devices: Strategies for Hardware-Software Integration Testing

IoT devices sit at the intersection of hardware and software, and that intersection is where bugs love to hide. A firmware change that works flawlessly in a unit test can cause a sensor to report garbage data when it encounters real electromagnetic interference. A network stack that passes all integration tests can deadlock when a cellular modem drops and reconnects mid-transmission. Testing IoT devices well means building a strategy that spans both worlds — silicon and software — and it requires a fundamentally different mindset than testing a web application.

This post walks through practical strategies for hardware-software integration testing, with concrete tooling and code examples you can adapt today.

Why IoT Integration Testing Is Uniquely Hard

In traditional software testing, the "hardware" is abstracted away. You run tests on a machine, and the machine behaves predictably. IoT testing removes that abstraction. You're testing against:

  • Physical sensors that drift, saturate, or fail
  • Communication protocols (MQTT, CoAP, BLE, LoRa) with real-world timing constraints
  • Power states — deep sleep, wake-on-interrupt, low-battery behavior
  • Firmware that runs on microcontrollers with kilobytes of RAM
  • Environmental conditions — temperature, humidity, vibration

The challenge is that most of these factors are non-deterministic. Your test suite needs to handle that non-determinism explicitly rather than assume it away.

Layer Your Tests: Unit, Integration, and System

A mature IoT test strategy mirrors the classic test pyramid, but the layers look different.

Unit Tests: Test Logic in Isolation on Native Hardware

Most embedded firmware can be compiled and run on a host machine for unit testing. Tools like Unity and CMock are the standard for C-based firmware. You mock peripheral drivers (ADC, SPI, I2C) and test pure logic.

// sensor_processor.c
float process_temperature_reading(int16_t raw_adc, float vref, float r_nominal) {
    float voltage = (raw_adc / 4095.0f) * vref;
    float resistance = r_nominal * voltage / (vref - voltage);
    // Steinhart-Hart equation for NTC thermistor
    float log_r = logf(resistance / r_nominal);
    float temp_k = 1.0f / (0.001129f + 0.000234f * log_r + 0.0000000877f * log_r * log_r * log_r);
    return temp_k - 273.15f;
}
// test_sensor_processor.c
#include "unity.h"
#include "sensor_processor.h"

void test_room_temperature_reading(void) {
    // ADC value corresponding to ~25°C for a 10kΩ NTC thermistor
    int16_t raw = 2048;
    float result = process_temperature_reading(raw, 3.3f, 10000.0f);
    TEST_ASSERT_FLOAT_WITHIN(2.0f, 25.0f, result);
}

void test_freezing_point_reading(void) {
    int16_t raw = 3200;
    float result = process_temperature_reading(raw, 3.3f, 10000.0f);
    TEST_ASSERT_FLOAT_WITHIN(2.0f, 0.0f, result);
}

void test_overrange_clamps_gracefully(void) {
    int16_t raw = 4095; // rail voltage — open circuit
    float result = process_temperature_reading(raw, 3.3f, 10000.0f);
    TEST_ASSERT_TRUE(result > 100.0f || isnan(result)); // should flag fault condition
}

Run these in CI on every commit. They're fast, deterministic, and catch most logic bugs before hardware is ever involved.

Hardware-in-the-Loop (HIL) Testing

Unit tests cover logic. HIL testing connects real hardware to your test automation. A HIL rig consists of:

  1. Device Under Test (DUT) — the actual PCB or module
  2. Test controller — a Raspberry Pi, Jetson, or standard PC connected via UART/JTAG/USB
  3. Signal stimulation — GPIO toggles, sensor simulators, power supply control

Here's a Python HIL test using pyserial and a custom test harness:

import serial
import time
import pytest

class IoTDeviceHarness:
    def __init__(self, port="/dev/ttyUSB0", baud=115200):
        self.serial = serial.Serial(port, baud, timeout=2.0)
        time.sleep(2)  # wait for device boot

    def send_command(self, cmd: str) -> str:
        self.serial.write(f"{cmd}\r\n".encode())
        response = self.serial.readline().decode().strip()
        return response

    def reset_device(self):
        # Toggle reset line via GPIO or send soft-reset command
        self.send_command("RESET")
        time.sleep(3)

    def read_sensor(self, sensor_id: str) -> float:
        response = self.send_command(f"READ {sensor_id}")
        # Expected format: "SENSOR:temp VALUE:24.7 UNIT:C STATUS:OK"
        parts = dict(p.split(":") for p in response.split())
        if parts.get("STATUS") != "OK":
            raise ValueError(f"Sensor read failed: {response}")
        return float(parts["VALUE"])

    def close(self):
        self.serial.close()


@pytest.fixture(scope="session")
def device():
    harness = IoTDeviceHarness()
    yield harness
    harness.close()


def test_temperature_sensor_reads_in_range(device):
    temp = device.read_sensor("temp")
    assert 15.0 <= temp <= 40.0, f"Temperature {temp}°C outside expected lab range"


def test_humidity_sensor_reads_in_range(device):
    humidity = device.read_sensor("humidity")
    assert 20.0 <= humidity <= 80.0, f"Humidity {humidity}% outside expected range"


def test_device_recovers_after_reset(device):
    device.reset_device()
    # First read after boot should succeed within 5 seconds
    temp = device.read_sensor("temp")
    assert temp is not None
    assert -40.0 <= temp <= 85.0  # full operating range post-reset

Fault Injection Testing

Hardware fails in ways software doesn't. Good HIL rigs include fault injection: the ability to cut power, simulate sensor shorts, or inject corrupted data. Use a programmable power supply and GPIO-controlled relay boards to automate this.

def test_brownout_recovery(device, power_controller):
    """Device should recover gracefully from a brief power sag."""
    initial_temp = device.read_sensor("temp")

    # Drop voltage to 2.7V (below brownout threshold) for 100ms
    power_controller.set_voltage(2.7)
    time.sleep(0.1)
    power_controller.set_voltage(3.3)

    # Device should reboot and become responsive within 5 seconds
    time.sleep(5)
    recovery_temp = device.read_sensor("temp")
    assert abs(recovery_temp - initial_temp) < 5.0, "Temperature wildly different after brownout"

Build a Device Abstraction Layer for Testability

One of the best investments you can make in IoT testability is designing your firmware with a clean hardware abstraction layer (HAL). When all hardware access goes through an interface, you can swap in mock implementations for testing.

// hal_interface.h
typedef struct {
    int  (*gpio_read)(uint8_t pin);
    void (*gpio_write)(uint8_t pin, uint8_t value);
    int  (*i2c_read)(uint8_t addr, uint8_t reg, uint8_t *buf, size_t len);
    int  (*i2c_write)(uint8_t addr, uint8_t reg, const uint8_t *data, size_t len);
    uint32_t (*get_timestamp_ms)(void);
} hal_t;

// Production implementation uses real hardware
extern hal_t hw_hal;

// Test implementation returns canned values
extern hal_t mock_hal;
// Application code only touches the HAL
void read_accelerometer(hal_t *hal, float *x, float *y, float *z) {
    uint8_t buf[6];
    hal->i2c_read(ACCEL_ADDR, REG_DATA_X, buf, 6);
    *x = (int16_t)((buf[1] << 8) | buf[0]) * ACCEL_SCALE;
    *y = (int16_t)((buf[3] << 8) | buf[2]) * ACCEL_SCALE;
    *z = (int16_t)((buf[5] << 8) | buf[4]) * ACCEL_SCALE;
}

With this pattern, you can test read_accelerometer on your laptop without any hardware, and your HIL tests use the same function via the real HAL.

Integrating HIL Tests Into CI/CD

Most teams run HIL tests on a dedicated test rig connected to a CI runner. GitHub Actions supports self-hosted runners, which means you can connect your HIL hardware to a runner and trigger tests on every PR.

# .github/workflows/hil-tests.yml
name: Hardware-in-the-Loop Tests

on:
  pull_request:
    paths:
      - 'firmware/**'
      - 'tests/hil/**'

jobs:
  hil-test:
    runs-on: [self-hosted, hil-rig-1]
    steps:
      - uses: actions/checkout@v4

      - name: Build firmware
        run: |
          cmake -B build -DTARGET=stm32f4 -DCMAKE_BUILD_TYPE=Release
          cmake --build build --target firmware.bin

      - name: Flash device
        run: |
          openocd -f interface/stlink.cfg -f target/stm32f4x.cfg \
            -c "program build/firmware.bin verify reset exit"

      - name: Run HIL tests
        run: pytest tests/hil/ -v --timeout=120

      - name: Upload test report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: hil-test-report
          path: test-results/

Handling Flakiness in Hardware Tests

Hardware tests are inherently flakier than software tests. A test that polls a sensor might occasionally hit a timing window where the sensor is mid-conversion. Build retry logic and clear failure modes into your harness:

def read_sensor_with_retry(device, sensor_id, retries=3, delay=0.5):
    last_error = None
    for attempt in range(retries):
        try:
            return device.read_sensor(sensor_id)
        except ValueError as e:
            last_error = e
            time.sleep(delay)
    raise RuntimeError(f"Sensor {sensor_id} failed after {retries} attempts: {last_error}")

Mark genuinely flaky tests with @pytest.mark.flaky(reruns=3) using pytest-rerunfailures, but investigate every flaky test — flakiness in hardware tests is almost always a signal of a real intermittent bug, not test infrastructure noise.

Key Takeaways

  • Layer your tests: unit tests on host, integration tests on HIL rigs, system tests in realistic environments
  • Design for testability: a clean HAL makes firmware unit-testable without hardware
  • Automate HIL tests in CI: self-hosted runners make this achievable
  • Inject faults deliberately: brownouts, sensor failures, and network drops are not edge cases — they're normal operating conditions
  • Treat flakiness as signal: a hardware test that flickers is exposing a real intermittent defect

IoT integration testing is harder than web app testing, but the cost of getting it wrong — a fleet of devices in the field with a deadlocking firmware — is dramatically higher. The investment pays off.

Read more

Start now free