Setups and teardowns
Chapter lab: Structure and execution control
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:
curl -fsSLO https://manykarim.github.io/robotframework-essentials-web/labs/ch06-structure-control.zip && unzip -q ch06-structure-control.zip && rm ch06-structure-control.zipInvoke-WebRequest https://manykarim.github.io/robotframework-essentials-web/labs/ch06-structure-control.zip -OutFile ch06-structure-control.zip; Expand-Archive ch06-structure-control.zip -DestinationPath .; Remove-Item ch06-structure-control.zipOpen 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:
uv syncpython3 -m venv .venvsource .venv/bin/activatepip install -r requirements.txtpython -m venv .venv.venv\Scripts\Activate.ps1pip install -r requirements.txtKeep the environment active in every terminal where you run lesson commands.
poetry install --no-rootPython 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.
Task 1 — Add suite and test setup/teardown
Section titled “Task 1 — Add suite and test setup/teardown”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 upSuite Teardown Log Shutting down
*** Test Cases ***Uses A Lifecycle [Setup] Log Getting ready [Teardown] Log Tidying up Should Be Equal on onDone 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 suiteSuite 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 readyTask 2 — Run the suite green
Section titled “Task 2 — Run the suite green”Run it:
uv run robot checksrobot checkspoetry run robot checksOpen 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.
When does a Suite Teardown run?
Suite Setup runs once before the suite, Suite Teardown once after — even if a test failed.
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.
How do you give one test its own setup?
[Setup]and[Teardown]inside a test case set that test's own setup/teardown.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.