一篇搞定RobotFramework中字典和列表的取值和判斷和evaluate用法


RF中一些關鍵字的用法可以參考:robotframework官方文檔

http://robotframework.org/robotframework/latest/libraries/BuiltIn.html

test.robot

*** Keywords ***
#自定義變量: scalar list dict
getval ${val1} set variable ${22} [Return] ${val1} getdict &{dict2}  create dictionary   id=1    name=Lara [Return] ${dict2} getlist @{list2} create list hello world [Return] ${list2} *** Variables ***
#共享變量(變量表)
@{list1} hello world &{dict1}     name=auto   pwd=sdfsdfsdf *** Test Cases ***
#測試用例
測試1--By Lara should be true 參數是list #得到關鍵字getlist返回的列表list
 ${retlist} getlist log to console ${list1} log to console ${retlist} log 判斷列表相等 should be true $list1==$retlist 測試2--should be true 參數是dict #得到關鍵字getdict返回的字典dict
 ${retdict} getdict log to console ${dict1} log to console ${retdict} log 判斷字典相等 should be true $dict1==$retdict 測試3--取字典&{dict1}中name屬性的值 #打印auto 常用
    log to console  &{dict1}[name] #打印auto
 log to console ${dict1}[name] #打印auto
    log to console  ${dict1['name']} #打印22 常用
    run keyword if  $dict1['name']=='auto'   log to console   22 測試4--取字典中所有的key[注意evaluate用法!!!] ${retkeys} evaluate [one for one in $dict1] #這種方式也可以
    #${retkeys} evaluate [one for one in ${dict1}]
    log to console  ----------- log to console ${retkeys} 測試5--拓展evaluate用法 ${retval} getval ${sum} evaluate ${retval}+1 log to console ${sum} #總結: #1.run keyword if <condition> #2.should be true <condition> #后面緊跟python表達式,取字典中的值要去掉大括號,key要帶引號.

#3.evaluate 會把你想要計算的表達式直接傳遞給Python,並把Python的計算結果返回給你。這是最經常要用到的。 #跟python中的eval函數用法一致

運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM