Robot Framework框架簡介
- Robot Framework框架是一個通用的驗收測試和驗收測試驅動開發的自動化測試框架(ATDD),使用的是關鍵字驅動的測試方法。它本身擁有強大的標准庫,此外還可以根據項目需要,使用Python對其測試庫和框架本身進行擴展和優化,能同時滿足對接口、數據庫、UI自動化和服務器端自動化的測試需求,編寫測試用例的成本低,維護方便。
項目說明
- 本框架使用的是接口設計的分層玩法,分層的原則是:接口數據和接口業務分離。具體為:項目目錄結構分層,接口數據與接口用例分離,接口用例與接口業務分離,公共方法和公共配置也接口業務分離,在測試用例中可以傳入不定參數,同時也可以定義用例的執行順序。
技術棧
- Robot Framework
- RequestsLibrary
- HttpLibrary.HTTP
框架目錄結構及相關說明
1、框架目錄結構圖如下

2、目錄結構說明
- 公共配置 ===========> 配置文件,定義公共請求頭、請求域名等
- 基礎模塊 ===========> 封裝請求方法,公共方法和工具方法模塊
- 功能組件 ===========> 封裝接口業務模塊,接口依賴模塊,多個接口組合復雜業務場景等
- 主干用例&項目用例 ===========> 存放接口測試用例
代碼設計與功能說明
1、封裝請求方法,如下
*** Settings ***
Library RequestsLibrary
Library Collections
Library HttpLibrary.HTTP
Resource ../icmcenterApi/公共配置/公共配置_index.txt
*** Keywords ***
SendPost
[Arguments] ${root_url} ${uri} ${ParameterDict} ${DataType} ${header}
[Documentation] ${root_url}:接口的host;
... ${uri}:接口uri;
... ${dict}:接口傳入參數,字典數據類型;
... ${DataType}:傳入參數類型,如data等;
... ${header}:請求頭,字典類型。
...
... 響應數據為json類型。
...
... 若為獨立請求,可在請求結束后立即釋放連接。
${RequestData} Create Dictionary
log ${ParameterDict.keys()}
: FOR ${key} IN @{ParameterDict.keys()}
\ set to dictionary ${RequestData} ${key} ${ParameterDict['${key}']}
log ${RequestData}
create session api ${root_url}
${response} post request api ${uri} ${DataType}=${RequestData} headers=${header} timeout=${timeout}
# 將json的string類型的數據轉成python的字典類型
Comment ${ResponseBody} To Json ${response.content}
sleep ${sleepTime}
log ${response.text}
[Return] ${response.text}
SendGet
[Arguments] ${root_url} ${uri} ${ParameterDict} ${header}
[Documentation] 響應數據為json類型
${RequestData} Create Dictionary
log ${ParameterDict.keys()}
: FOR ${key} IN @{ParameterDict.keys()}
\ set to dictionary ${RequestData} ${key} ${ParameterDict['${key}']}
log ${RequestData}
create session api ${root_url}
${response} get request api ${uri} params=${RequestData} headers=${header} timeout=${timeout}
Comment ${ResponseBody} To Json ${response.content}
sleep ${sleepTime}
log ${response.text}
[Return] ${response.text}
re_session
[Arguments] ${host}
[Documentation] 創建會話
create session session ${host}
re_post
[Arguments] ${uri} ${ParameterDict} ${DataType} ${header}
[Documentation] 不創建會話,僅發送post請求;
... 響應數據為json類型
${RequestData} Create Dictionary
log ${ParameterDict.keys()}
: FOR ${key} IN @{ParameterDict.keys()}
\ set to dictionary ${RequestData} ${key} ${ParameterDict['${key}']}
log ${RequestData}
${response} post request session ${uri} ${DataType}=${RequestData} headers=${header} timeout=${timeout}
Comment ${ResponseBody} To Json ${response.content}
sleep ${sleepTime}
log ${response.text}
[Return] ${response}
re_get
[Arguments] ${uri} ${ParameterDict} ${header}
[Documentation] 不創建會話,僅發送get請求;
... 響應數據為json類型
${RequestData} Create Dictionary
log ${ParameterDict.keys()}
: FOR ${key} IN @{ParameterDict.keys()}
\ set to dictionary ${RequestData} ${key} ${ParameterDict['${key}']}
log ${RequestData}
${response} get request session ${uri} params=${RequestData} headers=${header} timeout=${timeout}
Comment ${ResponseBody} To Json ${response.content}
sleep ${sleepTime}
log ${response.text}
[Return] ${response}


- 以不定參數的形式封裝請求,后續調用時,可傳入不同個數的參數,方便不同場景下測試用例的設計
2、封裝接口業務模塊

- 根據業務應用場景,組合接口模塊
- 發起請求,獲取響應,並進行響應斷言
3、測試用例設計

- 根據業務場景,設計測試用例
- 調用組合的接口模塊,並依據場景需要,傳入不同的參數
項目實例演示
