From Setting Up Your Environment to Writing and Running Your First Tests with Python Robot Framework

Learn Everything You Need to Kickstart Your API Testing Project

Vincent Delacourt
4 min readOct 31, 2023

1. Set Up Your Development Environment

Make sure Python is installed on your system. You can download the latest version from the official Python website. Me, I used homebrew 😊

We will call this project api_testing_project . Create the project folder:

mkdir api_testing_project
cd api_testing_project

2. Use a Virtual Environment

A virtual environment is an isolated space on your computer where you can install packages and run software without affecting other projects. It’s crucial to keep dependencies required by different projects separate and to avoid conflicts.

python -m venv venv
source venv/bin/activate # On Linux/Mac
.\venv\Scripts\activate # On Windows

3. Choose an IDE/Code Editor

Choose an IDE or code editor that suits your workflow. Popular choices include PyCharm, Visual Studio Code, and Jupyter Notebook.

Personally, I go for Visual Studio Code. 😊

4. Install Robot Framework

pip install robotframework # The main Library
pip install robotframework-requests # As we will test API later

5. Project Directory Structure

In the upcoming steps, we’ll be creating various directories and files. Here’s an overview of the project structure we’ll achieve, which helps in organizing our test cases, resources, and results for easy navigation and maintenance.

api_testing_project/
│ README.md

├───resources/ # Robot Framework resource files
│ │ variables.robot

├───tests/ # Test cases
│ │ stupid_tests.robot

├───results/ # Directory to store test results

├───venv/ # Virtual environment

└───requirements.txt # Python dependencies

└──.flake8 # Flake8 config file
  • README.md: Documentation for your testing project.
<!-- README.md -->
# API Testing Project

This project contains end-to-end tests for our RESTful API using Robot Framework.

## Getting Started

- Install Python on your system.
- Clone this repository.
- Navigate to the project directory and create a virtual environment: `python -m venv venv`
- Activate the virtual environment.
- Install the required dependencies: `pip install -r requirements.txt`
- Run the tests: `robot -d results tests/`

## Writing Tests

- Add your test cases to the `tests/` directory.
- Define reusable keywords in the `resources/` directory.
- Update `variables.robot` with any environment-specific variables.

## Format and Linting

- Lint your code `flake8 ./`
- Format your code `black ./`

## Precommit

- Run on all files `pre-commit run --all-files`
  • resources/variables.robot: Environment-specific variables.
# variables.robot
*** Variables ***
${API_URL} https://jsonplaceholder.typicode.com
  • tests/stupid_tests.robot: Specific test cases for testing.
# stupid_tests.robot
*** Settings ***
Resource ../resources/variables.robot


*** Test Cases ***
Stupid Pass Test
[Documentation] This is a stupid pass test
Log This is a stupid pass test
Should Be Equal ${API_URL} https://jsonplaceholder.typicode.com

6. Linters

Follow PEP 8 guidelines to write clean and readable code. You can use a linter to help you adhere to these standards. flake8 is a popular choice

Add a .flake8 file a the root of your project to ignore folders

; .flake8
[flake8]
exclude =
venv
results

Then run:

pip install flake8
flake8 .

The pip command install the package. You should run once. The flake8 checks your code against coding style (PEP 8), you can run it every time your code changes.

Regularly linting your code ensures consistency and helps identify issues early, which is an important part of maintaining the quality of your project

7. Formatters

black is an opinionated code formatter:

pip install black
black .

8. Install Visual Studio Code Extensions

Flake8:

Install the Flake8 Extension: https://marketplace.visualstudio.com/items?itemName=ms-python.flake8

Black:

Install the Black Extension: https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter

Robot Framework:

Install the Robot Framework Extension: https://marketplace.visualstudio.com/items?itemName=robocorp.robotframework-lsp

Setup Workspace Settings:

mkdir .vscode

Create a file ./.vscode/settings.json

// settings.json
{
"editor.formatOnSave": true,
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
}

9. Set Up Version Control with Git

Version control is essential for tracking changes and collaborating with others.

git init
echo "venv/\nresults/" > .gitignore
git add .
git commit -m "Initial commit"

10. Add Pre-Commit

Pre-commit hooks are scripts you set up in your repository to run checks before each commit. They help you catch issues early, ensuring that you only commit quality code.

Install Pre-Commit

First, you need to have pre-commit installed. You can install it using pip:

pip install pre-commit

Create a Pre-Commit Configuration File

Next, create a .pre-commit-config.yaml file in the root of your repository with the following content:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: requirements-txt-fixer
- id: detect-private-key
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8
- repo: https://github.com/psf/black
rev: 23.10.1
hooks:
- id: black
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v2.4.0
hooks:
- id: conventional-pre-commit
stages: [commit-msg]

This configuration file tells pre-commit to run flake8 and black. Also, we are checking the requirements.txt , and if no private key has been pushed. We also use the commitlint tool to lint your commit messages against the conventional commit specification.

Install the Pre-Commit Hook

Now, you can install the pre-commit hook with the following command:

pre-commit install

Try it

pre-commit run --all-files

11. Dependency Management

Create a requirements.txt file to list your project’s dependencies.

pip freeze > requirements.txt

And to install them:

pip install -r requirements.txt

12. Run your first test

robot -d results tests/

All the reports will be in the results folder.

Running robot -d results tests/ executes your test cases and stores the outputs in the 'results' directory. This allows you to see what passed, and what failed, and troubleshoot as necessary.

Conclusion

By following the steps outlined in this guide, you’ll create a solid foundation for your project, ensuring it is maintainable, scalable, and ready for collaboration. Happy coding!

--

--

Vincent Delacourt

Interesting in start-up or project development in the latest technologies for web and mobile apps