Reputation: 356
Good morning,
I've seen in the RobotFramework documentation that it is possible to create keyword in python directly (http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#creating-user-keywords)
for instance the do_nothing function.
def hello(name):
print "Hello, %s!" % name
def do_nothing():
pass
It works very well, but the issue is that this has to be added in the variable PYTHONPASS, or in the directory that contains the file.
The issue is that the tests can be performed on more than one computer, and it means that the this variable should be modified on every computer that should run the tests.
Second point, unfortunately my file will be used in several dir.
eg:
A
A/B
A/B/C <== use here
A/B/D
A/B/E <== use here
...
and so one..
Is it possible to add directly the function at A level only once for instance?
Or is it possible to add it once in the section Keywords of RobotFramework at the A directory level and reference it directly in C and E robot files:
A/lib.robot:
*** Keywords ***
def hello(name):
print "Hello, %s!" % name
def do_nothing():
pass
C and E:
*** Settings ***
Resource ../../A/lib.robot
Upvotes: 0
Views: 3088
Reputation: 386342
You cannot put python-based keywords in a robot file. Your only choice is to put it in a .py file. You have two choices to import that library: import it via the path to the file (eg: Library the_library.py
), or import it by module name (eg: Library the_library
).
If you use the path, the path can be relative or absolute. For example, if you need to use the library in A/B/C and A/B/E, you could put the file in A and then reference it as ../../the_library.py.
If you import it by module name, like with any python module it must be in your PYTHONPATH. There are no exceptions. If you import it by filename, you must specify a filename that actually points to the file. Those are your only two options.
The most common solution is to put the file in the same location on all machines that use it. Then, no tests have to be modified to work on different machines. It also means that every machine can define PYTHONPATH the same.
The bottom line is pretty straight forward: your tests either have to provide the proper path to the library, or the folder that contains the library must be on PYTHONPATH. Those are the only two options.
This is fully documented in the user guide, in a sections titled Importing Libraries and Configuring where to search libraries and other extensions
Upvotes: 8