Overview
Testing is a part of product life cycle. I wonder is there any tool or programing language which will be fun to work with except python. Since python is grown in such a way that even a non programmer become a programmer. In case of testing team, traditionaly there is a image that they won’t code. But python fills the gap between coding and testing.
Still many people doesn’t know the power of Python and Robotframe work in testing field.
The main objective of this article is to give an Example of an Embedded system and how that can be tested using python and robotframe work.
Actually I am rewriting this article which I posted in stack overflow documentation (How robot framework is used in Automation testing in Embedded Systems?). Since documentation beta is closed by Stack Overflow.
I am writing this article because I like to make testing team to have a taste of coding.
Requirements to build the RPS
Requirements for building a remote power supply are
Remote power supply should be able to turn ON/OFF remotely
Remote power supply status can be accessed remotely.
Remote Power supply simulation
Since we don’t have a real remote power supply hardware, we are going to simulate it using python program.
Basic idea about RPS
-
Actually remote power supply has a http server.
-
User can send commands to turn ON/OFF power supply using http request.
We are going to simulate remote power supply using following program rps-server.py.
from flask import Flask, request
from flask_httpauth import HTTPBasicAuth
app = Flask(__name__)
auth = HTTPBasicAuth()
users = {
'admin': '12345678'
}
app.url_map.strict_slashes = False
PINS = ['P60', 'P61', 'P62', 'P63']
PINS_STATUS = {'P60':'0', 'P61': '0', 'P62':'0', 'P63':'0'}
@auth.get_password
def get_pw(username):
if username in users:
return users.get(username)
return None
@app.route('/')
@auth.login_required
def index():
return "Hello, %s!" % auth.username()
def get_html_string():
html_str = '<html>P60={}P61={}P62={}P63={}</html>'.format(PINS_STATUS['P60'],
PINS_STATUS['P61'],
PINS_STATUS['P62'],
PINS_STATUS['P63'])
return html_str
def parse_cmd_args(args):
global current_status
if str(args['CMD']) == 'SetPower':
for key in args:
if key in PINS:
PINS_STATUS[key] = str(args[key])
return get_html_string()
if str(args['CMD']) == 'GetPower':
return get_html_string()
@app.route('/SetCmd', methods=['GET','POST'])
def rps():
if request.method=="GET":
args=request.args.to_dict()
ret = parse_cmd_args(args)
return ret
How to run rps server ?
$ export FLASK_APP=rps-server.py
$ flask run
How to send commands to rps server ?
Following are the two commands used to control the RPS
1. SetPower
2. GetPower
By default the server will be listening at the port 5000.
The power supply ports are,
P60
P61
P62
P64
The states of the ports are,
ON - 1
OFF - 0
Deriving test cases
Now the RPS server is ready for simulation. We need to write test case to test RPS system.
Test cases derived from requirement
Turn on Power supply 2 remotely.
Verify power supply 2 is on.
Turn off Power supply 2 remotely.
Verify power supply 2 is off.
Manual Testing
-
Run the rps server.
-
To turn on Port 3, open a browser and give following URI
http://admin:12345678@localhost:5000/SetCmd?CMD=SetPower&P62=1
-
To get the status of all the ports
http://admin:12345678@localhost:5000/SetCmd?CMD=GetPower
Writing test library
We need to write a test library in python for sending http commands using http request. Later we will be using this library as keywords in robot frame work.
We are going to use library from commands.py to send SetPower and GetPower.
Source code commands.py
import requests
import re
#http://admin:12345678@192.168.1.100/Set.cmd?CMD=SetPower&P60=1&P61=1&P62=1&P63=0
class commands(object):
ROBOT_LIBRARY_SCOPE = 'GLOBAL' #
def __init__(self, ip='localhost:5000'):
self.ip_address = ip
self.query = {}
self.user = 'admin'
self.passw = '12345678'
def form_query(self, state, cmd, port):
port = self.get_port_no(port)
self.query = {port: state}
return self.query
def get_port_no(self, port_no):
port = 'P6' + str(port_no)
return port
def clean_html(self, data):
exp = re.compile('<.*?>')
text = re.sub(exp, "", data)
return text.rstrip()
def send_cmds(self, cmd, port=None, state=None): #
url = 'http://{}:{}@{}/SetCmd?CMD={}'\
.format(self.user,
self.passw,
self.ip_address,
cmd)
print url
if cmd == 'SetPower':
self.form_query(state, cmd, port)
self.req = requests.get(url, params=self.query)
return True
elif cmd == 'GetPower':
self.req = requests.get(url)
data = self.clean_html(self.req.text)
return data
else:
return False
return self.req.text
# c = commands('localhost:5000')
# c.send_cmds('SetPower', 2, 1)
# c.send_cmds('SetPower', 3, 1)
# print c.send_cmds('GetPower')
The scope of the variable will available through all test case | |
Send Cmds is the key word we are going to use. |
Python Keyword documentation for send_cmds
-
send_cmds(cmd, port=None, state=None)
is the function we are going to use. -
While using this function in Robot key word, no need to bother about
_
, orLowercaser
orUppercase
in function name.
Robot keyword derived from python function
Send Cmds cmd port state
Writing Robot keyword using Python Library
We are going to use Send Cmds
as python keyword in test suite.
-
RPS send commands uses following three arguments to set power
-
command = SetPower
-
port = 2
-
state = 1 for ON / 0 for off When we call that command it will turn ON/OFF the power supply
-
*** Keywords ***
RPS send commands
[Arguments] ${command} ${port} ${state}
${output}= Send cmds ${command} ${port} ${state}
[return] ${output}
RPS get Power
[Arguments] ${command}
${output}= Send cmds ${command}
[return] ${output}}
Algorithm to test power supply
-
Set power to a port
-
Check the status of cmd
-
Get the status of the port and check whether it is ON/OFF
Writing Test Suite using test case we derived
Now we are ready to write test suite using following two keywords
-
RPS send commands - To set and unset a power of port
-
RPS get power - To get the status of all the port
*** Settings ***
Library ../library/commands.py # Import the library
*** Test Cases ***
Turn on Power supply 2 remotely
${out}= RPS send commands SetPower 2 1
Should be equal ${out} ${True}
Verify power supply 2 is on
${out}= RPS get power GetPower
should contain ${out} P62=1
Turn off Power supply 2 remotely
${out}= RPS send commands SetPower 2 0
Should be equal ${out} ${True}
Verify power supply 2 is off
${out}= RPS get power GetPower
should contain ${out} P62=0
*** Keywords ***
RPS send commands
[Arguments] ${command} ${port} ${state}
${output}= Send cmds ${command} ${port} ${state}
[return] ${output}
RPS get Power
[Arguments] ${command}
${output}= Send cmds ${command}
[return] ${output}}
Note: A keywords can be put as seprate a file and import in test suite
Credits
-
I like to thank VVDN Technologies PVT Ltdfor providing me the VVDN labs to explore on RPS.