【问题标题】:Variable from one Test cant be used in other(Variable 'token' is used, but not defined)一个测试中的变量不能用于其他测试(使用了变量“令牌”,但未定义)
【发布时间】:2022-01-18 16:30:39
【问题描述】:

我有以下两个测试,

*** Settings ***

Library     CustomizeLibrary

*** Variables ***

${username}    anti
${password}    anti
${headers}    {"Content-Type": "application/json"}

*** Test Cases ***

GET token based on existing user

    ${token}=    Customize Get Token    http://127.0.0.1:5000/api/auth/token    ${username}    ${password}    ${headers}
    Set suite variable    ${token}


GET Users

    ${token}    Customize Get Token    http://127.0.0.1:5000/api/auth/token    ${token}  (Gives error: Variable 'token' is used, but not defined)



    

${token} 在之前的测试中被定义并设置为套件变量还不能使用它,有什么建议吗?

【问题讨论】:

  • “还不能使用”是什么意思?你有错误吗?它是否设置为错误的值?未完成测试机器人会崩溃吗?您是否在两个测试中记录了值以查看值是多少?
  • 当我在使用 customize get token 关键字创建自己的 CustomizeLibrary 后运行您的代码时,它完全可以正常工作。当我在GET Users 中添加一条日志语句时,将记录第一个测试中的令牌。也许问题不在于测试,而在于您自己的自定义关键字。
  • 另外,您报告的错误看起来不像机器人会抛出的错误。这对我来说更能证明是您的自定义关键字引发了错误,而不是机器人。

标签: selenium robotframework


【解决方案1】:

我建议在套件设置期间通过调用关键字来初始化变量来设置套件变量。它将在测试之前运行并消除测试之间的依赖关系。

这是一个虚拟示例:

*** Settings ***
Suite Setup  Init Suite Variable

*** Test Cases ***
Scenario: Test 1 - Suite Variable
    Log To Console  ${suite_variable}

Scenario: Test 2 - Test Variable 
    Init Test Variable
    Log To Console  ${test_variable}

*** Keywords ***
Init Suite Variable 
    Set Suite Variable  ${suite_variable}  I'm at suite level

Init Test Variable 
    Set Test Variable   ${test_variable}  I'm at test level

在你的情况下,它看起来像......

*** Settings ***
Library     CustomizeLibrary
Suite Setup  GET token based on existing user

*** Variables ***
${username}    anti
${password}    anti
${headers}    {"Content-Type": "application/json"}

*** Test Cases ***
GET Users
    ${token}    Customize Get Token    http://127.0.0.1:5000/api/auth/token

*** Keywords ***
GET token based on existing user
    ${token}=    Customize Get Token    http://127.0.0.1:5000/api/auth/token    ${username}    ${password}    ${headers}
    Set suite variable    ${token}

【讨论】:

    【解决方案2】:

    它被称为“静态变量”,一个不限于单个测试/方法/类实例的变量(谷歌了解更多信息)。此变量存在于所有测试之外。 Robotframework 将其称为“套件变量”,另见http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Suite%20Variable

    如何实现:Static Variables in Robot Framework

    *** Settings *** 
    ... 
    *** Variables *** 
    .... other variables ....
    
    *** Test Cases ***
    GET token based on existing user
        ${token}=    Customize Get Token    http://127.0.0.1:5000/api/auth/token    ${username}    ${password}    ${headers}
        Set suite variable    ${token}
    
    GET Users
        ${token}    Customize Get Token    http://127.0.0.1:5000/api/auth/token    
    

    备注:另一个答案更好,它解决了初始化部分,因此测试可以被注释掉/跳过/以不同的顺序运行

    【讨论】:

    • Robot 没有静态变量的概念。
    • 我不是robotframework专家,但是***变量***下的东西是什么?看起来它们存在于测试中?其他令人难以置信的肮脏解决方案,将其写入本地文件并检索它(糟糕,但完成了工作)
    • @TacoVerhagen 它们不符合静态变量的定义,但从概念上讲,您在 *** 变量 *** 部分中使用变量的建议并没有错 - 如果不是为了您不能在变量部分运行关键字的事实(关键字将被视为变量值的一部分)
    • "什么是***变量***" - 它们是变量,但它们不是静态的。
    • 所以,我尝试在变量部分运行关键字,但它不运行关键字,而是将整个作为字符串分配给变量,如果这样可以很好地解决任何其他建议?
    猜你喜欢
    • 2019-09-17
    • 2011-12-18
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多