安裝request庫
以火車的站站查詢爲例的post和get方法的接口測試
使用pytest測試接口
1、requests的請求機制
1、安裝request庫
2、以火車的站站查詢爲例的post和get請求方法
2.1get請求:
兩種傳參方式
1、_url = “網址+參數” = “網址?key1=value1&key2=value2”
response1 = request.get(url = _url)
2、字典拼接
_params = {
“key1” : “value1”,
“key2” : “value2”,
}
response2 = requests.get(url=“網址”, params = _params)
import requests
response = requests.get(url="https://api.binstd.com/train/station2s?start=北京&end=西安&ishigh=0&appkey=d737aad9a0d9dc97")
print(response.text) #字符串格式
print(response.json()) #json,前提需要確保返回內容爲json格式,否則報錯
#字典方式拼接參數
print("-------------字典方式拼接參數---------------")
params = {
"start" : "北京",
"end" : "西安",
"ishigh" : 0 ,
"appkey" : "d737aad9a0d9dc97"
}
response1 = requests.get(url="https://api.binstd.com/train/station2s", params = params)
print(response1.text)
print(response1.json())
2.2post請求
拼接參數方式傳參
import requests
#字典方式拼接參數
data = {
"start" : "北京",
"end" : "西安",
"ishigh" : 0 ,
"appkey" : "d737aad9a0d9dc97"
}
response1 = requests.post(url="https://api.binstd.com/train/station2s", data = data)
print(response1.text)
print(response1.json())
#獲取響應狀態碼
print(response1.status_code)
#獲取原始模式
print(response1.raw)
常見的請求方法
請求方法 | 含義 |
---|---|
requests.get() | 獲取html的主要方法 |
requests.head() | 獲取html頭部信息的主要方法 |
requests.post() | 向html網頁提交post請求的方法 |
requests.put() | 向html網頁提交put請求的方法 |
requests.patch() | 向html提交局部修改的請求 |
requests.delete() | 向html提交刪除請求 |
2、pytest測試接口
1、安裝pytest
pip install pytest
2、使用pytest測試接口
在pytest框架中,有如下約束:
文件名要以test開頭或者結尾(test_*.py / *_test.py),可以包含一個或多個test_開頭的函數。
此時,在執行pytest命令時,會自動從當前目錄及子目錄中尋找符合上述約束的測試函數來執行。
4.1首先得到響應數據
import requests
def request_ticket():
#返回接口響應結果
url = "https://api.binstd.com/train/ticket"
payload = {
"start": "北京",
"end": "西安",
"date": "2019-10-1",
"appkey": "d737aad9a0d9dc97"
}
#response = requests.get(url = _url, parms = payload)
response = requests.post(url = url, data = payload)
print(response.text)
return response
request_ticket()
4.2爲了方便查看將響應結果格式化:由於太長,部分用省略號代替
{
"status": 0,
"msg": "ok",
"result": {
"start": "北京",
"end": "西安",
"date": "2020-06-10",
"list": [
{
"trainno": "G667",
"type": "G",
"typename": "高鐵",
"station": "北京西",
"endstation": "西安北",
"departuretime": "11:19",
...
"departstationcode": "BXP",
"terminalstationcode": "EAY",
"startdate": "20200610",
...
},
{
"trainno": "G659",
"type": "G",
"typename": "高鐵",
"station": "北京西",
"endstation": "西安北",
"departuretime": "11:53",
...
"departstationcode": "BXP",
"terminalstationcode": "EAY",
"startdate": "20200610",
...
},
{...},
{...},
...
]
}
}
4.3取出數據
出發站(station)和到達站(endstation)在result中的list下,怎麼取到呢?----[“result”] [“list”]
---- request_ticket().json()[“result”][“list”]
def test_departur_station():
"""
始發站測試,測試接口返回的所有車次信息,他們的出發站,和到達站都符合參數約定
:return:
"""
#從響應中獲取測試列表
trainSli = request_ticket().json()["result"]["list"] #單個的車次信息
#trainSli是取出來的list列表
for trainInfo in trainSli:
assert "北京" in trainInfo["station"] #判斷‘北京’是否是列表中‘station’的值
assert "西安" in trainInfo["endstation"] #判斷到達站是不是‘西安’
#調用函數
test_departur_station()
'''def test_train_date():
"""
發車日期測試,接口返回的所有車次信息,發車日期,都符合參數約定
:return:
"""
#從響應中獲取測試列表
trainSli = request_ticket().json()["result"]["list"] #單個的車次信息
for trainInfo in trainSli:
assert "20200610" in trainInfo["startdate"]
test_train_date()'''
4.4 運行
4.5 查看結果
如果該路徑下有多個以test開頭或者結尾的文件,則會一起檢測兩個文件中的接口
如果出現ERROR則在文件中找錯誤原因