【问题标题】:Robot Framework - query returned keyword typesRobot Framework - 查询返回的关键字类型
【发布时间】:2017-06-05 15:32:46
【问题描述】:

有没有办法查询返回的关键字变量类型? 尝试调试一些脚本,如果有像 Python type(my_variable) 这样可用的东西真的很有用吗?

【问题讨论】:

    标签: python types robotframework keyword


    【解决方案1】:

    假设您使用的是机器人版本 2.9 或更高版本,您可以使用 Evaluate 关键字通过利用 Evaluate 的 variable substitution 来使用 python 的 type(myvariable)

    例如:

    *** Keywords ***
    keyword that returns an int
        ${result}=  set variable  ${42}
        [return]    ${result}
    
    keyword that returns a list
        ${result}=    create list   one  two  three
        [return]    ${result}
    
    keyword that returns a string
        ${result}=    set variable  foo
        [return]    ${result}
    
    *** Test Cases ***
    Example
        ${result1}=    keyword that returns an int
        @{result2}=    keyword that returns a list
        ${result3}=    keyword that returns a string
    
        ${type1}=    evaluate    type($result1)
        ${type2}=    evaluate    type($result2)
        ${type3}=    evaluate    type($result3)
    
        should be equal as strings    ${type1}    <type 'int'>
        should be equal as strings    ${type2}    <type 'list'>
        should be equal as strings    ${type3}    <type 'unicode'>
    

    【讨论】:

    • Evaluate 关键字是通过比较进行验证的唯一或主要方式吗?我可以看到用途,但它需要知道比较的类型。我的关键字(内置在 Python 库中)返回一个字符串。但验证为“unicode”失败......这让我试图找出我应该检查哪些类型。
    • 我不明白你的问题。是的,要验证对象的类型,您需要“知道用于比较的类型”。这实际上是使用 python 的 type 命令返回信息,这正是您在问题中要求的信息。当然,您可以在使用type 或其他任何内容的python 中编写自己的关键字,并将结果转换为您的测试可用的内容。
    猜你喜欢
    • 2011-11-26
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 2022-08-18
    • 2015-12-04
    • 2019-04-26
    • 2015-11-25
    相关资源
    最近更新 更多