Skip to main content

MQTT IoT Testing

Rumour's MQTT engine allows you to write rigorous integration test cases for IoT message brokers and background workers. You can publish messages across specific QoS (Quality of Service) levels, or subscribe to topics to validate background worker processing.


The Challenge with Pub/Sub Testing​

Testing event-driven architectures is notoriously difficult due to race conditions. If your testing tool sends an MQTT PUBLISH payload, and then tries to SUBSCRIBE to a topic to verify the result, the broker might have already routed and deleted the message before your tool even connected to listen!

Rumour solves this gracefully with the publish_subscribe atomic action.


The Scenario​

We will construct a test that publishes an event to an IoT sensor topic, and simultaneously listens to the exact same topic to assert that the MQTT broker correctly echoed/routed the message to all active subscribers.

1. Actionable Setup​

Let's build this from scratch. First, we need a live environment to test against. We will use the official rumour-mock-server repository.

Start the Mock Server​

In a new terminal window, boot up the multi-protocol mock server (which spins up a lightweight Aedes MQTT broker on port 1883):

cd rumour-mock-server/mock-server
npm install
npm start

Scaffold the Workspace​

In your main terminal, initialize a fresh testing workspace and scaffold a new collection:

mkdir mqtt_tutorial
cd mqtt_tutorial
rumour init
rumour new iot_flows

Open the newly generated workspace.env.toml and define the target endpoint:

[variables]
mqtt_broker = "mqtt://127.0.0.1:1883"

2. Testing the Loopback​

Create your request file: test_mqtt_echo.toml.

name = "mqtt_publish_subscribe_echo"
protocol = "mqtt"

[request]
url = "{{mqtt_broker}}"

[body]
type = "json"
raw = '{"device": "sensor-01", "temperature": 45.2, "status": "critical"}'

[mqtt]
topic = "rumour/test/sensors"
subscribe_topic = "rumour/test/sensors"

# The magic atomic action
action = "publish_subscribe"

# QoS 1 guarantees the message is delivered at least once
qos = 1
timeout_ms = 6000

# Ensures the received message payload contains this exact string
expect_message = "sensor-01"

[assert]
# 200 implies the MQTT connection and actions succeeded without IO errors
status = 200
body_contains = "critical"

Let's break down what just happened:​

When using action = "publish_subscribe", Rumour orchestrates a highly specific connection dance:

  1. Connect & Subscribe: Rumour connects to the broker and immediately registers a subscription to subscribe_topic (rumour/test/sensors).
  2. Wait for ACK: It blocks execution until the broker explicitly acknowledges the subscription (SUBACK). This guarantees we won't miss any messages.
  3. Publish: Only after the subscription is locked in does Rumour publish the JSON [body] to topic.
  4. Listen: Rumour listens on the active socket. Because we are subscribed to the same topic we just published to, the broker instantly routes our own message back to us.
  5. Assert: Rumour captures the incoming payload and runs it against the expect_message and [assert.body_contains] rules.

The Output Trace​

POST mqtt://localhost:1883
URL: mqtt://localhost:1883
Body (json): {"device": "sensor-01", "temperature": 45.2, "status": "critical"}
✓ SUCCESS: test_mqtt_echo.toml (14ms)
tip

If you only want to test a fire-and-forget payload without listening for a response, simply change action = "publish", remove the subscribe_topic, and set qos = 0. Rumour will blast the payload to the broker and instantly mark the test as passed.