我在一个测试用例中设置了一个环境变量,但无法在同一 .robot 文件中的另一个测试用例中检索它。我怎样才能访问它?

问题描述 投票:0回答:1

我在第一个测试用例中设置了一个环境变量,然后尝试在同一 .robot 文件中的另一个测试用例中检索该环境变量的值,但它不起作用。如何从同一 .robot 文件中的另一个测试用例访问一个测试用例中设置的环境变量错误:

设置环境变量后,应该可以在相同的 .robot 文件中的另一个测试中使用它。但它给了我错误答案。

python python-3.x robotframework
1个回答
0
投票

如果您在

Test Case
中设置变量,而没有将其指定为
Suite
Global
,则对于该特定测试用例,它会保持
Local
,而不是您应该按
global
规模或如下面的第 3 部分中那样声明它例子。

示例:

  1. 全局变量

    *** 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. 局部变量

    *** 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. 套件或全局内部测试用例

    *** 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
    
© www.soinside.com 2019 - 2024. All rights reserved.