31
loading...
This website collects cookies to deliver better user experience
"The Bluetooth communication between our connected frige and our PC-based software is gettting better and better. I have a Python library that handles everything! Now, I would like to write tests for it."
"You should try Robot Framework, it seems promising!"
"OK, let's do this!"
Robot Framework is a generic open source automation framework. It can be used for test automation and robotic process automation (RPA).
[...]
Robot Framework itself is open source software released under Apache License 2.0, and most of the libraries and tools in the ecosystem are also open source. The framework was initially developed at Nokia Networks and was open sourced in 2008.
$ pip install robotframework
*** Test Cases ***
Valid Login
Open Browser To Login Page
Input Username demo
Input Password mode
Submit Credentials
Welcome Page Should Be Open
[Teardown] Close Browser
*** Keywords ***
Input Credentials
[Arguments] ${username} ${password}
Input Username ${username}
Input Password ${password}
*** Test Cases ***
Valid Login
Open Browser To Login Page
Input Credentials demo mode
Submit Credentials
Welcome Page Should Be Open
[Teardown] Close Browser
Its capabilities can be extended by libraries implemented with Python or Java.
The simplest approach is having a module (in Python) or a class (in Python or Java) with methods which map directly to keyword names. Keywords also take the same arguments as the methods implementing them. Keywords report failures with exceptions, log by writing to standard output and can return values using the return statement.
_
in their names are hidden in Robot Framework.input_credentials()
can be used in Robot Framework as Input Credentials
, input credentials
, Input credentials
or even input_credentials
(it's up to you).fridge.py
:import robot.api.logger
import robot.utils.asserts
# Variable that is supposed to be in the fridge firmware
_temperature_setpoint = 5
def _ble_send_setpoint(value):
"""Pretend to send data to the fridge with the new temperature setpoint.
:param value: the setpoint value
"""
global _temperature_setpoint
_temperature_setpoint = value
print('Sending setpoint to the fridge: ', _temperature_setpoint)
def _ble_read_setpoint():
"""Pretend to read data to the fridge to get the temperature setpoint.
:return: the setpoint value
"""
global _temperature_setpoint
print('Reading setpoint to the fridge: ', _temperature_setpoint)
return _temperature_setpoint
def change_temperature_setpoint(value):
"""Function exposed as a keyword to change the temperature setpoint.
:param value: the setpoint value
"""
value = float(value)
robot.api.logger.info('Change temperature setpoint to %f' % value)
_ble_send_setpoint(value)
def check_temperature_setpoint(expected):
"""Function exposed as a keyword to check the temperature setpoint.
:param expected: the expected setpoint value
"""
expected = float(expected)
actual = _ble_read_setpoint()
robot.utils.asserts.assert_equal(expected, actual)
float()
. Indeed, arguments are passed from Robot Framework to Python as strings by default and this is not what we want. Hence we simply convert those strings to numbers.*** Settings ***
Library fridge.py
*** Test Cases ***
It is possible to change the temperature setpoint of the fridge
change_temperature_setpoint 4
check_temperature_setpoint 4
change temperature setpoint 8
Check temperature setpoint 8
Change_Temperature SETPOINT 3.5
CHECK temperature_SETpoint 3.5
tests.robot
(which will be considered as a test suite named Tests) and execute them in the terminal:$ robot tests.robot
==============================================================================
Tests
==============================================================================
It is possible to change the temperature setpoint of the fridge | PASS |
------------------------------------------------------------------------------
Tests | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output: C:\...\output.xml
Log: C:\...\log.html
Report: C:\...\report.html
fridge.py
:def connect():
"""Pretend to connect to the fridge."""
robot.api.logger.info('Connect to the frige')
def disconnect():
"""Pretend to disconnect from the fridge."""
robot.api.logger.info('Disconnect from the frige')
*** Settings ***
Library fridge.py
Test Setup Connect
Test Teardown Disconnect
*** Test Cases ***
It is possible to change the temperature setpoint of the fridge
Change temperature setpoint 6
Check temperature setpoint 6
my_module.py
and to import it with the command Library my_module.py
in the section *** Settings ***
of your test file. All functions in my_module.py
which names don't start with an underscore will be available as keywords.