Skip to content
This repository was archived by the owner on Nov 16, 2020. It is now read-only.

Commit f69f155

Browse files
authored
mqtt test script
1 parent 1ef2421 commit f69f155

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

unittest/mqtt_rule_test.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/python
2+
#
3+
# NOTE : DO NOT NAME YOUR UNITTEST SCRIPT AS "unittest.py". DELETE "unittest.pyc".
4+
# You will see "AttributeError: 'module' object has no attribute 'TestCase'" Error.
5+
#
6+
import unittest
7+
from mock import Mock
8+
import paho.mqtt.client as mqtt
9+
import sys
10+
#import shutil
11+
import time
12+
13+
def on_message(client, userdata, message):
14+
print("message received " ,str(message.payload.decode("utf-8")))
15+
print("message topic=",message.topic)
16+
print("message qos=",message.qos)
17+
print("message retain flag=",message.retain)
18+
print message
19+
20+
21+
############
22+
class mqtt_rule_test(unittest.TestCase):
23+
targetHost = "127.0.0.1"
24+
# Not used for this test. Focus on MQTT message payload from MQTT broker
25+
targetPort = 1883
26+
targetKeepalive = 60
27+
targetRetain = False
28+
targetQos = 0
29+
30+
@classmethod
31+
def setUpClass(self):
32+
self.mock_callback = Mock()
33+
self.client = mqtt.Client("P1") #create new instance
34+
#self.client.on_message = on_message #attach function to callback
35+
self.client.on_message = self.mock_callback
36+
self.client.connect(self.targetHost) #connect to broker
37+
self.client.loop_start() #start the loop
38+
print("Subscribing to topic","city/#")
39+
self.client.subscribe("city/#")
40+
41+
@classmethod
42+
def tearDownClass(self):
43+
self.client.loop_stop() #stop the loop
44+
45+
def setUp(self):
46+
pass
47+
48+
def tearDown(self):
49+
pass
50+
51+
def test_1_no_record_no_action(self):
52+
self.mock_callback.reset_mock()
53+
print("Publishing message to topic","city/building14/floor1/temperature")
54+
self.client.publish("city/building14/floor1/temperature","77")
55+
time.sleep(5)
56+
self.assertTrue(self.mock_callback.called)
57+
name, args, kwargs = self.mock_callback.mock_calls[0] # or args, kwargs = self.mock_callback.call_args
58+
# args[2] : MQTT Message defined in https://github.com/eclipse/paho.mqtt.python/blob/master/src/paho/mqtt/client.py
59+
self.assertEqual( args[2].payload , "77")
60+
61+
def test_2_no_action(self):
62+
self.mock_callback.reset_mock()
63+
print("Publishing message to topic","city/building11/floor1/temperature")
64+
self.client.publish("city/building11/floor1/temperature","78")
65+
time.sleep(5)
66+
self.assertTrue(self.mock_callback.called)
67+
name, args, kwargs = self.mock_callback.mock_calls[0]
68+
# args[2] : MQTT Message
69+
self.assertEqual( args[2].payload , "78")
70+
71+
def test_3_filter_ignore(self):
72+
self.mock_callback.reset_mock()
73+
print("Publishing message to topic","city/building12/floor1/temperature")
74+
self.client.publish("city/building12/floor1/temperature","99")
75+
time.sleep(5)
76+
self.assertFalse(self.mock_callback.called)
77+
78+
def test_4_filter_warn(self):
79+
self.mock_callback.reset_mock()
80+
print("Publishing message to topic","city/building12/floor1/temperature")
81+
self.client.publish("city/building12/floor1/temperature","110")
82+
time.sleep(5)
83+
self.assertTrue(self.mock_callback.called)
84+
name, args, kwargs = self.mock_callback.mock_calls[0]
85+
# args[2] : MQTT Message
86+
self.assertEqual( args[2].payload , "{ 'temperature' : 110 }")
87+
88+
def test_5_convert_to_percentage(self):
89+
self.mock_callback.reset_mock()
90+
print("Publishing message to topic","city/building12/floor2/humidity")
91+
self.client.publish("city/building12/floor2/humidity","1012")
92+
time.sleep(5)
93+
self.assertTrue(self.mock_callback.called)
94+
name, args, kwargs = self.mock_callback.mock_calls[0]
95+
# args[2] : MQTT Message
96+
self.assertEqual( args[2].payload , "{ 'humidity' : 49 }")
97+
98+
if __name__ == '__main__':
99+
suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__] )
100+
unittest.TextTestRunner(verbosity=3).run(suite)
101+

0 commit comments

Comments
 (0)