Skip to content

Exit codes and streams

Chapter lab: Processesch14-processes.zip · 7 KB

This lesson continues in the same lab as the lesson before it. If you don’t have it yet, download it here.

Download ch14-processes.zip

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

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

Open the folder in VS Code with code rfe-ch14-processes (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-ch14-processes/
    • 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

A process reports success through its exit code — 0 for success, non-zero for a problem — and its messages through two streams: stdout for normal output, stderr for errors. The inventory CLI exits 1 when a stock check fails and writes the offending product to stderr. Run it yourself and check the exit code with $?:

uv run python tools/inventory.py check --min-stock 10python tools/inventory.py check --min-stock 10poetry run python tools/inventory.py check --min-stock 10
low stock: desk-lamp has 8 (< 10)
echo $?
1

The error text went to stderr; the exit code says the check failed. Your test asserts on both.

Task 1 — Assert failing exit codes and stderr

Section titled “Task 1 — Assert failing exit codes and stderr”
Edit

Goal: create checks/exitcodes.robot with two tests: one running a stock check that fails (exit 1, offending product on stderr), and one running the CLI’s fail command (exit 1).

Here is the same shape — one passing run, one failing run. Adapt it.

Worked example — adapt it, don’t copy it

*** Settings ***
Library Process
*** Test Cases ***
Good Run Exits Zero
${ok}= Run Process python3 tools/inventory.py list
Should Be Equal As Integers ${ok.rc} 0
Bad Run Exits One
${bad}= Run Process python3 tools/inventory.py fail
Should Be Equal As Integers ${bad.rc} 1
Should Not Be Empty ${bad.stderr}

Done when

both failing-process tests are defined.

Hint 1

Run the stock check with its flags as separate cells: Run Process python3 tools/inventory.py check --min-stock 10. It exits 1.

Hint 2

The low-stock product is written to stderr — assert Should Contain ${result.stderr} desk-lamp. Add a second test that runs fail and checks ${result.rc} is 1.

Solution
*** Settings ***
Library Process
*** Test Cases ***
Stock Check Fails With Exit Code One
${result}= Run Process python3 tools/inventory.py check --min-stock 10
Should Be Equal As Integers ${result.rc} 1
Should Contain ${result.stderr} desk-lamp
Deliberate Failure Exits One
${result}= Run Process python3 tools/inventory.py fail
Should Be Equal As Integers ${result.rc} 1
Run

Run:

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

Done when

the failing checks exit 1, and the low-stock product appears on stderr.

Check your understanding

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

  1. A non-zero exit code from a process means…

    Show the answer

    Answer: a failure or problem the caller should handle — By convention, 0 is success and non-zero signals a failure — the inventory CLI uses 1 for a failed check.

  2. Where does a process report an error message?

    Show the answer

    Answer: .stderr — Error messages conventionally go to standard error, ${result.stderr}.

Key terms

exit code (rc)concept
0 = success, non-zero = failure; assert with Should Be Equal As Integers ${result.rc} ….
stdout / stderrconcept
A process's normal output vs its error output.

All terms in the glossary →