Skip to main content

pytest-mqtt supports testing systems based on MQTT

Project description

CI outcome Test suite code coverage PyPI downloads per month Package version on PyPI Project status (alpha, beta, stable) Supported Python versions Project license

pytest-mqtt

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.

mosquitto fixture

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

Usage

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

  • Both fixtures currently do not support changing the MQTT broker hostname and port number differently than localhost:1883.

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-mqtt-0.2.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

pytest_mqtt-0.2.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file pytest-mqtt-0.2.0.tar.gz.

File metadata

  • Download URL: pytest-mqtt-0.2.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/65.4.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.15

File hashes

Hashes for pytest-mqtt-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fb9327896a509050ae4151ec6dbf31a0a31a2e0338acd96d02c260d5139377d0
MD5 2bb7f918079070e1db09cb2a0202168f
BLAKE2b-256 aa6d99e6cdc119d6b8b7e5195fcbd80f66ced9e78cfac3c79010e65a58b3f596

See more details on using hashes here.

Provenance

File details

Details for the file pytest_mqtt-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_mqtt-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/65.4.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.15

File hashes

Hashes for pytest_mqtt-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 68c6806518dcc2dbeb4f4a1702cd9f2ba20c447839c8bc25b79dd1258e0c09d0
MD5 b40a3ddd5d4e3ba4477a1ab63cd7bbde
BLAKE2b-256 6f203c8cd7a4a2ef078e04d90ed173004e18951ec00f7cb7ee63f697a0df8d56

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page