03 · Structure & test data
Organize resource files and libraries, understand standard keywords, use templates and isolated data.
One source of truth
Separate suites (tests/), reusable domain behavior (resources/), adapters (libraries/) and environment configuration (variables/). Avoid suite-global mutable data. The offline reference project in chapter 07 demonstrates this separation.
Resources, variable files and project structure
Split domain keywords, environment data and tests into clear layers.
Resource files share Robot keywords and variables without becoming executable suites. Variable files are useful when configuration or test data is easier to express in Python. Keep path relationships simple and run from a stable project root.
Example structure:
robot-demo/
├── resources/
│ └── greetings.resource
├── variables/
│ └── env.py
└── tests/
└── greetings.robotresources/greetings.resource:
*** Keywords ***
Build Greeting
[Arguments] ${name}
RETURN Hello, ${name}!variables/env.py:
Run the code to see the result.
tests/greetings.robot:
*** Settings ***
Resource ../resources/greetings.resource
Variables ../variables/env.py
*** Test Cases ***
Greeting Uses Provided Name
${message}= Build Greeting ${DEFAULT_USER}
Should Be Equal ${message} Hello, Alice!
Should Be Equal ${ENV} stagingRun from robot-demo/:
python -m robot --outputdir results testsAvoid a giant common.resource. Organize resources around a domain or technical capability: users.resource, orders.resource, browser_session.resource, and so on. The test file should import the public capabilities it actually uses.
Key points
- Use
.resourcefor shared Robot keywords and variables - Use Python variable files when data/config benefits from Python
- Organize resources by capability, not by dumping everything into common
Common pitfalls
- Creating cyclic resource imports or a monolithic shared file with unrelated keywords
BuiltIn and the standard libraries you should know
Know what Robot already provides before adding dependencies or Python helpers.
BuiltIn is available automatically. Other standard libraries are imported explicitly. The commonly useful set includes Collections, String, DateTime, OperatingSystem, Process and XML.
*** Settings ***
Library Collections
Library String
Library DateTime
Library Process
*** Test Cases ***
Standard Libraries
@{items}= Create List alpha beta
Append To List ${items} gamma
Length Should Be ${items} 3
${upper}= Convert To Upper Case robot
Should Be Equal ${upper} ROBOT
${now}= Get Current Date
Should Not Be Empty ${now}
${result}= Run Process python --version
Should Be Equal As Integers ${result.rc} 0Use OperatingSystem for filesystem/environment operations and Process when the thing under test is a command-line program. XML can parse and assert XML structures. Collections provides list/dictionary assertions and mutation helpers.
Do not use standard-library keywords just because they exist. If a Python helper expresses complex transformation more clearly, keep that logic in Python and expose a focused keyword. Conversely, do not write Python for a basic list assertion already provided by Collections.
Key points
- BuiltIn is always available; other standard libraries are imported
- Prefer the simplest existing library for generic mechanics
- Use Process instead of shell-string hacks when testing subprocesses
Common pitfalls
- Reimplementing standard collection or process behavior in fragile custom keywords
Templates, tags, metadata and execution selection
Represent repeated behavior as data and use tags for meaningful execution slices.
Test templates let one keyword define the behavior while rows provide data:
*** Settings ***
Test Template Addition Should Work
Test Tags arithmetic
*** Test Cases *** A B Expected
Small integers 1 2 3
Zeros 0 0 0
Negative value -1 2 1
*** Keywords ***
Addition Should Work
[Arguments] ${a} ${b} ${expected}
${actual}= Evaluate int($a) + int($b)
Should Be Equal As Integers ${actual} ${expected}Templates are appropriate when the same behavior is exercised with many inputs. They are not a substitute for distinct tests that have different flows.
Tags are execution semantics. Use them for stable slices such as smoke, regression, api, ui, slow or environment requirements. Select by tag:
python -m robot --include smoke --exclude slow testsTag patterns support Boolean combinations. Keep tags lowercase and clear, especially because 7.5 deprecates ambiguous forms where Boolean operators are not separated clearly.
Robot Framework 7.5 adds test/task metadata:
Example
[Metadata] Issue QA-123
[Metadata] Owner payments
Log Test bodyUse metadata for descriptive name/value information; use tags when you need filtering or grouping semantics.
Key points
- Templates separate repeated behavior from input rows
- Tags should correspond to execution/grouping needs
- Use metadata for descriptive pairs that should not control selection
Common pitfalls
- Turning every test attribute into a tag and then creating an unmanageable tag taxonomy
Test isolation, fixtures and ownership
Model setup, teardown, shared data and error recovery explicitly.
A test is independent when it can run alone, after another test, in a different order and in parallel without changing its expected result. Robot suite and test setups are lifecycle scopes, not a substitute for test data ownership.
Example: test-owned state
*** Settings ***
Resource ../resources/inventory.resource
Test Setup Start With Inventory
Test Teardown Clean Up Inventory
*** Test Cases ***
One Reservation
Reserve Product notebook 2
Remaining Stock Should Be notebook 3Here Start With Inventory prepares exactly five units and Clean Up Inventory releases the local state. Both keywords live in a resource file and delegate technical storage logic to Python. See examples/robot-framework/.
For an API, create resources per test with unique identifiers and remove them in teardown. For Browser Library, use a fresh browser context per test. Suite-level shared state is suitable only when immutable or explicitly serialized. If setup fails, inspect teardown and the earliest failing dependency.
Key points
- Reorder and rerun tests to expose implicit dependencies
- Keep setup and cleanup at the narrowest sufficient scope
Common pitfalls
- Using a shared test account or record without a concurrency policy