27
27
28
28
29
29
class MqttClientAdapter (threading .Thread ):
30
- def __init__ (self , on_message_callback : t .Optional [t .Callable ] = None ):
30
+ def __init__ (self , on_message_callback : t .Optional [t .Callable ] = None , host : str = "localhost" , port : int = 1883 ):
31
31
super ().__init__ ()
32
32
self .client : mqtt .Client = mqtt .Client ()
33
33
self .on_message_callback = on_message_callback
34
+ self .host = host
35
+ self .port = int (port )
34
36
self .setup ()
35
37
36
38
def setup (self ):
@@ -43,7 +45,7 @@ def setup(self):
43
45
client .on_message = self .on_message_callback
44
46
45
47
logger .debug ("[PYTEST] Connecting to MQTT broker" )
46
- client .connect ("localhost" , port = 1883 )
48
+ client .connect (host = self . host , port = self . port )
47
49
client .subscribe ("#" )
48
50
49
51
def run (self ):
@@ -75,12 +77,12 @@ def publish(self, topic: str, payload: str, **kwargs) -> mqtt.MQTTMessageInfo:
75
77
class MqttCaptureFixture :
76
78
"""Provides access and control of log capturing."""
77
79
78
- def __init__ (self , decode_utf8 : t .Optional [bool ]) -> None :
80
+ def __init__ (self , decode_utf8 : t .Optional [bool ], host : str = "localhost" , port : int = 1883 ) -> None :
79
81
"""Creates a new funcarg."""
80
82
self ._buffer : t .List [MqttMessage ] = []
81
83
self ._decode_utf8 : bool = decode_utf8
82
84
83
- self .mqtt_client = MqttClientAdapter (on_message_callback = self .on_message )
85
+ self .mqtt_client = MqttClientAdapter (on_message_callback = self .on_message , host = host , port = port )
84
86
self .mqtt_client .start ()
85
87
# time.sleep(0.1)
86
88
@@ -119,20 +121,22 @@ def publish(self, topic: str, payload: str, **kwargs) -> mqtt.MQTTMessageInfo:
119
121
120
122
121
123
@pytest .fixture (scope = "function" )
122
- def capmqtt (request ):
124
+ def capmqtt (request , mqttcliargs ):
123
125
"""Access and control MQTT messages."""
124
126
125
127
# Configure `capmqtt` fixture, obtaining the `capmqtt_decode_utf8` setting from
126
128
# either a global or module-wide setting, or from a test case marker.
127
129
# https://docs.pytest.org/en/7.1.x/how-to/fixtures.html#fixtures-can-introspect-the-requesting-test-context
128
130
# https://docs.pytest.org/en/7.1.x/how-to/fixtures.html#using-markers-to-pass-data-to-fixtures
131
+
132
+ host , port = mqttcliargs
133
+
129
134
capmqtt_decode_utf8 = (
130
135
getattr (request .config .option , "capmqtt_decode_utf8" , False )
131
136
or getattr (request .module , "capmqtt_decode_utf8" , False )
132
137
or request .node .get_closest_marker ("capmqtt_decode_utf8" ) is not None
133
138
)
134
-
135
- result = MqttCaptureFixture (decode_utf8 = capmqtt_decode_utf8 )
139
+ result = MqttCaptureFixture (decode_utf8 = capmqtt_decode_utf8 , host = host , port = port )
136
140
delay ()
137
141
yield result
138
142
result .finalize ()
0 commit comments