Path to Robot Framework libraries and resources

Introduction

From this article you will learn how to explicitly specify the path to resources and external Python libraries in Robot Framework.

This will be especially useful for those who write their own libraries for the robot and/ or use a complex structure with a large number of nestings.

I recommend that you first read the articles about test architecture and about connecting external Python libraries

In the example from the article «Connecting external Python libraries» the libraries were in the directory libraries and the tests in tests

ext_py_lib/ |-- browser |-- libraries | `-- unstable_url.py `-- tests `-- test_title.robot

Therefore, the library import looked like this original way

Library ../libraries/unstable_url.py

If subdirectories appear in the tests directory, the import from there will look like this

Library ../../libraries/unstable_url.py

or even like that

Library ../../../libraries/unstable_url.py

To avoid doing a relative import, you can use one of the methods described below.

--pythonpath

Using the --pythonpath option, you can temporarily add the path to the libraries to PYTHONPATH

Starting with this example, we will specify libraries without .py

Library unstable_url

robot --pythonpath /c/Users/Andrei/robot/ext_py_lib/libraries test_unstable_title.robot

--argumentfile

To avoid entering the path manually, you can save it in the so-called argumentfile

Let's create a file all_arguments.robot and add the following code there

--pythonpath /c/Users/Andrei/robot/ext_py_lib/libraries test_unstable_title.robot

To use this file, we will use the --argumentfile option.

robot --argumentfile all_arguments.robot test_unstable_title.robot

Official documentation

Dockerfile

If your environment is created using Docker , then the path to the libraries can be specified in Dockerfile

An example that works in one of my projects:

… ENV TEST_DIR=/opt ENV PYTHON_DIR=/user/local/bin/python ENV ROBOT_RESOURCES=/opt/robot/src/resources ENV ROBOT_LIBRARIES=/opt/robot/src/libraries ENV PYTHONPATH=$TEST_DIR:$PYTHON_DIR:$ROBOT_RESOURCES:$ROBOT_LIBRARIES …

Article author: Andrei Olegovich