Skip to content

Setups and teardowns

Chapter lab: Structure and execution controlch06-structure-control.zip · 3 KB

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

Download ch06-structure-control.zip

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

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

Open the folder in VS Code with code rfe-ch06-structure-control (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-ch06-structure-control/
    • Directory.vscode/
      • extensions.json
    • Directorychecks/
      • prices.robot
    • .gitignore
    • .python-version
    • LAB.md
    • pyproject.toml
    • README.md
    • requirements.txt
    • uv.lock

Setup and teardown keywords run around your tests so each starts from a known state and cleans up after itself:

  • Suite Setup / Suite Teardown — once, before and after the whole suite.
  • [Setup] / [Teardown] — before and after one test case.

A teardown runs even if the test failed, which makes it the right place for cleanup. Set suite-level ones in *** Settings ***; set per-test ones inside the test.

Setup and teardown execution ordersequence diagram. A teardown runs even if the test failedSuite Setuponce, before the whole suite1[Setup]before this test2Test bodythe assertions run3[Teardown]after the test, even if it failed4Suite Teardownonce, after the whole suite5A teardown runs even if the test failed

Task 1 — Add suite and test setup/teardown

Section titled “Task 1 — Add suite and test setup/teardown”
Edit

Goal: create checks/lifecycle.robot with a suite-level setup and teardown and a test that also has its own [Setup] and [Teardown].

Here is the same lifecycle over different messages. Adapt the wording.

Worked example — adapt it, don’t copy it

*** Settings ***
Suite Setup Log Warming up
Suite Teardown Log Shutting down
*** Test Cases ***
Uses A Lifecycle
[Setup] Log Getting ready
[Teardown] Log Tidying up
Should Be Equal on on

Done when

checks/lifecycle.robot defines a Suite Setup, and the test passes.

Hint 1

In *** Settings ***, add Suite Setup Log Opening the suite and Suite Teardown Log Closing the suite.

Hint 2

Inside the test, add [Setup] Log ... and [Teardown] Log ..., then a simple passing assertion.

Solution
*** Settings ***
Suite Setup Log Opening the suite
Suite Teardown Log Closing the suite
*** Test Cases ***
Runs With Lifecycle
[Setup] Log Preparing the test
[Teardown] Log Cleaning up the test
Should Be Equal ready ready
Run

Run it:

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

Open log.html to see the setup and teardown wrapped around the test.

Done when

the test Runs With Lifecycle passes.

Check your understanding

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

  1. When does a Suite Teardown run?

    Show the answer

    Answer: Once, after all tests in the suite — Suite Setup runs once before the suite, Suite Teardown once after — even if a test failed.

  2. How do you give one test its own setup?

    Show the answer

    Answer: [Setup] inside the test — [Setup] and [Teardown] inside a test case set that test's own setup/teardown.

Key terms

Suite Setup / Teardownconcept
Run once before/after the whole suite; teardown runs even after failures.
[Setup] / [Teardown]concept
Per-test setup/teardown, declared inside a test case.

All terms in the glossary →