Managing Test Preparation and Cleanup in Robot Framework
In any test automation project, maintaining a clean and reliable test environment is essential. Before executing a test case, we often need to perform preparation activities such as opening an application, creating test data, logging into a system, or initializing resources. Similarly, after test execution, we may need to clean up resources, close applications, remove temporary data, or generate reports. This is where Robot Test Setup and TearDown come into play.
In this article from the Robot Framework Test Automation series, we will explore:
- What Test Setup and TearDown mean in Robot Framework
- Different levels of setup and teardown
- How to use them with practical examples
- Best practices for writing maintainable automation tests
What is Test Setup and TearDown in Robot Framework?
In Robot Framework, Setup and TearDown are special keywords that execute automatically before and after test execution.
Test Setup
A Test Setup runs before every test case.
It is mainly used for:
- Preparing test data
- Opening browsers
- Launching applications
- Performing login operations
- Configuring test environments
Test TearDown
A Test TearDown runs after every test case.
It is commonly used for:
- Closing browsers
- Releasing resources
- Capturing screenshots after failures
- Cleaning temporary files
- Logging out users
A typical automation flow looks like this:
Test Setup | ↓Execute Test Case | ↓Test TearDown
Why Do We Need Setup and TearDown?
Without proper setup and cleanup logic, automated tests can become unreliable.
For example, consider a web application test suite:
- Open browser
- Login to application
- Execute test
- Close browser
If every test case contains these same steps, your automation code becomes repetitive.
Example:
*** Test Cases ***Verify User Login Open Browser Enter Username Enter Password Click Login Verify Dashboard Close BrowserVerify User Profile Open Browser Enter Username Enter Password Click Login Verify Profile Page Close Browser
The browser opening and closing steps are duplicated.
Using Setup and TearDown improves reusability.
Types of Setup and TearDown in Robot Framework
Robot Framework supports setup and teardown at multiple levels:
- Suite Setup and Suite Teardown
- Test Setup and Test Teardown
- Keyword Setup and Keyword Teardown
Let’s explore each one.
1. Test Setup and Test Teardown
A Test Setup applies to individual test cases.
Example:
*** Settings ***Library SeleniumLibraryTest Setup Open ApplicationTest Teardown Close Application*** Test Cases ***Verify Login Input Text username admin Input Password password secret Click Button Login Page Should Contain Dashboard*** Keywords ***Open Application Open Browser https://example.com ChromeClose Application Close All Browsers
Execution flow:
Open Application | ↓Verify Login | ↓Close Application
Every test case automatically gets the same setup and teardown.
2. Suite Setup and Suite Teardown
Suite Setup executes once before all test cases in a test suite.
Suite Teardown executes once after all tests finish.
Example:
*** Settings ***Suite Setup Initialize Test EnvironmentSuite Teardown Cleanup Test Environment*** Keywords ***Initialize Test Environment Log Starting test executionCleanup Test Environment Log Cleaning test environment
Execution:
Suite SetupTest Case 1Test Case 2Test Case 3Suite Teardown
Common uses:
- Database initialization
- Environment configuration
- Starting services
- Generating final reports
3. Keyword Setup and Keyword Teardown
Robot Framework also supports setup and teardown for user-defined keywords.
Example:
*** Keywords ***Create User [Setup] Start Database Connection [Teardown] Close Database Connection Log Creating new userStart Database Connection Log Database connectedClose Database Connection Log Database connection closed
This is useful when a specific reusable keyword requires additional preparation.
Setting Setup and TearDown Directly Inside Test Cases
You can also define setup and teardown at the test case level.
Example:
*** Test Cases ***Verify Product Search [Setup] Login Application [Teardown] Logout Application Search Product Verify Results
This provides flexibility when only a few tests require special handling.
Using Built-in Keywords in TearDown
A common requirement is capturing screenshots when tests fail.
Example:
*** Settings ***Library SeleniumLibraryTest Teardown Capture Screenshot*** Keywords ***Capture Screenshot Run Keyword If Test Failed ... Capture Page Screenshot
This helps during debugging because failed test execution evidence is automatically collected.
Difference Between Setup and TearDown Levels
| Type | Execution Frequency | Common Usage |
|---|---|---|
| Suite Setup | Once before suite | Environment initialization |
| Suite Teardown | Once after suite | Final cleanup |
| Test Setup | Before every test | Test preparation |
| Test Teardown | After every test | Resource cleanup |
| Keyword Setup | Before keyword execution | Keyword-specific preparation |
| Keyword Teardown | After keyword execution | Keyword-specific cleanup |
Best Practices for Robot Framework Setup and TearDown
1. Keep Setup and TearDown Simple
Avoid placing large business logic inside setup keywords.
Good:
Test Setup Login User
Avoid:
Test Setup Complete Application Workflow
Setup should prepare the environment, not execute test scenarios.
2. Always Clean Up Resources
A failed test should not leave resources running.
Examples:
- Open browsers
- Database connections
- Temporary files
- Network sessions
Always include proper teardown logic.
3. Use Meaningful Keyword Names
Avoid generic names:
Setup1Cleanup2
Prefer:
Open Customer PortalClose Browser SessionReset Test Database
Readable keywords make automation easier to maintain.
4. Avoid Unnecessary Setup Execution
If a test does not require a browser, avoid launching one through a global setup.
Use test-specific setup when needed.
Common Mistakes with Setup and TearDown
Mistake 1: Forgetting Teardown After Resource Creation
Example:
Create Database ConnectionExecute Test
Without cleanup:
Database Connection Remains Open
This can cause failures in later tests.
Mistake 2: Putting Validation Logic in Setup
Setup should prepare the environment.
Avoid:
Setup: Login Verify Dashboard
Verification belongs inside test cases.
Mistake 3: Using Too Many Global Setups
A global setup can slow down execution.
Use the lowest possible level:
- Suite level for suite-wide actions
- Test level for test preparation
- Keyword level for reusable actions
Real-World Example: Web Automation Framework
A common Selenium-based Robot Framework structure:
Project│├── Tests│ └── LoginTests.robot│├── Resources│ ├── BrowserKeywords.robot│ └── UserKeywords.robot│└── Data └── TestData.xlsx
LoginTests.robot:
*** Settings ***Resource ../Resources/BrowserKeywords.robotTest Setup Start BrowserTest Teardown Close Browser*** Test Cases ***Valid Login Test Enter Username Enter Password Click Login Verify Home Page
This structure keeps tests clean and focused.
Conclusion
Robot Framework Setup and TearDown provide a powerful way to manage test preparation and cleanup activities. They reduce duplicate code, improve test reliability, and make automation frameworks easier to maintain.
Understanding when to use:
- Suite Setup/TearDown
- Test Setup/TearDown
- Keyword Setup/TearDown
is essential for building professional Robot Framework automation solutions.
In the next article of this Robot Framework Test Automation series, we will explore Robot Framework Variables and Data Management and learn how to create flexible, data-driven automation tests.
Related Articles
- What is Robot Framework?
- Framework Architecture
- Installation
- Installing Browser Library
- Browser Library vs SeleniumLibrary
- Writing Your First Browser Test
- Sections in Robot Test File
- Robot Resource Files
- Robot Test Templates
Discover more from Rotebit
Subscribe to get the latest posts sent to your email.


Pingback: Parallel Execution Using Pabot in Robot Framework - Rotebit