【问题标题】:I'm not able to validate less than and greater than in robot framework我无法在机器人框架中验证小于和大于
【发布时间】:2017-12-05 00:19:34
【问题描述】:

我正在尝试验证 0

我的python代码:

def should_be_x_than_y (number1, relation, number2, relation1, number3):

    if relation =="<" and relation1 == "<":
        print(relation)
        print (relation1)
        print (number1)
        print (number2)
        print (number3)
        **return float(number1) < float(number2) < float(number3)**
    if relation ==">" and relation1 == ">":
        return float(number1) > float(number2) > float(number3)
    if relation =="=>" and relation1 == "<=":
        return float(number1) >= float(number2) <= float(number3)
    if relation =="<=" and relation1 == "=>":
        return float(number1) <= float(number2) >= float(number3)
    if relation =="=":
        return float(number1) == float(number2)

机器人代码:

should_be_x_than_y   0  <  ${words[0]}  <  3

${word[0]} 的值为 4.3,所以理想情况下该案例应该失败,但它没有

【问题讨论】:

  • 要让它失败,你需要引发一个异常。简单地返回 False 不会导致测试失败。

标签: python python-2.7 robotframework


【解决方案1】:

也许最初的问题是 06 不在 ${} 中?

这对我很有效

*** Variables ***
@{words}      4.8    7.8


*** Test Cases ***
Test1
    [Tags]                             example
    Run Keyword Unless     ${0} < ${words[0]} < ${6}     Fail

Test2
    [Tags]                             example
    Run Keyword Unless     ${0} < ${words[1]} < ${6}     Fail

希望对您有所帮助,如果不是您的问题,请告诉我!

==============================================================================
Basic
==============================================================================
Test1                                                                 | PASS |
------------------------------------------------------------------------------
Test2                                                                 | FAIL |
AssertionError

【讨论】:

  • 不,关键字是显式地将参数转换为数字。
  • 当你说Keyword是指Run Keyword Unless?我提供了一个根本不需要他有 python 关键字的解决方案。他最初的问题是“0
  • “当您说关键字时,您的意思是运行关键字,除非” - 抱歉,我误解了您所写的内容。您的解决方案有效。如果您稍作修改,我可以删除我的反对票。
  • 非常感谢,我正在努力提高我的代表,以便能够发表评论。无关,感谢您如此积极地回答机器人框架问题。我已经与它密切合作一年半了,并且仍在发现新事物。
【解决方案2】:

在您的问题中,您表示 Robot Framework 无法将字符串转换为浮点数。这是您的 Python 开发的基础。然而,这是不正确的。在Variables 的 Robot Framework Userguide 中指出:

变量语法可用于创建整数和 浮点数,如下例所示...

BuiltIn LibraryConvert to Number 的关键字也明确表示它支持浮点数

将给定项目转换为浮点数。如果可选 精度为正或零,返回的数字四舍五入 小数位数。

这意味着您可以使用常规关键字轻松进行比较。

*** Variables ***
@{words}    4.7    7.8

*** Test Cases ***
TC
                                           Should Be X Than Y   0 < ${words[0]} < 6
    Run Keyword and Continue on Failure    Should Be X Than Y   0 < ${words[1]} < 6

*** Keywords ***
Should Be X Than Y
    [Arguments]    ${expression}
    Run Keyword If     
    ...    not(${expression})    
    ...    Fail    Number does not match Expression pattern.

正如@Bryan Oakley 所强调的,使用Fail 生成故障而不是返回值非常重要。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-08
    • 2014-07-24
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    相关资源
    最近更新 更多