Robot_Test_Setup_TearDown

Robot Test Setup & TearDown

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:

  1. Open browser
  2. Login to application
  3. Execute test
  4. 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 Browser
Verify 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:

  1. Suite Setup and Suite Teardown
  2. Test Setup and Test Teardown
  3. 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 SeleniumLibrary
Test Setup Open Application
Test 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 Chrome
Close 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 Environment
Suite Teardown Cleanup Test Environment
*** Keywords ***
Initialize Test Environment
Log Starting test execution
Cleanup Test Environment
Log Cleaning test environment

Execution:

Suite Setup
Test Case 1
Test Case 2
Test Case 3
Suite 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 user
Start Database Connection
Log Database connected
Close 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 SeleniumLibrary
Test 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

TypeExecution FrequencyCommon Usage
Suite SetupOnce before suiteEnvironment initialization
Suite TeardownOnce after suiteFinal cleanup
Test SetupBefore every testTest preparation
Test TeardownAfter every testResource cleanup
Keyword SetupBefore keyword executionKeyword-specific preparation
Keyword TeardownAfter keyword executionKeyword-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:

Setup1
Cleanup2

Prefer:

Open Customer Portal
Close Browser Session
Reset 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 Connection
Execute 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.robot
Test Setup Start Browser
Test 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


Discover more from Rotebit

Subscribe to get the latest posts sent to your email.

1 Comment

Leave a Reply