pytest-mqtt supports testing systems based on MQTT
Project description
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.
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.
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
On the module level, just say capmqtt_decode_utf8 = True on top of your file.
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest_mqtt-0.4.3.tar.gz
.
File metadata
- Download URL: pytest_mqtt-0.4.3.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7db5496c26d75242daf814259a97f3288e7e3ff8af80fe50f8b5732f864e8b73 |
|
MD5 | 76c846552818ab7105d80f666fe86317 |
|
BLAKE2b-256 | a68dac11ffd3f2b720c393d560cfc615c5ef4e54fc292e84e337b5a710636092 |
File details
Details for the file pytest_mqtt-0.4.3-py3-none-any.whl
.
File metadata
- Download URL: pytest_mqtt-0.4.3-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ffd23db3c03d365f01571dfc7b8528672daa3c9b6fa5d5c9b10d727439de8d2 |
|
MD5 | cdd2a11952f4e3126110c537bbd1e552 |
|
BLAKE2b-256 | 4bd12b39b1d8f4a8648fa4472047bcb2bf39b414af9a701f68b863733adf7bf7 |