【问题标题】:RobotFramework Keyword variable not settingRobotFramework 关键字变量未设置
【发布时间】:2017-04-07 13:19:06
【问题描述】:

我正在使用 RobotFramework 和 RobotRequestsLibrary 为一系列 API 创建冒烟测试套件。这是我第一次使用 RobotFramework。在尝试清理代码并使其更易于维护时,我决定尝试使用关键字来删除所有附带的细节。

例如,这里有两个我要清理的测试:

*** Variables ***
${sint}  http://int.somecoolwebsite.com

*** Test Cases ***
Retrieve Store Info By Code Should Return Successful
    [Tags]  get
    Create Session  data-int  ${sint}
    ${resp}=        Get Request  int  /store/1234
    Should Be Equal As Strings   ${resp.status_code}  200

Retrieve All Store Info Should Return Successful
    [Tags]  get
    Create Session  data-int  ${sint}
    ${resp}=        Get Request  int  /stores
    Should Be Equal As Strings   ${resp.status_code}  200

以及我使用关键字的尝试:

*** Variables ***
${sint}  http://int.somecoolwebsite.com

*** Keywords ***
Make ${call} Call To ${end_point}
    Create Session  ${sint}  ${sint}
    ${resp} =  Get Request  ${sint}  ${end_point}
    ${status} =  ${resp.status_code}
    Set Test Variable  ${status}

Status Should Be ${required_status}
    Should Be Equal  ${status}  ${required_status}

*** Test Cases ***
Retrieve Store Info By Code Should Return Successful
    [Tags]  get
    Make Get Call To /store/1234
    Status Should Be 200

Retrieve All Store Info Should Return Successful
    [Tags]  get
    Make Get Call To /stores
    Status Should Be 200

当我使用关键字运行测试用例时,出现以下错误:

Keyword name cannot be empty.

我尝试调试问题并在关键字分配中放置一个断点,我注意到${resp} 被分配并且${resp.status_code} 也有效。但是当我尝试分配 {$status}= ${resp.status_code} 时,会抛出错误。

我尝试了各种方法来使用内置的 Set Variable 重新分配变量,但没有任何运气。不能在关键字中以这种方式分配变量吗?任何见解都会有所帮助。谢谢!!

【问题讨论】:

  • 抱歉,我没有时间深入研究这一点,但通过简短的阅读,数据驱动的风格可能更适合您正在尝试做的事情 - robotframework.org/robotframework/latest/…
  • 您问题中的代码没有给出您所说的错误。该代码对我来说很好。
  • @BryanOakley 你是对的。我在此处编写代码时添加了Set Variable 声明。没有它,它确实会出错。但在测试用例中,我不需要使用该声明。
  • 如果您不显示重现问题的代码,我们将无法帮助您。
  • 我更新了代码,所以它会产生错误。

标签: python automated-tests robotframework smoke-testing


【解决方案1】:

即使问题中的代码仍然没有给出你所说的错误,因为还有其他错误阻止它运行,问题是这一行:

${status} =  ${resp.status_code}

这不是分配变量的正确方法。您必须使用 Set Variable 关键字(或其他一些“Set”关键字),如下所示:

${status}=  Set Variable    ${resp.status_code}

您收到错误的原因是每个测试或关键字步骤都必须有一个关键字。你只有变量名,没有关键字,所以你得到错误Keyword name cannot be empty.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-22
    • 2021-11-05
    • 2018-01-17
    • 2017-05-15
    • 2021-08-13
    相关资源
    最近更新 更多