簡體   English   中英

我無法驗證小於和大於機器人框架中的值

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

我正在嘗試驗證0 <4.3 <6,我嘗試使用Robot框架評估,但無法獲得結果,因為我找不到任何將字符串轉換為float的方法,然后我編寫了一個python類來實現這一點但是在所有情況下它都可以通過

我的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,因此理想情況下應該失敗,但是並沒有

也許最初的問題是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

在您的問題中,您說過Robot Framework無法將字符串轉換為Float。 這是您進行Python開發的基礎。 但是,這是不正確的。 在《機器人框架變量用戶指南》中指出:

變量語法可用於創建整數和浮點數,如下例所示。

BuiltIn庫中,“ Convert 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生成失敗而不是返回值很重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM