pycharm中使用request和Pytest進行接口測試的方法
安裝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 requestsresponse = 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、安裝pytestpip install pytest
2、使用pytest測試接口在pytest框架中,有如下約束:文件名要以test開頭或者結尾(test_*.py / *_test.py),可以包含一個或多個test_開頭的函數。此時,在執行pytest命令時,會自動從當前目錄及子目錄中尋找符合上述約束的測試函數來執行。
4.1首先得到響應數據
import requestsdef 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 responserequest_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則在文件中找錯誤原因
總結
到此這篇關于pycharm中使用request和Pytest進行接口測試的文章就介紹到這了,更多相關pycharm使用request和Pytest接口測試內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. Docker究竟是什么 為什么這么流行 它的優點和缺陷有哪些?2. idea打開多個窗口的操作方法3. IntelliJ IDEA設置編碼格式的方法4. IDEA 重新導入依賴maven 命令 reimport的方法5. 如何通過vscode運行調試javascript代碼6. Java14發布了,再也不怕NullPointerException了7. IntelliJ IDEA 2020.2正式發布,兩點多多總能助你提效8. Intellij IDEA 閱讀源碼的 4 個絕技(必看)9. IntelliJ IDEA 統一設置編碼為utf-8編碼的實現10. IntelliJ IDEA設置背景圖片的方法步驟
