PyCharm's support for Robot Framework
Introduction
This article will show you how to run tests written in Robot Framework
in PyCharm.
First, we will demonstrate the selection of
Python interpreter
,
then
editing the configuration
We will also consider support for Robot Framework syntax and imports in
PyCharm
Support for .robot files
To highlight the syntax of .robot files and drill deeper into the code,
simply install the Intellibot or Intellibot patched for Selenium plugin.
In order to highlight the syntax of .robot files and be able to fall down deeper
into the code, you previously had to install the Intellibot or
Intellibot patched for Selenium plugin
They did good syntax highlighting, but did not allow you to fall down into keyword
sources or Python libraries.
A modern solution to these problems is the Hyper RobotFramework Support plugin
Плагин
Support for .resource files
If you need support for .resource files, PyCharm offered to install Robot Framework Language Server.
But with the advent of Hyper, this need disappeared.
Language Server had to be configured immediately after installation
Python interpreter
If you use the environment that is automatically created by PyCharm - continue working with it.
If you prefer to create your own
virtual environment for Python
-
do it the way you are used to.
In this example, the environment was created using
venv
python -m venv venv
If the interpreter is not selected yet:
In the lower right corner of the window
PyCharm
click
Add New Interpreter → Add Local Interpreter…
Select Existing in the Environment field and specify the path to the newly created virtual environment
C:\Users\Andrei\robot\venv\Scripts\python.exe
If the interpreter is not selected yet:
Click on the lower right corner of the window
PyCharm
https://aredel.com
Click Add New Interpreter
https://aredel.com
Click Add Local Interpreter…
https://aredel.com
Select the interpreter from the drop-down list, or click …
https://aredel.com
Specify the path to python.exe from your virtual environment
https://aredel.com
The name of the interpreter should appear in the lower right corner
https://aredel.com
Installing Robot Framework
After
the interpreter is selected
, make sure that the Robot Framework itself is installed in it.
If the robot is not installed, install it either with the command
python -m pip install robotframework
Or through the interpreter settings:
Go to the interpreter settings either through the lower right corner or through
Settings → Project → Python Interpreter
Click +
https://aredel.com
In the search box, type robotframework
Click Install Package
https://aredel.com
Click Close
https://aredel.com
robotframework should appear in the list of installed packages
https://aredel.com
Running a simple test
Tests can be run in the PyCharm terminal by running the command
python -m robot my_test.robot
By a simple test we mean a test that does not import external resources and libraries. That is, it does not require
manipulations with the
system path
.
To run tests using SHIFT + F10 or the button - you need to edit the file launch configuration
Edit Configurations…
If the configuration has not been set up it will be displayed as Current File
Click on the configuration name, for example, Current File
https://aredel.com
Select Edit Configurations…
https://aredel.com
Click +
https://aredel.com
Select Robot from the list
https://aredel.com
Name the configuration and select the desired interpreter
https://aredel.com
In the script field, click on file search
https://aredel.com
Specify the path to robot.exe from your virtual environment
C:\Users\Andrei\robot\venv\Scripts\robot.exe
Or specify the path to run.py
C:/Users/Andrei/robot/venv/Lib/site-packages/robot/run.py
https://aredel.com
In Script parameters specify the file before the test and click OK
Working directory will leave empty for now, we'll talk about it
here
https://aredel.com
The configuration name should appear in the top panel of the PyCharm window, where Current File used to be
Now, if the test does not import external libraries, it can be run with one click on the button
https://aredel.com
Another example of setting up a configuration:
Open the desired .robot file and next to the start button select
Edit Configuration …
In PyCharm Community Edition I use the following settings
- Module name: robot.run - unfortunately I forgot why robot.run is here
- Script parameters: -v browser:chrome --test "*" C:/AutoTest/project/tests/robot/tests/demo.robot
-
Environment:
-
Environment variables:
- PYTHONPATH: C:\AutoTest\project\tests\robot\libraries,C:\AutoTest\project\gen-py,C:\AutoTest\project\tests\robot\resources
- PYTHONBUFFERED: 1
- Python interpreter: C:\AutoTest\project\venv\Scripts\python.exe
- Working directory: C:\AutoTest\project
- ✅ Add content roots to PYTHONPATH
- ✅ Add source roots to PYTHONPATH
-
Environment variables:
In PyCharm Professional, some fields have a different name or order.
- Name: demo.robot
- Run: C:\AutoTest\project\venv\Scripts\python.exe
- Script: C:/AutoTest/project/venv/Lib/site-packages/robot/run.py
- Script parameters: -v browser:chrome --test "*" C:/AutoTest/project/tests/robot/tests/demo.robot
- Working directory: C:\AutoTest\project
-
Environment variables:
PYTHONPATH: C:\AutoTest\project\tests\robot\libraries,C:\AutoTest\project\gen-py,C:\AutoTest\project\tests\robot\resources
PYTHONBUFFERED: 1 - Add content roots to PYTHONPATH ✕
- Add source roots to PYTHONPATH ✕
Running tests with libraries
To proceed to configuring PyCharm to run robot tests, add all paths to libraries and resources to the interpreter.
File → Settings → Project → Project Interpreter →= ⌵ → Show all → Select the desired interpreter → There are five icons in the right toolbar, click the folder tree icon → + → Select the directory → OK
For example
C:\AutoTest\project\tests\robot\Libraries (added by user) C:\AutoTest\project\tests\robot\Resources (added by user) C:\AutoTest\project\tests\robot (added by user) C:\AutoTest\project\gen-py (added by user)
aredel.com
Running tests with options
To use test run options in PyCharm, for example, --include they must be specified in the configuration
aredel.com
Working directory
Using Working directory, you can specify the directory from which tests will be run.
This can be useful if you decide to run tests intended for running in a CI/CD pipeline. For example, in
Jenkins
or
GitHub Actions
These tests can rely on a specific directory on the runner. For example:
# common.py from robot.libraries.BuiltIn import BuiltIn _built_in = BuiltIn() def get_robot_variable(variable_name: str) -> Any: return _built_in.get_variable_value("${" + variable_name + "}") N_FTR_RSRCS: Path = Path(get_robot_variable("EXECDIR")).joinpath("xyz", "Resources", "n_ftr")
The code above assumes that the tests are run from a directory parent to xyz.
It is not specified explicitly, but let's assume that it is autotests.
If the robot is located in the project structure so that it sees, for example, a directory parent to autotests as EXECDIR, then a space will appear in the path.
repo_name | `--autotests | |-- __init__.py | `-- xyz | |-- __init__.py | `-- Resources | `-- n_ftr `-- venv
The error that Python will return will look something like this:
FileNotFoundError: [Errno 2] No such file or directory
To avoid it, you need to explicitly specify the path to autotests in the Working directory
aredel.com