mqtt-tools / pytest-mqtt

pytest-mqtt supports testing systems based on MQTT.
MIT License
9 stars 2 forks source link

✨ Allowing pytest cli arguments for host and port #8

Closed zedfmario closed 6 months ago

zedfmario commented 10 months ago

TL;DR

Adding two optional pytest command line arguments describing a MQTT broker:

Hi there,

First of all, thank you for the project. It's been super helpful while developing MQTT related features.

However, while using the library I found it didn't quite match my current needs, so I tried to find a way to solve my issue. I hope you find this PR suitable for your package. Since it's my first time contributing to your project, I'm not 100% sure I'm covering all the requirements. Feel free to suggest any changes you may find to match your criteria, please.

I needed to test against a remote MQTT broker, not running on localhost. All connections to the MQTT broker were hardcoded to localhost:1883 though. I added the chance to make both values configurable, based on pytest cli arguments, using the request fixture.

Example of use

Given the following docker-compose.yml file

services:
  mqtt:
    image: eclipse-mosquitto:2
    container_name: mqtt
    hostname: mqtt
    restart: unless-stopped
    ports:
      - "11883:1883/tcp"
    volumes:
      - ./mosquitto.conf:/mosquitto/config/mosquitto.conf:rw

and starting the MQTT server on port 11883

> docker compose up mqtt
[+] Running 1/0
 ✔ Container mqtt  Created                                                                                                                                                                                                                                                                           0.0s
Attaching to mqtt
mqtt  | 1701088758: mosquitto version 2.0.18 starting
mqtt  | 1701088758: Config loaded from /mosquitto/config/mosquitto.conf.
mqtt  | 1701088758: Opening ipv4 listen socket on port 1883.
mqtt  | 1701088758: Opening ipv6 listen socket on port 1883.
mqtt  | 1701088758: mosquitto version 2.0.18 running

> docker ps -a
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS         PORTS                     NAMES
690e16652154   eclipse-mosquitto:2   "/docker-entrypoint.…"   22 seconds ago   Up 3 seconds   0.0.0.0:11883->1883/tcp   mqtt

running pytest against the new port works

> pytest --mqtt_port 11883 testing
========================================================================================================================================== test session starts ===========================================================================================================================================
platform darwin -- Python 3.11.5, pytest-7.4.3, pluggy-1.3.0 -- /Workspace/pytest-mqtt/.venv/bin/python3.11
cachedir: .pytest_cache
rootdir: /Workspace/pytest-mqtt
configfile: pyproject.toml
plugins: fixture-order-0.1.4, mqtt-0.3.2, ordering-0.6
collected 10 items

testing/test_mosquitto.py::test_mosquitto_running SKIPPED (Unable to run together with other test cases)                                                                                                                                                                                           [ 10%]
testing/test_app.py::test_app_version PASSED                                                                                                                                                                                                                                                       [ 20%]
testing/test_capmqtt.py::test_mqtt_client_adapter PASSED                                                                                                                                                                                                                                           [ 30%]
testing/test_integration.py::test_basic_submit_text_receive_binary PASSED                                                                                                                                                                                                                          [ 40%]
testing/test_integration.py::test_basic_submit_and_receive_binary PASSED                                                                                                                                                                                                                           [ 50%]
testing/test_integration.py::test_basic_submit_text_receive_text_marker PASSED                                                                                                                                                                                                                     [ 60%]
testing/test_integration.py::test_basic_submit_text_receive_text_config PASSED                                                                                                                                                                                                                     [ 70%]
testing/test_module_settings.py::test_basic_submit_text_receive_text PASSED                                                                                                                                                                                                                        [ 80%]
testing/test_util.py::test_probe_tcp_connect_available PASSED                                                                                                                                                                                                                                      [ 90%]
testing/test_util.py::test_probe_tcp_connect_unavailable PASSED

and the logs on the mqtt broker running on docker:

mqtt  | 1701089046: New connection from 192.168.144.1:44976 on port 1883.
mqtt  | 1701089046: New connection from 192.168.144.1:44968 on port 1883.
mqtt  | 1701089046: Client <unknown> closed its connection.
mqtt  | 1701089046: New client connected from 192.168.144.1:44968 as auto-39EFDC26-0F83-C3F9-AA61-DCA97AFBF938 (p2, c1, k60).
mqtt  | 1701089046: Client auto-39EFDC26-0F83-C3F9-AA61-DCA97AFBF938 disconnected.
mqtt  | 1701089046: New connection from 192.168.144.1:44982 on port 1883.
mqtt  | 1701089046: New client connected from 192.168.144.1:44982 as auto-FFEA2F95-4EB1-EA94-FE76-6E55A5C25586 (p2, c1, k60).
mqtt  | 1701089047: Client auto-FFEA2F95-4EB1-EA94-FE76-6E55A5C25586 disconnected.
mqtt  | 1701089047: New connection from 192.168.144.1:44988 on port 1883.
mqtt  | 1701089047: New client connected from 192.168.144.1:44988 as auto-ADC8D5D6-E426-6308-D1E6-C744790E751D (p2, c1, k60).
mqtt  | 1701089047: Client auto-ADC8D5D6-E426-6308-D1E6-C744790E751D disconnected.
mqtt  | 1701089047: New connection from 192.168.144.1:45002 on port 1883.
mqtt  | 1701089047: New client connected from 192.168.144.1:45002 as auto-EB7D08D0-F56B-F118-AC3C-33BACBEE90C4 (p2, c1, k60).
mqtt  | 1701089047: Client auto-EB7D08D0-F56B-F118-AC3C-33BACBEE90C4 disconnected.
mqtt  | 1701089047: New connection from 192.168.144.1:45008 on port 1883.
mqtt  | 1701089047: New client connected from 192.168.144.1:45008 as auto-BE6EE2C2-B903-6E3F-38DE-3830E0F5FF78 (p2, c1, k60).
mqtt  | 1701089047: Client auto-BE6EE2C2-B903-6E3F-38DE-3830E0F5FF78 disconnected.
mqtt  | 1701089047: New connection from 192.168.144.1:45016 on port 1883.
mqtt  | 1701089047: New client connected from 192.168.144.1:45016 as auto-5DA27123-440C-7DBF-0DC5-649FE943F244 (p2, c1, k60).
mqtt  | 1701089048: Client auto-5DA27123-440C-7DBF-0DC5-649FE943F244 disconnected.
codecov[bot] commented 10 months ago

Codecov Report

Attention: Patch coverage is 95.83333% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 95.45%. Comparing base (0e4c401) to head (6d94524). Report is 1 commits behind head on main.

Files Patch % Lines
pytest_mqtt/mosquitto.py 90.00% 1 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #8 +/- ## ========================================== + Coverage 95.07% 95.45% +0.38% ========================================== Files 5 5 Lines 142 154 +12 ========================================== + Hits 135 147 +12 Misses 7 7 ``` | [Flag](https://app.codecov.io/gh/mqtt-tools/pytest-mqtt/pull/8/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mqtt-tools) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/mqtt-tools/pytest-mqtt/pull/8/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mqtt-tools) | `95.45% <95.83%> (+0.38%)` | :arrow_up: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=mqtt-tools#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

amotl commented 6 months ago

Dear Mario,

thank you so much for submitting this patch, and apologies for missing it up until now. Your contribution is well received, and will be part of the next release of pytest-mqtt.

With kind regards, Andreas.

amotl commented 6 months ago

Dear Mario,

we push a few adjustments to your branch, along the lines of our suggestions, and renamed the CLI parameters to --mqtt-host resp. --mqtt-port. Thanks again for your contribution!

With kind regards, Andreas.

amotl commented 6 months ago

Hi again,

pytest-mqtt 0.4.0 has been released, including corresponding improvements from your pen, slightly adjusted. Thank you very much!

With kind regards, Andreas.