Skip to content

Commit 7659f21

Browse files
committed
Standardize tests
1 parent 769e723 commit 7659f21

File tree

4 files changed

+48
-62
lines changed

4 files changed

+48
-62
lines changed

tests/backoff_test.py renamed to tests/test_backoff.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
"""exponential back-off tests"""
66

7+
import pytest
78
import socket
89
import ssl
910
import time
10-
from unittest import TestCase, main
1111
from unittest.mock import call, patch
1212

1313
import adafruit_minimqtt.adafruit_minimqtt as MQTT
1414

1515

16-
class ExpBackOff(TestCase):
16+
class TestExpBackOff:
1717
"""basic exponential back-off test"""
1818

1919
connect_times = []
@@ -42,9 +42,9 @@ def test_failing_connect(self) -> None:
4242
connect_retries=connect_retries,
4343
)
4444
print("connecting")
45-
with self.assertRaises(MQTT.MMQTTException) as context:
45+
with pytest.raises(MQTT.MMQTTException) as context:
4646
mqtt_client.connect()
47-
self.assertTrue("Repeated connect failures" in str(context.exception))
47+
assert "Repeated connect failures" in str(context)
4848

4949
mock_method.assert_called()
5050
calls = [call((host, port)) for _ in range(0, connect_retries)]
@@ -53,7 +53,3 @@ def test_failing_connect(self) -> None:
5353
print(f"connect() call times: {self.connect_times}")
5454
for i in range(1, connect_retries):
5555
assert self.connect_times[i] >= 2**i
56-
57-
58-
if __name__ == "__main__":
59-
main()

tests/test_loop.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
"""loop() tests"""
66

7+
import pytest
78
import random
89
import socket
910
import ssl
1011
import time
1112
import errno
1213

13-
from unittest import TestCase, main
1414
from unittest.mock import patch
1515
from unittest import mock
1616

@@ -101,7 +101,7 @@ def recv_into(self, retbuf, bufsize):
101101
raise exc
102102

103103

104-
class Loop(TestCase):
104+
class TestLoop:
105105
"""basic loop() test"""
106106

107107
connect_times = []
@@ -173,10 +173,10 @@ def test_loop_timeout_vs_socket_timeout(self):
173173
)
174174

175175
mqtt_client.is_connected = lambda: True
176-
with self.assertRaises(MQTT.MMQTTException) as context:
176+
with pytest.raises(MQTT.MMQTTException) as context:
177177
mqtt_client.loop(timeout=0.5)
178178

179-
assert "loop timeout" in str(context.exception)
179+
assert "loop timeout" in str(context)
180180

181181
def test_loop_is_connected(self):
182182
"""
@@ -189,10 +189,10 @@ def test_loop_is_connected(self):
189189
ssl_context=ssl.create_default_context(),
190190
)
191191

192-
with self.assertRaises(MQTT.MMQTTException) as context:
192+
with pytest.raises(MQTT.MMQTTException) as context:
193193
mqtt_client.loop(timeout=1)
194194

195-
assert "not connected" in str(context.exception)
195+
assert "not connected" in str(context)
196196

197197
# pylint: disable=no-self-use
198198
def test_loop_ping_timeout(self):
@@ -258,7 +258,3 @@ def test_loop_ping_vs_msgs_sent(self):
258258

259259
# This means no other messages than the PUBLISH messages generated by the code above.
260260
assert len(mocket.sent) == i * (2 + 2 + len(topic) + len(message))
261-
262-
263-
if __name__ == "__main__":
264-
main()

tests/test_port_ssl.py

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
"""tests that verify the connect behavior w.r.t. port number and TLS"""
66

7+
import pytest
78
import socket
89
import ssl
9-
from unittest import TestCase, main
1010
from unittest.mock import Mock, call, patch
1111

1212
import 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)

tox.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ envlist = py311
1010
description = run tests
1111
deps =
1212
pytest==7.4.3
13-
pytest-subtests==0.11.0
1413
commands = pytest
1514

1615
[testenv:coverage]
1716
description = run coverage
1817
deps =
1918
pytest==7.4.3
2019
pytest-cov==4.1.0
21-
pytest-subtests==0.11.0
2220
package = editable
2321
commands =
2422
coverage run --source=. --omit=tests/* --branch {posargs} -m pytest

0 commit comments

Comments
 (0)