Skip to main content

Project status (alpha, beta, stable) Project license PyPI downloads per month Package version on PyPI

CI outcome Code coverage Supported Python versions

About

pytest-mqtt supports testing systems based on MQTT by providing test fixtures for pytest. It has been conceived for the fine terkin-datalogger and mqttwarn programs.

capmqtt fixture

Capture MQTT messages, using the Paho MQTT Python Client, in the spirit of caplog and capsys. It can also be used to publish MQTT messages.

MQTT server host and port are configurable via pytest cli arguments: --mqtt-host and --mqtt-port. Default values are localhost/1883.

You may additionally specify a username and password for connecting to the broker with: --mqtt-username and --mqtt-password. Default values are guest/guest.

By default, the test fixture subscribes to all topics, listening on #. This can be disabled with the --no-subscribe-all option. You can then subscribe to the topics you are interested in within each test using capmqtt.mqtt_client.client.subscribe(cpe.info).

mosquitto fixture

Provide the Mosquitto MQTT broker as a session-scoped fixture to your test cases.

Usage

import pytest
from pytest_mqtt.model import MqttMessage

@pytest.mark.capmqtt_decode_utf8
def test_mqtt_send_receive(mosquitto, capmqtt):
    """
    Basic send/receive roundtrip, using text payload (`str`).

    By using the `capmqtt_decode_utf8` marker, the message payloads
    will be recorded as `str`, after decoding them from `utf-8`.
    Otherwise, message payloads would be recorded as `bytes`.
    """

    # Submit a basic MQTT message.
    capmqtt.publish(topic="foo", payload="bar")

    # Demonstrate the "messages" property.
    # It returns a list of "MqttMessage" objects.
    assert capmqtt.messages == [
        MqttMessage(topic="foo", payload="bar", userdata=None),
    ]

    # Demonstrate the "records" property.
    # It returns tuples of "(topic, payload, userdata)".
    assert capmqtt.records == [
        ("foo", "bar", None),
    ]

The capmqtt_decode_utf8 setting can be enabled in three ways.

  1. Session-wide, per pytestconfig option, for example within conftest.py:

    @pytest.fixture(scope="session", autouse=True)
    def configure_capmqtt_decode_utf8(pytestconfig):
        pytestconfig.option.capmqtt_decode_utf8 = True
  2. On the module level, just say capmqtt_decode_utf8 = True on top of your file.

  3. On individual test cases as a test case marker, using @pytest.mark.capmqtt_decode_utf8.

Issues

  • The mosquitto fixture currently does not support either authentication or encryption.

  • capmqtt should be able to capture messages only from specified topics.

Development

git clone https://github.com/mqtt-tools/pytest-mqtt
cd pytest-mqtt
python3 -m venv .venv
source .venv/bin/activate
pip install --editable=.[test,develop]
poe test

Project information

Contributions

Every kind of contribution, feedback, or patch, is much welcome. Create an issue or submit a patch if you think we should include a new feature, or to report or fix a bug.

Resources

License

The project is licensed under the terms of the MIT license, see LICENSE.

Release files for pytest-mqtt 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-mqtt 0.7.0
File Size Uploaded
pytest_mqtt-0.7.0.tar.gz 11.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-mqtt 0.7.0
File Interpreter ABI Platform
pytest_mqtt-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.0 kB

Release files / pytest_mqtt-0.7.0.tar.gz

Download URL pytest_mqtt-0.7.0.tar.gz
Size 11.8 kB
Tags Source
SHA-256 checksum
How to use checksums
fb8a74dcfc87ac8b397f8792913a4ed7c749d7fa22f8371de9d6d1d4ea71eb97
BLAKE2b-256 checksum
How to use checksums
e9a0ca8890b82e99bc928e7c71e96848cdff45d8e5f6f4c5f42980168d837973
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / pytest_mqtt-0.7.0-py3-none-any.whl

Download URL pytest_mqtt-0.7.0-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d3c003da758e6e5f8261c500d5bee8e11e3b419b5d789ce917ea9264fbb6638d
BLAKE2b-256 checksum
How to use checksums
1170dbd8468c7c74649b07230ce2252f678c325b48bb6f7e7e7e2ec27c8bf5a7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page