pytest-mqtt支持基于MQTT的系统测试
项目描述
关于
pytest-mqtt 通过为pytest提供测试固件,支持基于MQTT的系统测试。它是为精细的terkin-datalogger和mqttwarn程序而设计的。
capmqtt 固件
捕获MQTT消息,使用Paho MQTT Python客户端,类似于caplog和capsys。它也可以用于发布MQTT消息。
MQTT服务器的主机和端口可以通过pytest命令行参数配置:--mqtt-host和--mqtt-port。默认值是localhost/1883。
mosquitto fixture
将Mosquitto MQTT代理作为会话范围内的fixture提供给测试用例。
用法
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), ]
可以通过三种方式启用capmqtt_decode_utf8设置。
在会话范围内,通过pytestconfig选项,例如在conftest.py中。
@pytest.fixture(scope="session", autouse=True) def configure_capmqtt_decode_utf8(pytestconfig): pytestconfig.option.capmqtt_decode_utf8 = True
在模块级别,只需在文件顶部声明capmqtt_decode_utf8 = True。
在单个测试用例中作为测试用例标记,使用@pytest.mark.capmqtt_decode_utf8。
问题
mosquitto fixture当前不支持身份验证或加密。
capmqtt应该只能捕获指定主题的消息。
开发
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
项目信息
贡献
欢迎各种贡献、反馈或补丁。如果您认为我们应该包含新功能,或者报告或修复错误,请创建问题或提交补丁。
资源
许可协议
该项目根据MIT许可证的条款进行许可,请参阅LICENSE。
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源代码分发
pytest_mqtt-0.4.2.tar.gz (10.6 kB 查看哈希值)
构建分发
pytest_mqtt-0.4.2-py3-none-any.whl (9.3 kB 查看哈希值)