简体   繁体   中英

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

my python code:

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

Maybe the original issue is that the 0 and 6 were not in ${}?

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. This is the basis of your Python development. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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