十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
如何手寫robot自動化用例,網(wǎng)上雖然有一大堆教程,但都是關(guān)于ride的,對于如何從腳本角度來寫,一直零零散散,語焉不詳,本文以互聯(lián)網(wǎng)最常見的API接口自動化為例,給出方向性指引,文字不多,足夠你理解,想要深入,請依循文中的文字,再去百度,慢慢體會,逐步精深。
用例編寫前提:已經(jīng)部署好了robot/python環(huán)境
*** Settings ***
Library Collections
Library Process
Library RequestsLibrary
*** Variables ***
${DOMAIN} http://10.xx.xx.xx:8091
${URI} /test/backend/xxxxlogin
${HEADER} {"Content-Type":"application/json"}
${USER} {"accountType":"1", "phonenumStr":"iamusername", "password":"iampassword"}
${LIST_ANIMALS} ["cat", "dog"]
*** Test Cases ***
my first http test case
create session myhttp ${DOMAIN}
${uri} set variable ${URI}
# headers一定要轉(zhuǎn)成 json,不然data傳不過去
${header} to json ${HEADER}
${postdata} to json ${DATA}
${resp} post request myhttp uri=${uri} data=${postdata} headers=${header}
# 校驗 http的狀態(tài)
should be equal as integers 200 ${resp.status_code}
# 打印 調(diào)測信息
#log ${resp.content}
#log ${json}
#log ${json[0]}
#log ${json[1]["id"]}
${json} to json ${resp.content}
# 校驗返回數(shù)據(jù)的關(guān)鍵值
should be equal as strings 2.0 ${json[1]["jsonrpc"]}
所有的返回值都是字符串類型,轉(zhuǎn)成json后就可以像字典型一樣隨意取用了
${json} to json ${resp.content}
把取到值當(dāng)成下一次調(diào)用的入?yún)?/p>
建議每個testcase只測試一個功能,接口測試階段,不要構(gòu)造過于復(fù)雜的場景(用例代碼超過一屏將被視為復(fù)雜,需要拆分)
請求http獲得的返回都是字符串類型,解析成json才可以自由取用值
提供多種校驗函數(shù)如下:should be equal as integers should be equal as numbers should be equal as strings should be true should be empty should contain should contain any # not 也支持 should not be equal as integers should not be equal as numbers should not be equal as strings should not be true should not be empty should not contain should not contain any
robot宣稱支持數(shù)據(jù)驅(qū)動,支持按照template從表格讀數(shù)據(jù)來跑用例,但是問題是,如果要對數(shù)據(jù)表格里的case做揀選和拆分就會無力了,所以,數(shù)據(jù)驅(qū)動適合比較大規(guī)模的協(xié)議測試,而對于有細力度的統(tǒng)計case的需求是不好用的,我們的建議是用測試數(shù)據(jù)分散在testcase里,保持case的獨立性和靈活性,如果確實有大量的手工操作的話,提供小工具自動創(chuàng)建testcase。
數(shù)據(jù)驅(qū)動即為定義好流程(Template),然后就可以按照參數(shù)羅列形成用例了,每一條數(shù)據(jù)都會獨立執(zhí)行
*** Test Cases ***
Invalid Username
[Tags] Iteration-3 Smoke
[Template] Login with invalid credentials should fail
invalidname qwe123
091931 invalidpassword
*** Test Cases ***
Login Success
[Tags] Iteration-3 Smoke core
[Template] Login success
068970 qwe123
052255 qwe123
一個用例文件中包含很多testcase,我想統(tǒng)一加些動作該怎么辦?框架提供兩個方法,如果是前置條件(每個用例開始前執(zhí)行)就用Set up,在setting里編寫你的動作即可,如果動作很復(fù)雜,可以做成關(guān)鍵字調(diào)用即可,就像下面一樣:
*** Settings ***
Test Setup Get config
*** Keywords ***
Get config
log 正在初始化...
set suite variable ${config} {"env":"uat", "user":"haha"}
如果是后置條件(每個testcase結(jié)束后調(diào)用),就用Tear down,樣例如下:
*** Settings ***
Test Teardown Do clean
*** Keywords ***
Do clean
log 正在清理...
框架提供自定義的關(guān)鍵字方案,只需要在Keywords下面書寫你的邏輯動作即可,關(guān)鍵字可以是英文,也可以是中文,頂格寫,不限制空格和特殊字符,你可以像書寫自然語言一樣寫用例代碼啦
*** Keywords ***
My first keywords
log hello keywords
我的第一個關(guān)鍵字
log 你好, 我的關(guān)鍵字
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。