關(guān)于python實(shí)現(xiàn)requests接口測(cè)試的問(wèn)題
requests接口測(cè)試的介紹
requests是一個(gè)很實(shí)用的Python HTTP客戶端庫(kù),編寫(xiě)爬蟲(chóng)和測(cè)試服務(wù)器響應(yīng)數(shù)據(jù)時(shí)經(jīng)常會(huì)用到,Requests是Python語(yǔ)言的第三方的庫(kù),專門用于發(fā)送HTTP請(qǐng)求
requests接口測(cè)試的使用前提
pip install requests
1.requests中的get請(qǐng)求
1 GET無(wú)參請(qǐng)求
r = requests.get('http://www.baidu.com')
案例:
import requests class Classrequset: def Claete(self): r = requests.get('http://www.baidu.com') print(r.text) a=Classrequset() a.Claete()
2.GET傳參
payload = {'key1': 'value1', 'key2': 'value2', 'key3': None} r = requests.get('http://www.baidu.com ', params=payload)
案例:
def XWTTMethod(self): params = {"type": "guonei", "key": "4b72107de3a197b3bafd9adacf685790"} r = requests.get("http://v.juhe.cn/toutiao/index", params=params) print(r.text) a=Classrequset() a.XWTTMethod()
2.requests中的post請(qǐng)求
payload = {'key1': 'value1', 'key2': 'value2'} r = requests.post("http://httpbin.org/post", data=payload)
案例:
def XWTTMethodpost(self): uripost="http://v.juhe.cn/toutiao/index" datapost={"type":"youxi","page":"1","size":"10","key":"ff64bdb75dd1fbc636724101514cfbe7"} r =requests.post(url=uripost,data=datapost) print(r.text) # print(r.status_code) #這是查看狀態(tài)碼的 a=Classrequset() a.XWTTMethodpost()
3.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)制)返回
最常用的是根據(jù)響應(yīng)狀態(tài)碼判斷接口是否連通,經(jīng)常用于做接口中斷言判斷
4.Request擴(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) #添加文件
文件傳輸
url = 'http://httpbin.org/post' files = {'file': open('report.xls', 'rb')} r = requests.post(url, files=files)
5.python實(shí)現(xiàn)requests+pytest+allure的操作
1 流程如下
讀取文件中的數(shù)據(jù) requests拿到數(shù)據(jù)請(qǐng)求接口返回狀態(tài)碼通過(guò)斷言驗(yàn)證返回狀態(tài)碼和200對(duì)比生成allure的測(cè)試報(bào)告
6.讀取csv文件流程
1 存儲(chǔ)數(shù)據(jù)(csv)
2 讀取數(shù)據(jù)(readDemo)
import csv class ReadCsv(): def readCsv(self): item = [] rr = csv.reader(open("../request/1212223.csv")) for csv_i in rr: item.append(csv_i) return item a=ReadCsv() print(a.readCsv())
3 request請(qǐng)求接口返回狀態(tài)碼
from request.dataDemo import ReadCsv import requests r=ReadCsv() ee=r.readCsv() ltms=[] class RequestClass: def requesthome(self): for a in ee: if a[2]=="get": ss=requests.get(url=a[0],params=a[1]) ltms.append(ss.status_code) else: ss=requests.post(url=a[0],data=a[1]) ltms.append(ss.status_code) return ltms q=RequestClass() print(q.requesthome())
4 pytest斷言設(shè)置并結(jié)合allure生成測(cè)試報(bào)告
import pytest, allure, os from request.request03_csv import RequestClass r = RequestClass() aa = r.requesthome() class TestRequest: def testcvsHose(self): for s in aa: assert s == 200 if __name__ == '__main__': pytest.main(['--alluredir','report/result','requests_test.py']) split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean' os.system(split)
5 測(cè)試報(bào)告展示
7.讀取excle文件流程
1 存儲(chǔ)數(shù)據(jù)(xlsx)
2 讀取數(shù)據(jù)(readDemo)
from openpyxl import load_workbook class UseExcel(): def get_TestExcel(self): # 打開(kāi)表 workbook = load_workbook('./777.xlsx') # 定位表單 sheet = workbook['Sheet1'] print(sheet.max_row) #3 行 print(sheet.max_column) #3 列 test_data = []#把所有行的數(shù)據(jù)放到列表中 for i in range(2,sheet.max_row+1): sub_data = {}#把每行的數(shù)據(jù)放到字典中 for j in range(1,sheet.max_column+1): sub_data[sheet.cell(1,j).value] = sheet.cell(i,j).value test_data.append(sub_data)#拼接每行單元格的數(shù)據(jù) return test_data t = UseExcel() f = t.get_TestExcel() print(f)
3.request請(qǐng)求接口返回狀態(tài)碼
import requests from request.requestxls import UseExcel a=UseExcel() f = a.get_TestExcel() item = [] class Use_Requestexcel(): def qualification_mord(self): for excel_i in f: if excel_i["method"] == "get": rr = requests.get(url=excel_i["url"],params=excel_i["paras"]) item.append(rr.status_code) else: rr = requests.post(url=excel_i["url"],data=excel_i["paras"]) item.append(rr.status_code) return item r=Use_Requestexcel()
4 pytest斷言設(shè)置并結(jié)合allure生成測(cè)試報(bào)告
import pytest, allure, os from request.requestextes import Use_Requestexcel r = Use_Requestexcel() aa = r.qualification_mord() print(aa) class Testrequest: def testcvsHose(self): for s in aa: assert s == 200 if __name__ == '__main__': pytest.main(['--alluredir','report/result','test_req.py']) split = 'allure ' + 'generate ' + './report/result ' + '-o ' + './report/html ' + '--clean' os.system(split)
5 測(cè)試報(bào)告展示
到此這篇關(guān)于python實(shí)現(xiàn)requests接口測(cè)試的文章就介紹到這了,更多相關(guān)python requests接口測(cè)試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python自定義一個(gè)類實(shí)現(xiàn)字典dict功能的方法
今天小編就為大家分享一篇Python自定義一個(gè)類實(shí)現(xiàn)字典dict功能的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01由淺入深學(xué)習(xí)TensorFlow MNIST 數(shù)據(jù)集
這篇文章主要由淺入深學(xué)習(xí)的講解TensorFlow MNIST 數(shù)據(jù)集,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09python 使用OpenCV進(jìn)行簡(jiǎn)單的人像分割與合成
這篇文章主要介紹了python 使用OpenCV進(jìn)行簡(jiǎn)單的人像分割與合成的方法,幫助大家更好的利用python處理圖像,感興趣的朋友可以了解下2021-02-02python字典如何獲取最大和最小value對(duì)應(yīng)的key
這篇文章主要介紹了python字典如何獲取最大和最小value對(duì)應(yīng)的key問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Python基于鏈接表實(shí)現(xiàn)無(wú)向圖最短路徑搜索
鏈接表的存儲(chǔ)相比較鄰接炬陣,使用起來(lái)更方便,對(duì)于空間的使用是剛好夠用原則,不會(huì)產(chǎn)生太多空間浪費(fèi)。所以本文將以鏈接表方式實(shí)現(xiàn)無(wú)向圖最短路徑搜索,需要的可以參考一下2022-04-04Pygame用200行代碼實(shí)現(xiàn)俄羅斯方塊
俄羅斯方塊的邏輯很簡(jiǎn)單,就是幾個(gè)方塊組合在一起,然后下落,當(dāng)其碰到四周的墻壁后便無(wú)法移動(dòng),若某行被方塊所填滿,那么就刪除這一行,然后此行上面的所有方塊下降一行,本文給大家介紹了用Pygame實(shí)現(xiàn)俄羅斯方塊,文中代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12對(duì)Python 網(wǎng)絡(luò)設(shè)備巡檢腳本的實(shí)例講解
下面小編就為大家分享一篇對(duì)Python 網(wǎng)絡(luò)設(shè)備巡檢腳本的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04不到20行代碼用Python做一個(gè)智能聊天機(jī)器人
小編先向大家介紹一下本次運(yùn)用到的python庫(kù),本次項(xiàng)目主要運(yùn)用到的庫(kù)有wxpy和chatterbot。對(duì)Python做一個(gè)智能聊天機(jī)器人的相關(guān)知識(shí)感興趣的朋友跟隨小編一起看看吧2019-04-04