設計理念:
- 充分復用優(yōu)秀的開源項目,不追求重復造輪子,而是將強大的輪子組裝成戰(zhàn)車
- 遵循 “約定大于配置” 的準則,在框架功能中融入自動化測試最佳工程實踐
- 追求投入產(chǎn)出比,一份投入即可實現(xiàn)多種測試需求
在 HttpRunner 中,測試用例組織主要基于三個概念:
- 測試用例集(testsuite):對應一個文件夾,包含單個或多個測試用例(YAML/JSON)文件
- 測試用例(testcase):對應一個 YAML/JSON 文件,包含單個或多個測試步驟
- 測試步驟(teststep):對應 YAML/JSON 文件中的一個 test,描述單次接口測試的全部內(nèi)容,包括發(fā)起 接口請求、解析響應結(jié)果、校驗結(jié)果等
Quick Start
- a. 創(chuàng)建項目:$ hrun --startproject projectName
- b. 項目分層:api、data、testcases、testsuites、debugtalk.py、.env、requirements.txt
api: 定義單個請求信息
data: 測試過程中使用的參數(shù),如參數(shù)化 parameters 數(shù)據(jù)驅(qū)動
testcases: 多個api組成的測試用例,也可以是單個
testsuites:單個或多個測試無序用例集
debugtalk.py:公共函數(shù)、hook函數(shù)定義
.env: 環(huán)境配置信息
requirements.txt: 環(huán)境依賴相關(guān) pip install -r requirements.txt
一、API文件概述
api文件:描述請求相關(guān)內(nèi)容
關(guān)鍵字:name、variables、request、base_url、validate、extract
name: api 名稱描述
base_url: \${ENV(LOGIN_URL)} 固定寫法
variables:定義api變量用于下面的 request 調(diào)用
request: 請求對象
url: 接口地址
method: 請求方法
headers: 請求頭
params: 請求頭參數(shù),一般用于get請求
data: Content-type: application/x-www-form-urlencoded
json: Content-Type: application/json;charset=UTF-8
files: Content-Type: multipart/form-data
upload: 一般用于簡單圖片上傳同files類似更為簡單
validate: 驗證斷言結(jié)果
extract: 提取參數(shù), $token的形式進行引用
`注意點:其中,name 和 request 部分是必須的,request 中的描述形式與 requests.request 完全相同,另外API 描述需要盡量保持完整,做到可以單獨運行.`
在開始測試之前,我們需要先了解 接口的請求和響應細節(jié) ,而最佳的方式就是采用 Charles Proxy 或者 Fiddler 這類網(wǎng)絡抓包工具進行抓包分析。導出HAR格式文件,在CMD中運行 har2case 自動解析生成用例:
har2case docs/data/demo-quickstart.har 不帶參數(shù)默認json格式
har2case docs/data/demo-quickstart.har -2y 加上 -2y/--to-yml 參數(shù)
轉(zhuǎn)換為 YAML 格式測試用例
詳細可以參考一下:錄制生成用例
api 編寫規(guī)范:
1. name 描述接口的具體功能,增強接口可讀性.
2. 在 api 內(nèi)盡量提供參數(shù)實例說明便于協(xié)作 ,默認傳參跟實際業(yè)務保持一致.
3. requests 中傳參盡可能的進行變量引用,避免參數(shù)寫死,便于后續(xù)參數(shù)分離多環(huán)境執(zhí)行.
4. 盡量不要在API文件里提取返回參數(shù) // token 之類的除外
5. 遵循restful規(guī)范的API,盡量寫成1個API文件,如果傳參比較復雜則可以根據(jù)請求方式(GET/POST/PUT/DELETE)寫成多個
6. API 文件中的斷言校驗,僅校驗常規(guī)字段,實際業(yè)務斷言放在測試用例中體現(xiàn).
7. 注釋必須要有!!! // 所有業(yè)務字段必須在api中說明
二、testcases/testsuites概述
關(guān)鍵字:config、teststeps
config:
name: 測試用例描述
variables_ 變量定義
setup_hooks: 全局用例測試之前執(zhí)行,局部執(zhí)行直接在teststeps定義即可
teardown_hooks: 全局用例測試之后執(zhí)行,局部執(zhí)行直接在teststeps定義即可_默認False, 主要針對https請求
output: 變量輸出,字符串引用,在日志debug模式顯示,作用于export類似
teststeps:
name: 步驟api描述1
api: 需要調(diào)用對應的api路徑+文件名.yml/.json
validate: 斷言驗證
#其他api中關(guān)鍵字也可以使用
測試套件(集合testsuites)
關(guān)鍵字:config testcases
config:
name: testsuite description
testcases: 用例集
testcase1_name: 用例1
testcase: /path/to/testcase1 #測試用例path
testcase2_name: 用例2
testcase: /path/to/testcase2 #測試用例path
注意點:關(guān)鍵字段的縮進和yml文件的語法.`
三、validate校驗器方法源碼:
def get_uniform_comparator(comparator):
""" convert comparator alias to uniform name
"""
if comparator in ["eq", "equals", "==", "is"]:
return "equals"
elif comparator in ["lt", "less_than"]:
return "less_than"
elif comparator in ["le", "less_than_or_equals"]:
return "less_than_or_equals"
elif comparator in ["gt", "greater_than"]:
return "greater_than"
elif comparator in ["ge", "greater_than_or_equals"]:
return "greater_than_or_equals"
elif comparator in ["ne", "not_equals"]:
return "not_equals"
elif comparator in ["str_eq", "string_equals"]:
return "string_equals"
elif comparator in ["len_eq", "length_equals", "count_eq"]:
return "length_equals"
elif comparator in ["len_gt", "count_gt", "length_greater_than", "count_greater_than"]:
return "length_greater_than"
elif comparator in ["len_ge", "count_ge", "length_greater_than_or_equals",
"count_greater_than_or_equals"]:
return "length_greater_than_or_equals"
elif comparator in ["len_lt", "count_lt", "length_less_than", "count_less_than"]:
return "length_less_than"
elif comparator in ["len_le", "count_le", "length_less_than_or_equals",
"count_less_than_or_equals"]:
return "length_less_than_or_equals"
else:
return comparator
總結(jié)下相關(guān)的校驗方法:
eq equals: 判斷實際結(jié)果和期望結(jié)果是否相等,可以用”eq”, “equals”, “==”, “is”
lt less_than: 判斷實際結(jié)果小于期望結(jié)果 ,可以用 “l(fā)t”, “l(fā)ess_than”
le less_than_or_equals: 判斷實際結(jié)果小于等于期望結(jié)果 ,可以用 “l(fā)e”, “l(fā)ess_than_or_equals”
gt greater_than: 判斷實際結(jié)果大于期望結(jié)果,可以用”gt”, “greater_than”
ge greater_than_or_equals: 判斷實際結(jié)果大于等于期望結(jié)果,可以用”ge”, “greater_than_or_equals”
ne not_equals: 判斷實際結(jié)果和期望結(jié)果不相等,可以用”ne”, “not_equals”
str_eq string_equals: 判斷轉(zhuǎn)字符串后對比 實際結(jié)果和期望結(jié)果是否相等,可以用”str_eq”, “string_equals”
len_eq length_equals: 判斷字符串或list長度,可以用”len_eq”, “l(fā)ength_equals”, “count_eq”
en_gt length_greater_than: 判斷實際結(jié)果的長度大于和期望結(jié)果,可以用”len_gt”, “count_gt”, “l(fā)ength_greater_than”, “count_greater_than”
len_ge length_greater_than_or_equals: 實際結(jié)果的長度大于等于期望結(jié)果,可以用”len_ge”, “count_ge”, “l(fā)ength_greater_than_or_equals”, “count_greater_than_or_equals”
len_lt length_less_than: 實際結(jié)果的長度小于期望結(jié)果,可以用”len_lt”, “count_lt”, “l(fā)ength_less_than”, “count_less_than”
len_le length_less_than_or_equals : 實際結(jié)果的長度小于等于期望結(jié)果,可以用”len_le”, “count_le”, “l(fā)ength_less_than_or_equals”, “count_less_than_or_equals”
實際demo案例:
pipline




