Get Started with VS Code
Let's explore how you can enhance your development experience with the RobotCode - Robot Framework Support extension for Visual Studio Code. We'll walk you through the steps of installing the RobotCode extension, configuring your environment, and ensuring smooth functionality. Get ready to unleash the power of RobotCode and Robot Framework in no time!
Tutorial Video
Section titled “Tutorial Video”If you prefer to watch a video tutorial, check out the tutorial I gave at RoboCon 2024, which covers installing, setting up and using RobotCode in Visual Studio Code:
Installing RobotCode
Section titled “Installing RobotCode”To install RobotCode and set up your Robot Framework project in Visual Studio Code, follow these steps:
- Open Visual Studio Code.
- Go to the Extensions tab by pressing CONTROL+SHIFT+X or COMMAND+SHIFT+X on Mac.
- Search for RobotCode and install the RobotCode - Robot Framework Support extension. This will also install the Python and Python Debugger extensions.
- (Recommended) Install the Even Better TOML extension for better handling of
.tomlfiles, which will be useful when configuring your project settings in Configure Your Project.
Tip: Add to Workspace Recommendations (Optional)
To ensure a consistent development environment for everyone working on this project, follow these steps:
- Right-click on the RobotCode extension in the Extensions tab.
- Click on Add to Workspace Recommendations. This will prompt all team members to install the RobotCode extension, creating a unified development environment.
See also the VS Code documentation on how to work with extensions.
Set up a Robot Framework Project
Section titled “Set up a Robot Framework Project”First you need to open your project folder in Visual Studio Code. You can do this by selecting File > Open Folder menu and navigating to the root folder of your project. If you don't have a project folder yet, you can also create a new folder in the "Open Folder" dialog.
It is a good style to manage your project dependencies in a requirements.txt file. This file lists all the Python packages required for your project, including Robot Framework and any other dependencies you may need. This file can be used to install all the dependencies at once, ensuring that your project runs consistently across different environments.
Create a requirements.txt file in the root folder of your project and add the following content:
robotframeworkIf you require additional dependencies, you can include them in the requirements.txt file. For instance, if you wish to utilize robotframework-robocop for formatting and linting your robot files and the Browser library for website testing, you can add the following line to the requirements.txt file:
robotframeworkrobotframework-robocoprobotframework-browserInstalling Dependencies
Section titled “Installing Dependencies”To install the dependencies listed in the requirements.txt file, open the terminal in Visual Studio Code and run the following command:
pip install -r requirements.txtThis command will install the Robot Framework and any other dependencies listed in the requirements.txt file. Make sure to run this command in the root folder of your project.
Creating a Virtual Python Environment
Section titled “Creating a Virtual Python Environment”It is recommended to use a virtual python environment for your project to manage dependencies and ensure consistency across different environments. A virtual environment allows you to isolate your project's dependencies from the system-wide Python installation, ensuring that your project runs consistently across different environments.
What is a Virtual Python Environment?
A virtual environment is a self-contained directory that contains a Python installation along with all the necessary packages and dependencies for a specific project.
Using a virtual environment is beneficial for several reasons:
- Dependency management: By creating a virtual environment, you can easily manage and install project-specific dependencies without affecting other Python projects or the system-wide Python installation.
- Reproducibility: With a virtual environment, you can recreate the exact environment in which your project was developed, making it easier to reproduce and debug issues.
More information about virtual environments can be found in the Python documentation.
There are several ways to create a virtual Python environment and manage your project and its dependencies, depending on your preference and workflow. Below, we use the standard built-in Python methods and describe two ways for creating a virtual environment: the "VSCode way" and the "Python way". Some other good options to manage your Robot Framework/Python environment are hatch, poetry or pipenv
VSCode provides a built-in feature to create a virtual environment for your Robot Framework/ Python project. This feature allows you to create a virtual environment and install dependencies directly from the editor. You can also specify the Python version and include one or more requirements.txt files for managing project dependencies.
You can refer to the Python environments in VSCode Documentation for more information.
The short version of the steps are:
- Open the Command Palette by pressing
CONTROL+SHIFT+P or COMMAND+SHIFT+P on Mac, then search for
Python: Create Environment, and select it. - Choose
Venv, this will create a virtual environment in the.venvfolder in your project. - Select your preferred Python version.
- Check the box for requirements.txt and click OK.
To create a virtual environment in Python, you can use the built-in venv module. This module provides a command-line interface as well as a Python API for managing virtual environments.
Open a Terminal in VSCode and ensure you are in the root folder of your project. Then follow these steps:
-
Create a virtual environment:
python -m venv .venv -
Activate the virtual environment:
Terminal window .venv\Scripts\Activate.ps1Terminal window .venv\Scripts\activate.batTerminal window source .venv/bin/activate -
Install project-specific dependencies:
pip install -r requirements.txt
Selecting the Python Interpreter
Section titled “Selecting the Python Interpreter”RobotCode uses the Python environment that the Python extension selects for the workspace folder. If you want to switch to a different Python interpreter with Robot Framework installed for your project, you can select the Python interpreter in Visual Studio Code. This ensures that the correct Python environment is used for running your Robot Framework tests.
To select the Python interpreter, follow these steps:
- Open the Command Palette by pressing CONTROL+SHIFT+P or COMMAND+SHIFT+P on Mac.
- Search for
RobotCode: Select Python Environmentand select it. It opens the environment picker of the Python extension for the workspace folder. - Choose the Python interpreter associated with your virtual environment or the Python interpreter with Robot Framework installed.
As long as the selected environment has no Robot Framework, RobotCode shows a message that says so. You don't need to restart anything: when you install Robot Framework into the environment, RobotCode notices it within a few seconds and starts, and it also notices a virtual environment that you create in the folder; see The Python environment.
Verifying the Installation
Section titled “Verifying the Installation”- Open the terminal in Visual Studio Code. You can do this by pressing CONTROL+` or selecting Terminal > New Terminal from the menu. If there is an existing terminal, you can close it and open a new one to ensure that the virtual environment is activated.
- Run the command
robot --versionto check if Robot Framework is installed correctly.
If the command returns the Robot Framework version number, your installation is successful! With a .robot file open, the language status shows the Robot Framework version that RobotCode uses.
If you encounter any errors, ensure that your virtual environment is activated and that all dependencies in requirements.txt have been installed.
Create and Run Your First Suite/Test
Section titled “Create and Run Your First Suite/Test”Create a first.robot file in your project with the following code to demonstrate a basic example of logging a string message to the debug console:
*** Test Cases ***First Test Case Log Hello worldTo run this test file, press the green play button next to the First Test Case test case in the code.
You should see the Hello world output displayed in the Debug Console of Visual Studio Code.
And that's it! To learn how to write, navigate, run and debug your tests with RobotCode in VS Code, continue with Working in VS Code. If you have any questions or run into issues, check out the guides or join our community on Slack #robotcode for support. Happy coding!
Released under the Apache-2.0. Copyright © 2022-present Daniel Biehl