python - RobotFramework 关键字变量未设置

标签 python automated-tests robotframework smoke-testing

我正在使用 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

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

关键字名称不能为空

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

我尝试了多种方法来使用内置的 Set Variable 重新分配变量,但没有任何运气。 Keywords里不能这样赋值变量吗?任何见解都会有所帮助。谢谢!!

最佳答案

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

${status} =  ${resp.status_code}

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

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

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

关于python - RobotFramework 关键字变量未设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40766027/

相关文章:

python - Django Rest Framework 嵌套序列化程序不显示相关数据

javascript - .expect 断言在移动到页面模型中的函数时不起作用

testing - 使用/不同语言绑定(bind)的 Selenium 端到端测试

testing - 无法从 OSX 上的 Testcafe IDE 连接到站点

docker - 如何在Docker内部使用机器人框架执行本地测试并将输出保存到主机

python - 无法使用 Python 通过 Robot Framework 连接到 localhost :xxxx from session not created: Chrome version must be >= 69. 0.3497.0 上的 chrome

python - 如何将分钟添加到 Pandas Dataframe 中的 datetime64 对象

python - 函数可以在 for 循环语句中返回两次吗

python - 对来自html标签的信息进行分类

robotframework - 我可以在 Robot 框架中使用变量调用关键字吗