Permalink
Browse files

Initial commit with connect, publish and disconnect keywords

  • Loading branch information...
0 parents commit a0e1995b1acb2e8f86720867de9ba3feaea7aa58 @randomsync committed Feb 18, 2015
Showing with 512 additions and 0 deletions.
  1. +42 −0 .gitignore
  2. +2 −0 MANIFEST.in
  3. +15 −0 README.rst
  4. +301 −0 doc/MQTTLibrary.html
  5. +2 −0 requirements.txt
  6. +34 −0 setup.py
  7. +62 −0 src/MQTTLibrary/MQTTKeywords.py
  8. +13 −0 src/MQTTLibrary/__init__.py
  9. +1 −0 src/MQTTLibrary/version.py
  10. +27 −0 tests/keywords.txt
  11. +13 −0 tests/publish.txt
@@ -0,0 +1,42 @@
+# Backup files
+*.~
+
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+
+# C extensions
+*.so
+
+# Distribution / packaging
+bin/
+build/
+develop-eggs/
+dist/
+eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+*.egg-info/
+.installed.cfg
+*.egg
+MANIFEST
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+.tox/
+.coverage
+.cache
+nosetests.xml
+coverage.xml
+
+# Translations
+*.mo
+
+# Sphinx documentation
+docs/_build/
@@ -0,0 +1,2 @@
+include MANIFEST.in
+include *.txt
@@ -0,0 +1,15 @@
+robotframework-mqttlibrary
+===================
+
+MQTT keyword library for Robot Framework
+
+This is an initial POC...stay tuned for updates.
+
+Usage
+-----
+
+Detailed instructions coming soon...
+
+Reference
+----------
+[1] https://github.com/mqtt/mqtt.github.io/wiki/servers
Oops, something went wrong.
@@ -0,0 +1,2 @@
+paho-mqtt
+robotframework
@@ -0,0 +1,34 @@
+from setuptools import setup
+
+from os.path import abspath, dirname, join
+
+here = dirname(abspath(__file__))
+
+
+# Get version
+execfile(join(here, 'src', 'MQTTLibrary', 'version.py'))
+
+# Get the long description
+with open(join(here, 'README.rst')) as f:
+ long_description = f.read()
+
+setup(
+ name = 'robotframework-mqttlibrary',
+ version = VERSION,
+ description = 'MQTT Keyword Library Robot Framework',
+ long_description = long_description,
+ url = 'https://github.com/randomsync/robotframework-mqttlibrary',
+ author = 'Gaurav Gupta',
+ author_email = 'gaurav@randomsync.net',
+ license = 'Apache License 2.0',
+ classifiers = [
+ 'Development Status :: 1 - Planning',
+ 'License :: OSI Approved :: Apache Software License',
+ 'Programming Language :: Python :: 2',
+ 'Topic :: Software Development :: Testing',
+ ],
+ keywords = 'robotframework testing testautomation mqtt',
+ package_dir = {'': 'src'},
+ packages = ['MQTTLibrary'],
+ install_requires = ['robotframework', 'paho-mqtt'],
+)
@@ -0,0 +1,62 @@
+import paho.mqtt.client as mqtt
+import robot
+
+from robot.libraries.BuiltIn import BuiltIn
+
+class MQTTKeywords(object):
+ ROBOT_LIBRARY_SCOPE = 'Global'
+
+ def __init__(self):
+ self.builtin = BuiltIn()
+ #self.mqttc = mqtt.Client()
+
+ def connect(self, broker, port=1883, client_id=""):
+
+ """ Connect to an MQTT broker. This is a pre-requisite step for publish and subscribe keywords.
+
+ `broker` MQTT broker host
+
+ `port` broker port (default 1883)
+
+ `client_id` if not specified, a random id is generated
+
+ Example:
+ | Connect | 127.0.0.1 | 1883 | test.client |
+
+ """
+ self.builtin.log('Connecting to broker: %s' % broker, 'INFO')
+ self.mqttc = mqtt.Client(client_id)
+ self.mqttc.connect(broker, int(port))
+
+
+ def publish(self, topic, message=None, qos=0, retain=False):
+
+ """ Publish a message to a topic with specified qos and retained flag.
+
+ `topic` topic to which the message will be published
+
+ `message` message payload to publish
+
+ `qos` qos of the message
+
+ `retain` retained flag
+
+ Example:
+ | Publish | test/test | test message | 1 | ${false} |
+
+ """
+ self.builtin.log('Publish topic: %s, message: %s, qos: %s, retain: %s' % (topic, message, qos, retain), 'INFO')
+ self.mqttc.publish(topic, message, int(qos), retain)
+
+ def disconnect(self):
+
+ """ Disconnect from MQTT Broker.
+
+ Example:
+ | Disconnect |
+
+ """
+ self.mqttc.disconnect()
+
+
+
@@ -0,0 +1,13 @@
+from MQTTKeywords import MQTTKeywords
+from version import VERSION
+
+_version_ = VERSION
+
+
+class MQTTLibrary(MQTTKeywords):
+
+ """ A keyword library for Robot Framework. It provides keywords for performing various operations on an MQTT broker. See http://mqtt.org/for more details on MQTT specification.
+
+ """
+
+ ROBOT_LIBRARY_SCOPE = 'GLOBAL'
@@ -0,0 +1 @@
+VERSION = '0.1.0.dev1'
@@ -0,0 +1,27 @@
+| *Settings* | *Value*
+| Library | MQTTLibrary
+| Library | BuiltIn
+
+| *Variables* | *Value*
+| ${broker.uri} | 127.0.0.1
+| ${broker.port} | 1883
+| ${client.id} | mqtt.test.client
+| ${topic} | test/mqtt_test
+| ${sub.topic} | test/mqtt_test_sub
+
+| *Keywords* |
+| Publish to MQTT Broker and Disconnect
+| | [Arguments] | ${broker.uri}=${broker.uri} | ${port}=${broker.port}
+| | ... | ${client.id}=${client.id} | ${topic}=${topic}
+| | ... | ${message}=${EMPTY} | ${qos}=0
+| | ... | ${retention}=${false}
+| | Connect | ${broker.uri} | ${broker.port} | ${client.id}
+| | Publish | ${topic} | ${message} | ${qos} | ${retention}
+| | Disconnect |
+
+| Subscribe to MQTT Broker and Validate
+| | [Arguments] | ${broker.uri}=${broker.uri} | ${client.id}=${client.id}
+| | ... | ${topic}=${sub.topic} | ${message}=${EMPTY}
+| | ... | ${timeout}=1000
+| | Subscribe and Validate
+| | ... | ${broker.uri} | ${client.id} | ${topic} | ${message} | ${timeout}
@@ -0,0 +1,13 @@
+| *Settings* | *Value*
+| Resource | keywords.txt
+| Test Timeout | 30 seconds
+
+| *Test Cases*
+| Publish a non-empty message
+| | ${time} | Get Time | epoch
+| | ${message} | Catenate | test message | ${time}
+| | Publish to MQTT Broker and Disconnect | message=${message}
+
+| Publish an empty message
+| | Publish to MQTT Broker and Disconnect
+

0 comments on commit a0e1995

Please sign in to comment.