pythonpython-3.xrobotframework

I set an environment variable in one test case but can't retrieve it in another within the same .robot file. How can I access it?


I set an environment variable in the first test case and then try to retrieve the value of that environment variable in another test case within the same .robot file, but it doesn't work. How can I access the environment variable set in one test case from another test case within the same .robot fileError:

After setting enviroment variable it should give access to use it in anotehr test insilde same .robot file. but it gives me error answer.


Solution

  • If you set a variable inside a Test Case without pointing it as Suite or Global it stays Local for that specific Test Case instead you should declare it on global scale or as in the 3rd section of below examples.

    Examples:

    1. Global Variable

      *** Variables *** 
      ${my_global_var}     my_val
      
      *** Test Cases ***
      Test Case 1
          Log    ${my_global_var} # Works
      
      Test Case 2
          Log    ${my_global_var} # Works
      
    2. Local Variable

      *** Test Cases ***
      
      Test Case 1
          ${my_local_var}    Set Variable    Hello World
      
      Test Case 2
          Log    ${my_local_var}    # Fails: Variable only exists in the scope of Test Case 1
      
    3. Suite or Global Inside Test Case

      *** Test Cases ***
      
      Test Case 1
          Set Suite Variable    ${my_suite_var}    I'm a suite variable
          Set Global Variable    ${my_global_var}    I'm a global variable
      
      Test Case 2
          Log    ${my_suite_var}    # Works: Variable exists for the scope of the whole suite
          Log    ${my_global_var}   # Works: Variable exists for the scope of the whole test runof Test Case 1