Skip to content

Files, directories, and environment

Chapter lab: Files and directoriesch13-files.zip · 6 KB

This chapter works in a prepared project. Download it before the first task; it unpacks into its own folder.

Download ch13-files.zip

Or fetch and unpack it from a terminal in VS Code:

Terminal window
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch13-files.zip && unzip -q ch13-files.zip && rm ch13-files.zip

Open the folder in VS Code with code rfe-ch13-files (or File → Open Folder…) and accept the recommended RobotCode and Python extensions. Then set it up:

Terminal window
uv sync
Terminal window
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

Keep the environment active in every terminal where you run lesson commands.

Terminal window
poetry install --no-root

Python 3.13 or newer is required; uv installs it when it is missing.

Open in browserNo setup? Try the lab right here: its files, an editor and a terminal, running in your browser.

What's inside
  • Directoryrfe-ch13-files/
    • Directory.vscode/
      • extensions.json
    • Directorychecks/
      • smoke.robot
    • Directorysample_data/
      • app.log
      • catalog.xml
      • orders.csv
      • results.xml
      • settings.cfg
    • Directorytools/
      • inventory.py
    • .gitignore
    • .python-version
    • LAB.md
    • pyproject.toml
    • README.md
    • requirements.txt
    • uv.lock

The OperatingSystem standard library checks the disk: whether files and directories exist, what they contain, and the process environment. This part’s workspace ships a sample_data/ folder of fixtures — a log, a CSV, config, and XML — to work against. Keywords like File Should Exist, Directory Should Exist, Set Environment Variable, and Environment Variable Should Be Set make good preconditions: assert the data is there before a test uses it.

Task 1 — Assert the sample data files and an environment variable

Section titled “Task 1 — Assert the sample data files and an environment variable”
Edit

Goal: create checks/files.robot that imports OperatingSystem, asserts the sample data is present, sets an environment variable, and checks it is set.

Here is the same set of checks over a different file and variable. Adapt them.

Worked example — adapt it, don’t copy it

*** Settings ***
Library OperatingSystem
*** Test Cases ***
Log File Is Present
File Should Exist sample_data/app.log
Directory Should Exist sample_data
Set Environment Variable SHOP_REGION eu
Environment Variable Should Be Set SHOP_REGION

Done when

the suite imports OperatingSystem and defines Sample Data Is Present.

Hint 1

Assert two files this time: File Should Exist sample_data/catalog.xml and File Should Exist sample_data/orders.csv.

Hint 2

Set and verify your own variable — pick any name, e.g. Set Environment Variable SHOP_ENV test then Environment Variable Should Be Set SHOP_ENV.

Solution
*** Settings ***
Library OperatingSystem
*** Test Cases ***
Sample Data Is Present
File Should Exist sample_data/catalog.xml
File Should Exist sample_data/orders.csv
Directory Should Exist sample_data
Set Environment Variable SHOP_ENV test
Environment Variable Should Be Set SHOP_ENV
Run

Run:

Terminal window
uv run robot checks
Terminal window
robot checks
Terminal window
poetry run robot checks

Done when

the files, directory, and environment variable all check out.

Check your understanding

Quick recall. Your first answer stays in this browser; the quiz never blocks your progress.

  1. Which library checks files and directories on disk?

    Show the answer

    Answer: OperatingSystem — The OperatingSystem standard library provides File Should Exist, Directory Should Exist, environment keywords, and more.

  2. File Should Exist sample_data/catalog.xml fails when…

    Show the answer

    Answer: the file is missing — It asserts the file exists; an empty-but-present file still passes.

Key terms

OperatingSystemconcept
Standard library for files, directories, and environment variables.
File Should Existkeyword
Assert a file exists on disk (Directory Should Exist for folders).

All terms in the glossary →