Skip to main content

pytest-mqtt 支持基于 MQTT 的测试系统

项目描述

CI结果 测试套件代码覆盖率 每月 PyPI 下载量 PyPI 上的包版本 项目状态(alpha、beta、稳定) 支持的 Python 版本 项目许可证

pytest-mqtt

关于

pytest-mqtt通过为pytest提供测试夹具来支持基于 MQTT 的测试系统。

它是为精细的terkin-dataloggermqttwarn 程序而设计的。

细节

capmqtt夹具

本着 caplogcapsys的精神,使用Paho MQTT Python Client捕获 MQTT 消息。它还可以用于发布 MQTT 消息。

蚊子夹具

Mosquitto MQTT 代理作为会话范围的夹具提供给您的测试用例。

用法

@pytest.mark.capmqtt_decode_utf8
def test_mqtt_send_receive(mosquitto, capmqtt):
    """
    Basic send/receive roundtrip, with ASCII text payload (`str`).
    By using the `capmqtt_decode_utf8` marker, the payloads will also be received
    as `str`, after decoding them from `utf-8`.
    """

    # Submit two MQTT messages.
    capmqtt.publish(topic="foo", payload="bar")

    # Demonstrate the `messages` property.
    assert capmqtt.messages == [
        MqttMessage(topic="foo", payload="bar", userdata=None),
    ]

    # Demonstrate the `records` property.
    assert capmqtt.records == [
        ("foo", "bar", None),
    ]

capmqtt_decode_utf8设置可以通过三种方式启用

  1. 会话范围,每个pytestconfig选项,例如在conftest.py 中

    @pytest.fixture(scope="session", autouse=True)
    def configure_capmqtt_decode_utf8(pytestconfig):
        pytestconfig.option.capmqtt_decode_utf8 = True
  2. 在模块级别,只需在文件顶部说capmqtt_decode_utf8 = True 。

  3. 对单个测试用例作为测试用例标记。

问题

  • 目前,这两个装置不支持以不同于localhost:1883的方式更改 MQTT 代理主机名和端口号。

  • mosquitto夹具当前不支持身份验证或加密。

  • 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.1.0.tar.gz (9.4 kB 查看哈希)

已上传 source

内置分布

pytest_mqtt-0.1.0-py3-none-any.whl (8.6 kB 查看哈希

已上传 py3