接口測試2-接口測試 get post請求


一。找接口文檔(后端開發人員提供,前端沒有開發出來之前測)

接口文檔如下:

一、測試服務器信息

 測試服務器地址:https://api.weixin.qq.com

 二、接口列表

 2.1 獲取access_token

 

接口名稱

獲取access_token

接口描述

獲取access_token

請求地址

/cgi-bin/token

請求方式

get

輸入參數

 

參數名稱

參數描述

參數類型

是否必須

備注

grant_type

client_credential

String

必須

 

appid

wxaefa08168b458452

String

必須

 

secret

3d98210ddcdcaed24a3418585f1569dc

String

必須

 

 

 

 

 

 

輸出參數

 

 

 

 

參數名稱

參數描述

參數類型

 

備注

access_token

用戶的access_token

 

 

 

expires_in

過期時間

 

 

 

 

 

 

 

 

開發申請接口數據地址:https://www.juhe.cn/ 可以申請接口進行測試

 比如天氣預報(可免費調用500次)

 

 

 

 

二。設計接口測試用例

除了正常的測試用例,需要設計異常的用例

 

 

 

 

 

三。開展測試

第一步,使用python測試接口,需要安裝requests庫

打開電腦cmd  ---PIP

第二步,打開pycharm,導包編碼

編碼:

get類型請求接口
post類型請求接口
參數關聯接口
unittest實現接口測試
數據和代碼分離:xlrd--->讀取excel表數據

 

舉實例1:測試天氣預報接口-------根據id查詢天氣(get類型)

 一。分析需求,准備接口文檔:https://www.juhe.cn/docs/api/id/39,接口文檔如下:

 

二。編寫用例: 

 

三。接口測試python代碼編寫

1.用例1

 -----------------------------------------------------------------------------

# 導包
import requests

#給接口地址定義變量名稱
url = "http://v.juhe.cn/weather/index"
#通過字典的形式保存數據
para = {"cityname":"南京","key":"0f1c6ff6efc0408eddf0f07e792c6e5c"}

#發送請求(下面都是request自帶的方法)
r = requests.get(url,params=para)
#獲取返回結果,獲取json數據
print(r.status_code)
res = r.json()
print(res["reason"])
print(res["result"]["sk"]["temp"]) #獲取json中result中sk中的temp字段值
-----------------------------------------------------------

2.用例2 

 

 舉實例2:測試天氣預報接口-------根據GPS坐標自動定位城市,並返回該城市的天氣信息(post類型)

 一 需求

 

 

二。編寫python測試用例

-----------------------------------------

#導包
import requests
#根據gps坐標查詢天氣,使用post請求
url = "http://v.juhe.cn/weather/geo"
para = {"lon":"116.39277","lat":"39.933748","dtype":"json","key":"0f1c6ff6efc0408eddf0f07e792c6e5c"}
#發送請求,post請求中data就是para的數據,para數據包含所有的請求參數
r = requests.post(url,data=para)
#獲取json數據
res =r.json()
print(res)
print(res["reason"])

#輸入錯誤的請求查看返回碼
print(res["error_code"])
------------------------------

 


免責聲明!

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



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