Table of Contents
Open the command prompt and type python. On Windows you can also start the interpreter by selecting Start > All Programs > Python 2.x.
Statements and expressions can be written in the interpreter. Pressing enter will interpret the line and possible results are echoed. Try for example:
>>> 1 + 2
3
Use Ctrl-D to exit on UNIX like machines and Ctrl-Z and enter on Windows.
Dive Into Python has some more examples: http://diveintopython.net/installing_python/shell.html
Python has strings, integers, floating point numbers, Boolean values (True and False) similarly as most other programming languages.
Strings can be enclosed into double or single quotes. Different quotest do not have any difference like they do for example in Perl.
Unicode strings have a special syntax like u"Hyv\xE4\xE4 y\xF6\t\xE4!". Using Unicode with Python is not covered otherwise in this tutorial.
None is a special value meaning nothing similarly as null in Java.
Try at least these on the interpreter:
>>> 2 * 2.5
5.0
>>> 'This is easy'
'This is easy'
>>> "Ain't it"
"Ain't it"
All different values can be assigned to variables. Valid characters in variable identifiers are letters, underscore, and numbers, although numbers cannot start the variable name.
A variable needs not to be declared, it starts to exist when a value is assigned for the first time.
There is no need to specify the variable type either as the type is got from the assigned variable automatically.
Try it out:
>>> a = 3
>>> a
3
>>> b = 4
>>> a*b
12
>>> greeting = 'Hello'
>>> greeting
'Hello'
>>> greeting.upper()
'HELLO'
It is even possible to assign multiple variables at once:
>>> x, y = 'first', 'second'
>>> x
'first'
>>> y
'second'
Create a file hello.py with your editor of choice and write this content into it:
print "Hello, world!"
Then execute the file on the console like this:
python hello.py
As a result you should get Hello, world! printed into the screen. With Robot Framework keywords such messages would end up into the log file.
For more interesting examples see Dive Into Python: http://diveintopython.net/getting_to_know_python/index.html
Creating functions in Python is super easy. This example uses the interpreter, but you can also write the code into the previous hello.py file and execute it.
>>> def hello():
... print "Hello, world!"
...
>>> hello()
Hello, world!
Note that in Python code blocks must be indented (four spaces is the norm and highly recommended) and you close the block simply by returning to the earlier indentation level. Inside a block you must use the indentation level consistently.
Notice also that this hello function is actually already a valid keyword for Robot Framework!
A function with arguments is not that more complicated:
>>> def hello(name):
... print "Hello, %s!" % name
...
>>> hello("Python")
Hello, Python!
>>> hello("Robot Framework")
Hello, Robot Framework!
The hard part in this example is string formatting (i.e. "Hello, %s!" % name) which uses similar syntax as for example C language. More information about it can be found e.g. from Dive Into Python: http://diveintopython.net/native_data_types/formatting_strings.html
Functions can have default values for some or all of its arguments:
>>> def hello(name="World"):
... print "Hello, %s!" % name
...
>>> hello()
Hello, World!
>>> hello("Robot")
Hello, Robot!
If there are several optional arguments, it is also possible to specify only some of them by giving their name along with the value as the example below illustrates. Those arguments that do not have default values cannot be omitted.
>>> def test(a, b=1, c=2, d=3):
... print a, b, c, d
...
>>> test(0)
0 1 2 3
>>> test(0, 42)
0 42 2 3
>>> test(1, c=10)
1 1 10 3
>>> test(2, c=100, d=200)
2 1 100 200
>>> test(b=0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: test() takes at least 1 non-keyword argument (0 given)
Robot Framework keywords can have default values but they are always used with positional arguments. For example, if the above hello method was used as a keyword, it could be used with zero or one argument, and test could be used with one to four arguments.
Dive Into Python explains both optional and named arguments very well: http://diveintopython.net/power_of_introspection/optional_arguments.html
>>> def example(arg1, arg2, *rest): ... print arg1, arg2, rest ... >>> example(1, 2) 1 2 () >>> example(1, 2, 3) 1 2 (3,) >>> example(1, 2, 3, 4, 5) 1 2 (3, 4, 5)
Functions can use return statement to return values that can be assigned to variables or passed to other functions:
>>> def multiply_by_two(number):
... return number * 2
...
>>> result = multiply_by_two(10)
>>> result
20
>>> result = multiply_by_two(multiply_by_two(2))
>>> result
8
Robot Framework keywords can also return values that can be assigned to variables and then used as arguments to other keywords.
In Python functions, as well as classes and modules, are documented with so called doc strings:
>>> def hello():
... """Prints 'Hello, world!' to the standard output."""
... print "Hello, world!"
...
Interestingly the documentation is available dynamically:
>>> print hello.__doc__
Prints 'Hello, world!' to the standard output.
Doc strings are covered pretty well in Dive Into Python: http://diveintopython.net/getting_to_know_python/documenting_functions.html
Robot Framework has libdoc.py tool that can generate test library documentation based on these doc strings. Documenting functions that are used as keywords is thus very important.
A list is an ordered collection of items which you normally access by index.
They also have handy methods like append, insert and pop to access or alter the list.
>>> x = ['Some', 'strings', 'here']
>>> x[0]
'Some'
>>> x[1]
'strings'
>>> x[-1]
'here'
>>> x[2] = x[2].upper()
>>> x.append(42)
>>> x
['Some', 'strings', 'HERE', 42]
A tuple is a list like structure which you cannot alter after creating it.
>>> t = (1, 2, 'x')
>>> t[0]
1
>>> t[-1]
'x'
>>> t[0] = 'new value'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
Notice that you must use a trailing comma to create a tuple with one element:
>>> empty = ()
>>> one = (1,)
>>> two = (1, 2)
A dictionary is an unordered collection of key-value pairs. The same data structure is often called hashmap.
>>> d = {'x': 'some value', 'a': 1, 'b': 2}
>>> d['a']
1
>>> d['x']
'some value'
>>> d['a'] = d['b']
>>> d['tuple'] = t
>>> d
{'a': 2, 'x': 'some value', 'b': 2, 'tuple': (1, 2, 'x')}
>>> 'x' in d
True
>>> 'z' in d
False
Python has similar if/elif/else structure as most other programming languages.
Notice that no parentheses are needed around the expression as in Java or C.
def is_positive(number):
if number > 0:
return True
return False
def greet(name, time):
if 7 < time < 12:
print 'Good morning %s' % name
elif time < 18:
print 'Good afternoon %s' % name
elif time < 23:
print 'Good night %s' % name
else:
print '%s, you should be sleeping!' % name
for loops allow iterating over a sequence of items such as a list. This is probably the loop you are going to use most often.
def greet_many(names):
for name in names:
print 'Hello %s' % name
def count_up(limit):
for num in range(1, limit+1):
if num == limit:
print 'bye!'
else:
print num
while loops iterate as long as given expression is true. Very handy when waiting some event to occur.
def wait_until_message_received():
msg = try_to_receive_message()
while msg is None:
time.sleep(5)
msg = try_to_receive_message()
return msg
Both for and while loops have typical continue and break statements that can be used to end the current iteration or exit the loop altogether.
For more examples and information see:
Quite often for loops can be replaced with even more concise list comprehensions or generator expressions:
>>> numbers = [1, -5, 4, -32, 0, 42]
>>> positive = [ num for num in numbers if num > 0 ]
>>> positive
[1, 4, 42]
>>> sum(num * 2 for num in positive)
94
This syntax might look a bit strange at first but you will love it very soon. To learn more see, for example, Dive Into Python: http://diveintopython.net/native_data_types/mapping_lists.html
Because every .py file is effectively a Python module, you have already created at least hello module.
For example if we have the following code in a file called example.py:
def hello(name="World"):
print "Hello, %s!" % name
if __name__ == "__main__":
hello()
then we can use it in the interpreter (or from other modules) like:
>>> import example
>>> example.hello("Tellus")
Hello, Tellus!
if __name__ == "__main__" block in the previous example is important because it allows executing the file also as a script like python example.py.
The automatic __name__ attribute (Python has many of these as you will see if you study it more) gets value "__main___" when the file is run as a script and the if block is thus executed only in that case.
Bigger modules can be organized into several files inside a higher level module as submodules. In this case the higher level module is a directory with a special ___init___.py file.
For more information about modules see Python Tutorial: http://docs.python.org/tutorial/modules.html
Python is an object-oriented language but as we have seen you do not need to use classes everywhere like you need to with Java. It is totally fine to just have a module with functions if that suites your needs, but object oriented features are often really handy.
The syntax for creating classes and then instances from them is relatively straightforward:
>>> class MyClass:
... def __init__(self, name):
... self._name = name
... def hello(self, other="World"):
... print "%s says hello to %s." % (self._name, other)
...
>>> c = MyClass('Robot')
>>> c.hello()
Robot says hello to World.
>>> c.hello('Tellus')
Robot says hello to Tellus.
The only surprising part in the syntax is that every class method must have self as the first argument in the signature. After you create an instance of the class Python binds the method, and it also takes care of passing the self argument automatically so you do not use it when calling the method.
To learn more about classes you can follow an interesting example from Dive Into Python and/or study detailed information from Python Tutorial:
Python has an exception system similar to many other languages. Exceptions are classes and the normal way to raise them is raise SomeException("Error message").
Exceptions are handled in a try/except block:
try:
f = open(path)
except IOError, err:
print "Opening file %s for reading failed: %s" % (path, err)
The try/except block can have multiple except branches, an optional else to execute if no exception occurred, and finally to execute both when an exception occurred and when it did not.
Compared to Java there are some terminology differences (raise vs. throw and except vs. catch) but the biggest real difference is that there are no checked exceptions. This means that you do not need to add throws SomeException to methods that may raise an exception.
More information can be found, for example, from Dive Into Python: http://diveintopython.net/file_handling/index.html
Exceptions are an important part of the Robot Framework Library API because keywords use them to communicate failures to the framework.
Robot Framework's test library API is really simple. It is explained fully in Robot Framework User Guide and this tutorial only covers the very basic features with an executable example.
The test library can be either a module or a class. In case of a module, a keyword will be created for each top-level function in the module. In case of a class, a keyword will be created for each public method of the class.
The most important ways keywords can interact with the framework have already been covered in this tutorial:
The example library and associated test data shown below demonstrate the most important features of the test library API. You can execute these test cases in your own environment and edit them to test also other features. A precondition is having Robot Framework installed, but then you only need to get the library and the data, and run command pybot example_tests.tsv.
def simple_keyword():
"""Log a message"""
print 'You have used the simplest keyword.'
def greet(name):
"""Logs a friendly greeting to person given as argument"""
print 'Hello %s!' % name
def multiply_by_two(number):
"""Returns the given number multiplied by two
The result is always a floating point number.
This keyword fails if the given `number` cannot be converted to number.
"""
return float(number) * 2
def numbers_should_be_equal(first, second):
print '*DEBUG* Got arguments %s and %s' % (first, second)
if float(first) != float(second):
raise AssertionError('Given numbers are unequal!')
*Settings* | |||
Library | ExampleLibrary | ||
*Test Cases* | |||
Simple Test | |||
Simple Keyword | |||
Greet | Robot Framework | ||
Greet | World | ||
Returning Value | |||
${result} = | Multiply By Two | 4.1 | |
Numbers Should Be Equal | ${result} | 8.2 | |
Failing Test | |||
Numbers Should Be Equal | 2 | 2 | |
Numbers Should Be Equal | 2 | 3 |