简体   繁体   English

我无法验证小于和大于机器人框架中的值

[英]I'm not able to validate less than and greater than in robot framework

I'm trying to validate 0<4.3<6, I tried with Robot framework evaluate but I couldn't get the result, because I don't find any method to convert string to float, Then I wrote a python class to achieve this but for all the conditions it showing pass 我正在尝试验证0 <4.3 <6,我尝试使用Robot框架评估,但无法获得结果,因为我找不到任何将字符串转换为float的方法,然后我编写了一个python类来实现这一点但是在所有情况下它都可以通过

my python code: 我的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)

Robot Code: 机器人代码:

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

value of ${word[0]} is 4.3, so Ideally the case should fail, but It didn't ${word[0]}是4.3,因此理想情况下应该失败,但是并没有

Maybe the original issue is that the 0 and 6 were not in ${}? 也许最初的问题是06不在$ {}中?

This works for me fine 这对我来说很好

*** 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

Hope that helps, let me know if it is not your issue! 希望有帮助,如果不是您的问题,请告诉我!

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

In your question you stated that Robot Framework is unable to convert a string to a Float. 在您的问题中,您说过Robot Framework无法将字符串转换为Float。 This is the basis of your Python development. 这是您进行Python开发的基础。 However, this is not correct. 但是,这是不正确的。 In the Robot Framework Userguide on Variables it states: 在《机器人框架变量用户指南》中指出:

The variable syntax can be used for creating both integers and floating point numbers, as illustrated in the example below... 变量语法可用于创建整数和浮点数,如下例所示。

In the BuiltIn Library the keyword for Convert to Number also states firmly it supports floating number BuiltIn库中,“ Convert to Number ”关键字还明确表示它支持浮点数

Converts the given item to a floating point number. 将给定的项目转换为浮点数。 If the optional precision is positive or zero, the returned number is rounded to that number of decimal digits. 如果可选精度为正或零,则返回的数字将四舍五入为该十进制数字。

Which means that your comparison can easily be done using regular keywords. 这意味着您可以使用常规关键字轻松进行比较。

*** 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.

As highlighted by @Bryan Oakley, it is important to use Fail to generate a failure instead of returning a value. 正如@Bryan Oakley强调的那样,使用Fail生成失败而不是返回值很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM