requests接口測(cè)試

1.介紹:

Requests是一個(gè)很實(shí)用的Python HTTP客戶端庫(kù),用來(lái)做接口測(cè)試

*做接口測(cè)試前需要pip install requests

2.get請(qǐng)求

一、無(wú)參請(qǐng)求

r=requests.get('http://www.baidu.com')

二、get傳參

payload={'key1':'value1','key2':'value2','key3':None}

r=requests.get('http://www.baidu.com ',params=payload)

3.post請(qǐng)求

payload={'key1':'value1','key2':'value2'}

r=requests.post("http://httpbin.org/post",data=payload)

4.requests響應(yīng)

r.status_code響應(yīng)狀態(tài)碼

r.heards響應(yīng)頭

r.cookies響應(yīng)cookies

r.text響應(yīng)文本

r.encoding當(dāng)前編碼

r.content以字節(jié)形式(二進(jìn)制)返回

5.requests擴(kuò)充

1.添加等待時(shí)間

requests.get(url,timeout=1)#超過(guò)等待時(shí)間則報(bào)錯(cuò)

2.添加請(qǐng)求頭信息

requests.get(url,headers=headers)#設(shè)置請(qǐng)求頭

3.添加文件

requests.post(url,files=files)#添加文件

6.requests+pytest+allure

1.步驟如下:

讀取文件中的數(shù)據(jù)

requests拿到數(shù)據(jù)請(qǐng)求接口返回狀態(tài)碼

通過(guò)斷言驗(yàn)證返回狀態(tài)碼和200對(duì)比

生成allure的測(cè)試報(bào)告

也可以這樣說(shuō):

dataDemo(存放數(shù)據(jù))>> readDemo(讀取數(shù)據(jù))

useRequests(發(fā)送請(qǐng)求)>>testDemo(生成報(bào)告)

2.讀取csv數(shù)據(jù)流程

1.通過(guò)excel另存為csv

2.讀取

importcsv

classReadCsv():

defreadCsv(self):

item= []

rr=csv.reader(open("../dataDemo/123.csv"))

forcsv_iinrr:

item.append(csv_i)

item=item[1:]

returnitem

3.requests請(qǐng)求接口返回狀態(tài)碼

importrequests

fromreadDataDemo.readcsvimportReadCsv

r=ReadCsv()

ee=r.readCsv()

# print(ee)

classRequestCsv():

defrequestsCsv(self):

item= []

forcsv_iinee:

ifcsv_i[2] =="get":

rr=requests.get(csv_i[0],params=csv_i[1])

item.append(rr.status_code)

else:

rr=requests.post(csv_i[0],data=csv_i[1])

item.append(rr.status_code)

returnitem

4.生成測(cè)試報(bào)告

3.讀取excel文件流程

1.新建excel文件

2.讀取數(shù)據(jù),安裝pip install openpyxl

class ReadXlsx: ? ?

def readXlsx(self): ? ? ?

?? wordbook=load_workbook("絕對(duì)路徑") ? ? ? ?

?? sheet = wordbook["requests"] ? ? ? ?

?? testdata = [] ? ? ? ?

?? for i in range(2, sheet.max_row + 1): ? ? ? ? ? ?

? ? ?? d = {} ? ? ? ? ? ?

? ? ?? for j in range(1, sheet.max_column + 1): ? ? ? ? ? ? ? ? ? ? ? ? ?? d[sheet.cell(1, j).value] = sheet.cell(i, j).value ? ? ? ? ? ? ?? testdata.append(d) ? ? ? ? ? ?

? ? return testdata

3.requests請(qǐng)求接口返回狀態(tài)碼

importrequests

fromrequestdemo.readexcelimportReadxcel

classGetStatusCode():

defgetStatusCode(self):

t=Readxcel()

f=t.getTestExcel()

item= []

forexcel_iinf:

ifexcel_i["method"] =="get":

rr=requests.get(excel_i["url"],params=excel_i["params"])

item.append(rr.status_code)

else:

rr=requests.post(excel_i["url"],data=excel_i["params"])

item.append(rr.status_code)

returnitem

print(GetStatusCode().getStatusCode())

4.生成測(cè)試報(bào)告

importallure,pytest,os

fromrequestdemo.getStatusCodeimportGetStatusCode

get=GetStatusCode()

statusCodes=get.getStatusCode()

classTestReadExcel():

deftestReadExcel(self):

forcodeinstatusCodes:

assertcode==200

if__name__=="__main__":

# 生成測(cè)試報(bào)告json

pytest.main(["-s","-q",'--alluredir','report/result','testreadexcel.py'])

# 將測(cè)試報(bào)告轉(zhuǎn)為html格式

split='allure '+'generate '+'./report/result '+'-o '+'./report/html '+'--clean'

os.system(split)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、介紹 Requests是一個(gè)很實(shí)用的Python HTTP客戶端庫(kù),編寫爬蟲和測(cè)試服務(wù)器響應(yīng)數(shù)據(jù)時(shí)經(jīng)常會(huì)用到,...
    老友_9e40閱讀 289評(píng)論 0 0
  • 1.介紹: Requests是一個(gè)很實(shí)用的Python HTTP客戶端庫(kù),用來(lái)做接口測(cè)試 *做接口測(cè)試前需要pip...
    我向星明許愿閱讀 199評(píng)論 0 0
  • 1.介紹: Requests是一個(gè)很實(shí)用的Python HTTP客戶端庫(kù),用來(lái)做接口測(cè)試 *做接口測(cè)試前需要pip...
    ZhaoyiMing閱讀 284評(píng)論 0 0
  • 1.介紹: Requests是一個(gè)很實(shí)用的Python HTTP客戶端庫(kù),用來(lái)做接口測(cè)試 *做接口測(cè)試前需要pip...
    小董小董閱讀 190評(píng)論 0 0
  • 十七單元 requests接口測(cè)試 1.介紹: Requests是一個(gè)很實(shí)用的Python HTTP客戶端庫(kù),用來(lái)...
    何雅軒閱讀 364評(píng)論 0 0

友情鏈接更多精彩內(nèi)容