Tuesday, November 17, 2026

How to pass a value from one test case to another in Robot Framework

To pass a value from one test case to another in Robot Framework, use the Set Suite Variable or Set Global Variable keywords from the Robot Framework BuiltIn Library. By default, variables created inside a test case are local to that test and disappear once it finishes.

How to pass a value from one test case to another in Robot Framework


1. Within the Same Test Suite (.robot file)

Users on the Robot Framework Forum agree that Set Suite Variable is the standard way to share data between tests in the same file without polluting other suites.

*** Test Cases ***
Test Case 1: Generate Order ID
    ${order_id}=    Set Variable    ORD-98765
    Set Suite Variable    ${SHARED_ORDER_ID}    ${order_id}

Test Case 2: Verify Order ID
    Log To Console    Using order ID from previous test: ${SHARED_ORDER_ID}
    Should Be Equal    ${SHARED_ORDER_ID}    ORD-98765
  • Use two arguments. Pass the new variable name first, followed by the value you want to store.
  • Avoid assignment syntax. Do not write ${VAR}= Set Suite Variable ..., because the keyword registers the variable name directly in the suite scope.
  • Run tests together. Both tests must run in the same execution session, as executing Test Case 2 individually will fail with a Variable not found error.

2. Across Different Test Suites (Multiple .robot files)

When tests reside in separate files executed within the same test run, use Set Global Variable instead.

# suite_one.robot
*** Test Cases ***
Login And Capture Token
    ${token}=    Set Variable    abc123xyz
    Set Global Variable    ${AUTH_TOKEN}    ${token}

# suite_two.robot
*** Test Cases ***
Fetch User Profile
    Log To Console    Authenticating with token: ${AUTH_TOKEN}
  • Scope visibility. A global variable remains available to all subsequent suites until the entire robot command finishes.
  • Naming convention. Use uppercase names (like ${AUTH_TOKEN}) to visually distinguish global and suite variables from local variables.

3. Across Separate robot Executions

If you run tests in completely separate terminal commands or CI/CD stages, in-memory variables will reset.

  • Write to an external file. Use Create File from the OperatingSystem library (or Dump Json To File from JSONLibrary) in the first test to save the value to disk, then read it back in the second test using Get File.

No comments:

Post a Comment