44
55"""tests that verify the connect behavior w.r.t. port number and TLS"""
66
7+ import pytest
78import socket
89import ssl
9- from unittest import TestCase , main
1010from unittest .mock import Mock , call , patch
1111
1212import adafruit_minimqtt .adafruit_minimqtt as MQTT
1313
1414
15- class PortSslSetup ( TestCase ) :
15+ class TestPortSslSetup :
1616 """This class contains tests that verify how host/port and TLS is set for connect().
1717 These tests assume that there is no MQTT broker running on the hosts/ports they connect to.
1818 """
@@ -35,7 +35,7 @@ def test_default_port(self) -> None:
3535 ssl_mock = Mock ()
3636 ssl_context .wrap_socket = ssl_mock
3737
38- with self . assertRaises (MQTT .MMQTTException ):
38+ with pytest . raises (MQTT .MMQTTException ):
3939 mqtt_client .connect ()
4040
4141 ssl_mock .assert_not_called ()
@@ -58,51 +58,51 @@ def test_connect_override(self):
5858 connect_retries = 1 ,
5959 )
6060
61- with self . assertRaises (MQTT .MMQTTException ):
61+ with pytest . raises (MQTT .MMQTTException ):
6262 expected_host = "127.0.0.2"
6363 expected_port = 1884
64- self . assertNotEqual ( expected_port , port , " port override should differ" )
65- self . assertNotEqual ( expected_host , host , " host override should differ" )
64+ assert expected_port != port # port override should differ
65+ assert expected_host != host # host override should differ
6666 mqtt_client .connect (host = expected_host , port = expected_port )
6767
6868 connect_mock .assert_called ()
6969 # Assuming the repeated calls will have the same arguments.
7070 connect_mock .assert_has_calls ([call ((expected_host , expected_port ))])
7171
72- def test_tls_port (self ) -> None :
72+ @pytest .mark .parametrize ("port" , (None , 8883 ))
73+ def test_tls_port (self , port ) -> None :
7374 """verify that when is_ssl=True is set, the default port is 8883
7475 and the socket is TLS wrapped. Also test that the TLS port can be overridden."""
7576 host = "127.0.0.1"
7677
77- for port in [None , 8884 ]:
78- if port is None :
79- expected_port = 8883
80- else :
81- expected_port = port
82- with self .subTest ():
83- ssl_mock = Mock ()
84- mqtt_client = MQTT .MQTT (
85- broker = host ,
86- port = port ,
87- socket_pool = socket ,
88- is_ssl = True ,
89- ssl_context = ssl_mock ,
90- connect_retries = 1 ,
91- )
92-
93- socket_mock = Mock ()
94- connect_mock = Mock (side_effect = OSError )
95- socket_mock .connect = connect_mock
96- ssl_mock .wrap_socket = Mock (return_value = socket_mock )
97-
98- with self .assertRaises (MQTT .MMQTTException ):
99- mqtt_client .connect ()
100-
101- ssl_mock .wrap_socket .assert_called ()
102-
103- connect_mock .assert_called ()
104- # Assuming the repeated calls will have the same arguments.
105- connect_mock .assert_has_calls ([call ((host , expected_port ))])
78+ if port is None :
79+ expected_port = 8883
80+ else :
81+ expected_port = port
82+
83+ ssl_mock = Mock ()
84+ mqtt_client = MQTT .MQTT (
85+ broker = host ,
86+ port = port ,
87+ socket_pool = socket ,
88+ is_ssl = True ,
89+ ssl_context = ssl_mock ,
90+ connect_retries = 1 ,
91+ )
92+
93+ socket_mock = Mock ()
94+ connect_mock = Mock (side_effect = OSError )
95+ socket_mock .connect = connect_mock
96+ ssl_mock .wrap_socket = Mock (return_value = socket_mock )
97+
98+ with pytest .raises (MQTT .MMQTTException ):
99+ mqtt_client .connect ()
100+
101+ ssl_mock .wrap_socket .assert_called ()
102+
103+ connect_mock .assert_called ()
104+ # Assuming the repeated calls will have the same arguments.
105+ connect_mock .assert_has_calls ([call ((host , expected_port ))])
106106
107107 def test_tls_without_ssl_context (self ) -> None :
108108 """verify that when is_ssl=True is set, the code will check that ssl_context is not None"""
@@ -116,10 +116,6 @@ def test_tls_without_ssl_context(self) -> None:
116116 connect_retries = 1 ,
117117 )
118118
119- with self . assertRaises (AttributeError ) as context :
119+ with pytest . raises (AttributeError ) as context :
120120 mqtt_client .connect ()
121- self .assertTrue ("ssl_context must be set" in str (context ))
122-
123-
124- if __name__ == "__main__" :
125- main ()
121+ assert "ssl_context must be set" in str (context )
0 commit comments