Search code examples
robotframeworkbuilt-in

Robot FW : Builtin module : Set Suite Variable : How to overwrite the newly created variable to in the new scope?


Acc.to Variable scopes, we have Global Scope, Test Suite Scope, Test Case scope and Local Scope.

Referring to the Set Suite Variable documentation:

If a variable already exists within the new scope, its value will be overwritten.

My goal is to write a simple code snippet, which demonstrates this sentence in practice. This is the technical problem.

To be able to fully implement the technical goal, the new scope and current scope need an explanation. For example, how does current/new scope relate to Global Scope, Test Suite Scope, Test Case scope and Local Scope?


Solution

  • It's a bit hard to understand exactly what you're asking, but I think the following tests might illustrate the sentence "If a variable already exists within the new scope, its value will be overwritten."

    First, this test creates a variable named ${var} in the test scope of the first test. A second test verifies that the variable isn't visible outside of the original scope.

    The third test starts the same as the first test, creating a test-scoped variable. It then calls Set suite variable which overwrites the variable that is in the test scope of the third test.

    Finally, a fourth test shows that the variable is now available to every test in the suite since the variable exists in the suite scope.

    *** Test cases ***
    Example 1
        # Set a variable in a test scope
        Set test variable  ${var}  test
        Should be equal    ${var}  test
    
    Example 2
        # Verifies that the variable from the first test
        # is not available in this new test scope
        Variable should not exist  ${var}
    
    Example 3
        # Set a variable in the test scope
        Set test variable  ${var}  test
        Should be equal    ${var}  test
    
        # Set a suite variable that has the same name as the local variable
        Set suite variable  ${var}  suite
    
        # Even though we set it as a suite scope, it overwrites the local
        # variable of the same name
        Should be equal  ${var}  suite
    
    Example 4
        # Verify the suite variable from the previous test is visible
        # in this new test scope
        Should be equal  ${var}  suite