HttpRunner2.X開源介面測試框架學習(一):yaml格式測試用例編寫
阿新 • • 發佈:2021-06-17
HttpRunner測試用例的基本結構
-
每個YAML/JSON檔案對應一個測試用例(testcase)
-
每個測試用例為一個list of dict結構,其中可能包含全域性配置項(config)和若干個測試步驟(test)
-
config為全域性配置項,作用域為整個測試用例
-
test對應單個測試步驟,作用域僅限於本身
- 變數作用域以config為主,config如果沒有設定,就使用test中設定的變數
一、基礎栗子
get請求舉例說明
以開啟新夢想首頁為例
host:http://www.hnxmxit.com
url:/
請求方式:get
使用httprunner進行介面測試步驟:
1、開啟pycharm,新建一個httprunner專案,新建一個test_demo.yaml檔案,內容如下:
#不帶引數的get請求
- config:
name: 驗證能否開啟新夢想主頁
base_url: http://www.hnxmxit.com
- test:
name: open hnxmxit mainpage api
request:
url: /
method: GET
validate:
- eq: ['status_code',200]
2、在pycharm,終端視窗執行如下命令 :hrun yaml檔名
3、執行成功後,會自動生成一個報告,檢視報告:
二、模擬帶引數的get請求
#模擬帶引數的get請求
- config:
name: '驗證能否獲取token值'
base_url: 'https://api.weixin.qq.com'
- test:
name: 'get access token'
request:
url: '/cgi-bin/token'
method: GET
params:
grant_type: 'client_credential'
appid: 'wx0ebbdf4a197121'
secret: 'b876eeb2af99cc6623995201168e702f'
validate:
- eq: ['status_code',200]
- eq: [content.expires_in,7200]
三、模擬請求頭
#模擬請求頭
- config:
name: '驗證百度搜索是否正確'
base_url: 'https://www.baidu.com'
- test:
name: '百度搜索請求'
request:
url: '/s'
method: GET
headers:
User-Agent: 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Mobile Safari/537.36'
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
Accept-Encoding: 'gzip, deflate, br'
Accept-Language: 'zh-CN,zh;q=0.9'
params:
wd: 'newdream01'
validate:
- eq: ['status_code',200]
四、模擬POST請求
#模擬post請求
- config:
name: '驗證微信開發平臺中,使用者標籤管理修改標籤介面能否成功執行'
base_url: 'https://api.weixin.qq.com'
- test:
name: '修改標籤介面'
request:
url: '/cgi-bin/tags/update'
method: POST
headers:
Content-Type: 'application/json'
params:
access_token: '46_oMQuTV1IE2aTFeGKMHXHSNseS63bwEMxSyU7MrVYvOqXF1y5hnQvgopuZjBSqKBIGVrCaTlQEAeAAf-EtaGWahHTeEzFU-cNZ3_EgT7Xlbwx7rOFW9OTusypC9lIthaIn0Ooq60XJ09wrFYlJMIgAHAVYU'
json: { "tag" : { "id" : 101, "name" : "newdream123" } }
validate:
- eq: ['status_code',200]
- eq: [content.errcode,0]
五、變數作用域:變數作用域以config為主,config如果沒有設定,就使用test中設定的變數
#變數作用域:變數作用域以config為主,config如果沒有設定,就使用test中設定的變數
- config:
name: '驗證能否獲取token值'
base_url: 'https://api.weixin.qq.com'
variables:
- grant_type: 'client_credential'
- appid: 'wx0ebbdf4a197121'
- test:
name: 'get access token'
variables: #變數作用域以config為主,config如果沒有設定,就使用test中設定的變數
- grant_type: 'client_credential'
- appid: 'wx0ebbdf4a197121'
request:
url: '/cgi-bin/token'
method: GET
params:
grant_type: $grant_type
appid: $appid
secret: 'b876eeb2af99cc6623995201168e702f'
validate:
- eq: ['status_code',200]
- eq: [content.expires_in,7200]