机器人:在设置阶段使用 运行 关键字分配变量

Robot: assign variables in setup phase using Run Keywords

我正在尝试为我分配变量的测试用例创建设置阶段。我知道为了做多个关键字我需要使用 Run Keywords,但是这样做时可以设置变量吗?例如:

*** Test Cases ***
Case1
    [Setup]    Run Keywords
    ...            ${var1}=    Keyword1
    ...    AND     ${var2}=    Keyword2

显然上面的方法不起作用,因为 ${var1}${var2} 只是作为 Run Keywords 的参数。由于尚未定义,因此设置失败。

不,你不能。即使您添加了 "using Run Keywords",此问题的答案与

相同

您可以使用设置套件变量键来完成此操作。

set suite variable  ${var1}  Hello World

您可能需要转义变量...

set suite variable  ${var1}  Hello World

来自内置库文档:

如果一个变量已经存在于新范围内,它的值将被覆盖。否则会创建一个新变量。如果当前范围内已经存在变量,则该值可以留空,新范围内的变量获取当前范围内的值。

这里的问题是你为什么要这样做?

我这样做的方式,如果我想调用关键字并在变量中设置它们的输出 要在我的测试套件中重用它们,我执行以下操作:

*** Settings ***
Library        BuiltIn
Suite Setup    Initialize Variables


*** Keywords ***
Initialize Variables
    ${Argument1} =   Set Variable   some_value
    ${output1} =     Keyword1    ${Argument1}
    ${output2} =     Keyword2
    ${output3} =     Keyword3    ${Argument1}   other_value

*** Test Cases ***
Test Case 1
    # Here you can use the variables that you initialized in the Suite Setup.
    Log   ${output1}
    Log   ${output2}
    Log   ${output3}

Test Case 2
    # Also here you can use the same variables.
    No Operation

注意:如果你想为每个测试用例设置变量,你可以在设置部分这样做:

*** Settings ***
Test Setup   Initialize Variables

或者您可以使用测试用例本身的设置(与您在问题中所做的相同)

Test Case 1
    [Setup]   Initialize Variables

请注意,如果需要,"Initialize Variables" 也可以使用参数。