Search code examples
robotframeworkbuilt-in

Robot FW : Buitlin library : Set Suite Variable : How to pass the variable within the current test scope to a new test scope?


Acc.to the documentation:

(1) If a variable already exists within the current scope, the value can be left empty and the variable within the new scope gets the value within the current scope.

The goal is to implement an example to statement (1).

Here is an attempt:

(Test 1/2) Use "Set Suite Variable" : Set Locally Scoped Variable As Suite Scoped
       [Documentation]     If a variable already exists within the current scope 
       ...                 (i.e. Test 1/2), the value can be left empty and
       ...                 the variable within the new scope (i.e. Test 2/2) gets 
       ...                 the value within the current scope (i.e Test 1/2)

       ${local_to_suite_scoped} =     Set Test Variable   ${3}
       # intentionally not setting any value to comply with the statement (1)
       Set Suite Variable      ${local_to_suite_scoped} 

(Test 2/2) Use "Set Suite Variable" : Use local_to_suite_scoped in this test
       Variable Should Exist   ${local_to_suite_scoped}
       Should Be Equal         ${local_to_suite_scoped}     ${3}  # fails with None != 3, expected 3 == 3

Test 2/2 fails but why? Is statement (1) incorrect or are the test cases implemented incorrectly? If the test cases are implemented incorrectly, can you provide the correct implementation?


Solution

  • Set Test Variable (like Set Suite Variable -keyword) expects that the first argument is the name of the variable. Second variable will then be the value of the variable. So instead of

    ${local_to_suite_scoped} =     Set Test Variable   ${3}
    

    do

    Set Test Variable    ${local_to_suite_scoped}    ${3}
    

    See more at http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Suite%20Variable