Skip to main content

pytest-mqtt


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.1

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.1
File Size Uploaded Provenance Actions
pytest_mqtt-0.7.1.tar.gz 11.9 kB Without provenance Details for pytest_mqtt-0.7.1.tar.gz

Built distribution (wheel)

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

Total release size: 22.1 kB

Release files / pytest_mqtt-0.7.1.tar.gz

Download URL pytest_mqtt-0.7.1.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
583f36c5ec610fa45bc95bdff585a42012ee308eaafac98f0db68ea5997e8785
BLAKE2b-256 checksum
How to use checksums
be1aec1e1945cebececdd6fb6e1407985c2da146126a7069dc1ae6290ae01f2c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

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

Download URL pytest_mqtt-0.7.1-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
110e1c488b0cafe735bd496de206aec8e35f21969977ca0c6bd7f621a5ebc968
BLAKE2b-256 checksum
How to use checksums
a03564e10d109a28f5826e6df266c00471b2994222b3e46bd4702c24a49dcb5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Security for pytest-mqtt 0.7.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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