pycharm中使用request和Pytest進(jìn)行接口測(cè)試的方法
安裝request庫(kù)以火車的站站查詢?yōu)槔膒ost和get方法的接口測(cè)試使用pytest測(cè)試接口
1、requests的請(qǐng)求機(jī)制
1、安裝request庫(kù)
2、以火車的站站查詢?yōu)槔膒ost和get請(qǐng)求方法
2.1get請(qǐng)求:
兩種傳參方式
1、_url = “網(wǎng)址+參數(shù)” = “網(wǎng)址?key1=value1&key2=value2”
response1 = request.get(url = _url)
2、字典拼接
_params = {“key1” : “value1”,“key2” : “value2”,}response2 = requests.get(url=“網(wǎng)址”, 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,前提需要確保返回內(nèi)容為json格式,否則報(bào)錯(cuò)#字典方式拼接參數(shù)print('-------------字典方式拼接參數(shù)---------------')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請(qǐng)求拼接參數(shù)方式傳參
import requests#字典方式拼接參數(shù)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())#獲取響應(yīng)狀態(tài)碼print(response1.status_code)#獲取原始模式print(response1.raw)
常見(jiàn)的請(qǐng)求方法
請(qǐng)求方法 含義 requests.get() 獲取html的主要方法 requests.head() 獲取html頭部信息的主要方法 requests.post() 向html網(wǎng)頁(yè)提交post請(qǐng)求的方法 requests.put() 向html網(wǎng)頁(yè)提交put請(qǐng)求的方法 requests.patch() 向html提交局部修改的請(qǐng)求 requests.delete() 向html提交刪除請(qǐng)求
2、pytest測(cè)試接口
1、安裝pytestpip install pytest
2、使用pytest測(cè)試接口在pytest框架中,有如下約束:文件名要以test開(kāi)頭或者結(jié)尾(test_*.py / *_test.py),可以包含一個(gè)或多個(gè)test_開(kāi)頭的函數(shù)。此時(shí),在執(zhí)行pytest命令時(shí),會(huì)自動(dòng)從當(dāng)前目錄及子目錄中尋找符合上述約束的測(cè)試函數(shù)來(lái)執(zhí)行。
4.1首先得到響應(yīng)數(shù)據(jù)
import requestsdef request_ticket(): #返回接口響應(yīng)結(jié)果 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為了方便查看將響應(yīng)結(jié)果格式化:由于太長(zhǎng),部分用省略號(hào)代替
{ '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取出數(shù)據(jù)出發(fā)站(station)和到達(dá)站(endstation)在result中的list下,怎么取到呢?----[“result”] [“l(fā)ist”]---- request_ticket().json()[“result”][“l(fā)ist”]
def test_departur_station(): ''' 始發(fā)站測(cè)試,測(cè)試接口返回的所有車次信息,他們的出發(fā)站,和到達(dá)站都符合參數(shù)約定 :return: ''' #從響應(yīng)中獲取測(cè)試列表 trainSli = request_ticket().json()['result']['list'] #單個(gè)的車次信息 #trainSli是取出來(lái)的list列表 for trainInfo in trainSli: assert '北京' in trainInfo['station'] #判斷‘北京’是否是列表中‘station’的值 assert '西安' in trainInfo['endstation'] #判斷到達(dá)站是不是‘西安’#調(diào)用函數(shù)test_departur_station()’’’def test_train_date(): ''' 發(fā)車日期測(cè)試,接口返回的所有車次信息,發(fā)車日期,都符合參數(shù)約定 :return: ''' #從響應(yīng)中獲取測(cè)試列表 trainSli = request_ticket().json()['result']['list'] #單個(gè)的車次信息 for trainInfo in trainSli: assert '20200610' in trainInfo['startdate'] test_train_date()’’’
4.4 運(yùn)行
4.5 查看結(jié)果
如果該路徑下有多個(gè)以test開(kāi)頭或者結(jié)尾的文件,則會(huì)一起檢測(cè)兩個(gè)文件中的接口
如果出現(xiàn)ERROR則在文件中找錯(cuò)誤原因
總結(jié)
到此這篇關(guān)于pycharm中使用request和Pytest進(jìn)行接口測(cè)試的文章就介紹到這了,更多相關(guān)pycharm使用request和Pytest接口測(cè)試內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. PHP設(shè)計(jì)模式中工廠模式深入詳解2. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析3. .NET中l(wèi)ambda表達(dá)式合并問(wèn)題及解決方法4. 解決AJAX返回狀態(tài)200沒(méi)有調(diào)用success的問(wèn)題5. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)6. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明7. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法8. CSS hack用法案例詳解9. Ajax實(shí)現(xiàn)表格中信息不刷新頁(yè)面進(jìn)行更新數(shù)據(jù)10. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向
