From Flaky Tests to Fleet-Wide Confidence: How a Robotics Engineer Used AI Prompts to Automate IoT Testing

Imagine spending 40% of your week debugging why a smart thermostat failed its test suite—only to realize the sensor data was delayed by 200ms due to a network hiccup. That was my reality as a robotics engineer at a smart-home device company. We were drowning in flaky tests, manual test scripts, and devices that behaved differently on every Wi-Fi router. Then I discovered the power of AI prompts: not just for generating code, but for orchestrating the entire testing lifecycle. In this case study, I'll share how I used specific prompt patterns to automate IoT testing, slash manual effort, and finally trust our test results.

The Problem: When Test Automation Becomes the Bottleneck

Our IoT ecosystem included smart bulbs, sensors, and a central hub—all communicating via MQTT over unstable networks. Our test suite was a mix of Python scripts (pytest) and manual steps. The pain points:

  • Flaky tests: 15% of tests failed randomly due to network latency, causing teams to ignore failures.
  • Manual test data: We spent hours creating mock sensor data for edge cases.
  • Regression coverage: When we changed firmware, we missed testing older device models.
  • Log analysis: Debugging required manually grepping logs across multiple devices.

The breaking point was a firmware update that broke OTA (over-the-air) updates for a legacy device. The bug passed our tests but failed in production. We needed a smarter way.

The Solution: Prompt-Driven Test Orchestration

I turned to AI prompts as a co-pilot for our testing pipeline. The idea: use large language models (LLMs) like GPT-4 to generate test cases, analyze logs, and even auto-heal flaky tests. Below are the exact prompts I used (in a structured format), with examples and results.

1. Generating Edge-Case Test Data

Task: Create realistic but extreme sensor data for our temperature sensor.

Prompt:

Generate a Python list of 20 test cases for a temperature sensor, including normal values, boundary values, and error conditions. For each test case, provide input (temperature in Celsius) and expected output (e.g., 'low', 'normal', 'high', 'error'). Include values like -40, 0, 25, 85, 100, and non-numeric inputs.

Example result:

test_cases = [
    (-40, 'low'),
    (0, 'low'),
    (5, 'normal'),
    (25, 'normal'),
    (35, 'normal'),
    (40, 'high'),
    (85, 'high'),
    (100, 'error'),
    (None, 'error'),
    ('25', 'error'),  # string input
]
# ... and 10 more

Why it worked: The LLM understood the domain and generated boundary values I hadn't considered, like non-numeric inputs.

2. Writing Pytest Scripts from Natural Language

Task: Generate a pytest file for testing the MQTT reconnection logic.

Prompt:

Write a pytest script for the function `mqtt_reconnect()` in `mqtt_client.py`. The test should:
- Simulate a connection drop and verify reconnection within 5 seconds.
- Use a mock MQTT client.
- Include assertions for the number of reconnection attempts.

Example result:

import pytest
from unittest.mock import MagicMock, patch
from mqtt_client import mqtt_reconnect

def test_reconnect_success():
    mock_client = MagicMock()
    mock_client.is_connected.return_value = False
    with patch('time.sleep', return_value=None):
        reconnect = mqtt_reconnect(mock_client)
    assert reconnect is True
    assert mock_client.connect.call_count == 1

Why it worked: The prompt specified the function name, desired behavior, and mocking strategy, giving the LLM enough context.

3. Generating Test Cases for Protocol Compliance

Task: Ensure our device firmware complies with the MQTT 3.1.1 spec.

Prompt:

Given the MQTT 3.1.1 specification (http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html), generate a checklist of 10 test cases for a client that publishes and subscribes. Include tests for QoS 0, 1, 2, and retained messages.

Example result:

Test Case Description Expected Behavior
PUBLISH QoS 0 Send a message with QoS 0 Client receives it once, no ack
PUBLISH QoS 1 Send with QoS 1 Client receives it at least once, PUBACK sent
SUBSCRIBE with wildcard # Subscribe to home/+/temp Receive messages from matching topics
Retained message Publish with retain flag New subscriber gets last retained message
... ... ...

Why it worked: By referencing the official OASIS spec, the prompt anchored the LLM to a concrete standard, reducing hallucination.

4. Analyzing Logs for Root-Cause Analysis

Task: Automate log analysis after a test failure.

Prompt:

You are a debugging assistant. Here is a log snippet from an IoT device test failure. Identify the root cause, suggest a fix, and explain how to prevent it. Log:

[2026-08-21 14:22:01] ERROR: MQTT connection lost
[2026-08-21 14:22:03] WARN: Reconnect attempt 1
[2026-08-21 14:22:03] ERROR: Unable to establish TLS connection: certificate verify failed

Example result:
The root cause is TLS certificate verification failure. The device likely has an outdated root CA. Suggested fix: update the CA bundle in the firmware. To prevent this, add a CI test that checks certificate expiry.

Why it worked: The prompt provided context (log snippet) and a clear task (root cause + fix). I then integrated this into a CI script that posts logs to the LLM automatically.

5. Auto-Healing Flaky Tests

Task: Identify and fix flaky tests in our pytest suite.

Prompt:

Analyze the following test code and suggest changes to make it less flaky. The test sometimes fails due to network timeouts. Use proper waiting strategies like `pytest-timeout` or `WebDriverWait` (if applicable).
```python
def test_sensor_reading():
    reading = get_sensor_data()
    assert reading['temperature'] > 0
**Example result**:
Add a retry mechanism or use `pytest-timeout` to set a timeout. Also, wait for the sensor to be ready.

### 6. Generating a Test Plan for a New Firmware Release

**Prompt**:
```text
Create a test plan for firmware v2.0 that adds Zigbee support. Include regression tests, new feature tests, and compatibility tests for existing devices.

Example result:
A structured plan with sections: Smoke Tests, Regression Tests (existing Wi-Fi features), Zigbee-specific tests (pairing, communication range), and Compatibility Tests (with hub and sensors).

7. Converting Manual Test Steps to Automated Python

Prompt:

Convert the following manual test steps into a Python script using pytest and device API: 1. Turn on the device. 2. Wait 2 seconds. 3. Check that the LED is green. 4. Turn off.

Example result: A script using requests to call the device API and assert the LED status.

8. Creating a Mock MQTT Broker for Testing

Prompt:

Write a Python script using `paho-mqtt` to create a mock MQTT broker that can publish test messages and log received ones. Use `localhost` port 1883.

Example result: A script that sets up a broker and a subscriber to print messages.

9. Generating Performance Test Scenarios

Prompt:

Define 5 performance test scenarios for an IoT hub that handles 100 devices. Include latency, throughput, and CPU usage metrics.

Example result: Scenarios with expected thresholds, e.g., command response time < 200ms.

10. Writing a CI Pipeline for IoT Tests

Prompt:

Generate a GitHub Actions workflow that runs pytest on push, using a matrix for Python 3.9 and 3.11, and uploads test results as artifacts.

Example result: A YAML workflow with actions/checkout, actions/setup-python, and pytest steps.

11. Summarizing Test Results for Reports

Prompt:

Summarize the following pytest output into a concise report for management, highlighting pass/fail counts and key failures.

Example result: A bulleted summary with metrics and critical issues.

12. Generating Regression Test Data for Legacy Devices

Task: Create a dataset for testing old firmware versions.

Prompt:

Generate 5 test cases for a legacy firmware that expects a different JSON format for the `status` endpoint.

Example result: JSON payloads that mimic the old format.

13. Optimizing Test Execution Time

Prompt:

Propose ways to parallelize our pytest suite using `pytest-xdist`. Provide code examples for running tests in parallel.

Example result: Commands like pytest -n 4 and notes on test isolation.

14. Predicting Test Failures with AI

Prompt:

Based on code changes in this commit, predict which tests might fail. List the top 5 risk areas.

Example result: A list of modules likely affected, based on dependencies.

Results: From 40% Manual Work to 10%

After implementing these prompt-driven approaches, our testing efficiency improved dramatically:

  • Manual test effort: Reduced by 75% (we estimated this internally by tracking time logs).
  • Flaky test rate: Dropped from 15% to 2% (we measured this in our CI dashboard over 3 months).
  • Bug detection: We caught 3 critical bugs before release that would have affected production.
Metric Before After
Manual hours/week 20 5
Flaky test rate 15% 2%
Test coverage 70% 90%

Key Takeaways for Your IoT Testing

  1. Use AI prompts as a co-pilot, not a replacement: The LLM generated drafts, but I reviewed and adapted them to our environment.
  2. Anchor prompts to official specs: For protocol compliance, reference the exact RFC or standard (e.g., MQTT 3.1.1) to reduce hallucinations.
  3. Automate the orchestration: Integrate prompts into CI pipelines using tools like GitHub Actions, so they run automatically on every commit.
  4. Iterate and refine: The first prompt rarely works perfectly; I refined prompts based on the output quality.

If you're struggling with IoT test automation, start by picking one repetitive task and apply a prompt like the ones above. You'll be surprised how quickly you recover your time and sanity.

← All posts

Comments