Skip to content

Conversation

OliverWoolland
Copy link
Contributor

Summary

Adds optional authentication support by allowing username and password to be set via CLI arguments.

This should enable connections to brokers which disallow anonymous connections or if authentication is expected.

Defaults remain unchanged (guest/guest), so existing usage should be unaffected.

Details

  • Adds --mqtt-username and --mqtt-password CLI args to pytest-mqtt
  • Updates the fixtures / clients to accept credentials
  • Documents new arguments in README.rst

Copy link

codecov bot commented Jun 25, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.08%. Comparing base (ab524fd) to head (0e29dba).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #41      +/-   ##
==========================================
+ Coverage   95.90%   96.08%   +0.18%     
==========================================
  Files           5        5              
  Lines         171      179       +8     
==========================================
+ Hits          164      172       +8     
  Misses          7        7              
Flag Coverage Δ
unittests 96.08% <100.00%> (+0.18%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@OliverWoolland OliverWoolland marked this pull request as draft June 25, 2025 10:28
@OliverWoolland OliverWoolland marked this pull request as ready for review June 25, 2025 10:45
@tiniuclx
Copy link

@amotl would love to see this merged. I'm looking into using this library for regression tests on 5G routers, and it would be quite inconvenient for us to use a separate unauthenticated broker for testing as opposed to the testing instances we already have.

@amotl
Copy link
Member

amotl commented Sep 10, 2025

Hi @OliverWoolland and @tiniuclx. Thanks a stack for the reminder. This certainly skipped past our attention.

Copy link

@tiniuclx tiniuclx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this with a server that requires authentication, and the connection goes through successfully & I can receive & send MQTT messages with no issues.

image

@amotl
Copy link
Member

amotl commented Sep 10, 2025

Excellent. Because CI is also successful, I think it clearly demonstrates that the patch isn't harmful when running MQTT brokers without authentication.

@@ -52,6 +62,7 @@ def setup(self):
client.on_message = self.on_message
if self.on_message_callback:
client.on_message = self.on_message_callback
client.username_pw_set(self.username, self.password)
Copy link
Member

@amotl amotl Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it clearly demonstrates that the patch isn't harmful when running MQTT brokers without authentication.

It could make sense to not invoke this method when no credentials are defined. However, it apparently works well with Mosquitto (using guest/guest), which is the primary focus here, because pytest-mqtt brings it to the table as a testlayer broker. In this spirit, we consider it safe for merging unless/until we receive different notices.

@amotl amotl merged commit 2030f87 into mqtt-tools:main Sep 10, 2025
16 checks passed
@amotl
Copy link
Member

amotl commented Sep 10, 2025

Hi again. pytest-mqtt 0.6.0 has been released, including your improvements. Thanks again, @OliverWoolland.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants