關(guān)于python實(shí)現(xiàn)requests接口測(cè)試的問題
requests接口測(cè)試的介紹
requests是一個(gè)很實(shí)用的Python HTTP客戶端庫(kù),編寫爬蟲和測(cè)試服務(wù)器響應(yīng)數(shù)據(jù)時(shí)經(jīng)常會(huì)用到,Requests是Python語言的第三方的庫(kù),專門用于發(fā)送HTTP請(qǐng)求
requests接口測(cè)試的使用前提

pip install requests
1.requests中的get請(qǐng)求
1 GET無參請(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) #超過等待時(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)碼通過斷言驗(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):
# 打開表
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實(shí)現(xiàn)GeoServer矢量文件批量發(fā)布
由于矢量圖層文件較多,手動(dòng)發(fā)布費(fèi)時(shí)費(fèi)力,python支持的關(guān)于geoserver包又由于年久失修,無法在較新的geoserver版本中正常使用。本文為大家準(zhǔn)備了Python自動(dòng)化發(fā)布矢量文件的代碼,需要的可以參考一下2022-07-07
Python?np.where()的詳解以及代碼應(yīng)用
numpy里有一個(gè)非常神奇的函數(shù)叫做np.where()函數(shù),下面這篇文章主要給大家介紹了關(guān)于Python?np.where()的詳解以及代碼應(yīng)用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
關(guān)于python爬蟲應(yīng)用urllib庫(kù)作用分析
這篇文章主要介紹了關(guān)于python爬蟲應(yīng)用urllib庫(kù)作用分析,想要進(jìn)行python爬蟲首先我們需要先將網(wǎng)頁上面的信息給獲取下來,這就是utllib庫(kù)的作用,有需要的朋友可以借鑒參考下2021-09-09
Python實(shí)現(xiàn)識(shí)別XSS漏洞的方法詳解
XSS(跨站腳本攻擊)作為一種常見的網(wǎng)絡(luò)安全漏洞,經(jīng)常被黑客用來攻擊網(wǎng)站。這篇文章主要介紹了如何利用Python 識(shí)別 XSS 漏洞,需要的可以參考一下2023-02-02
Python學(xué)習(xí)工具jupyter notebook安裝及用法解析
這篇文章主要介紹了Python學(xué)習(xí)工具jupyter notebook安裝及用法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
python之matplotlib學(xué)習(xí)繪制動(dòng)態(tài)更新圖實(shí)例代碼
這篇文章主要介紹了python之matplotlib學(xué)習(xí)繪制動(dòng)態(tài)更新圖實(shí)例代碼,文中涉及具體實(shí)現(xiàn)代碼,演示效果及運(yùn)行時(shí)出現(xiàn)的問題分析等相關(guān)內(nèi)容,小編覺得還是挺不錯(cuò)的,這里分享給大家,需要的朋友可以參考下2018-01-01
Python中的yeild關(guān)鍵字提高代碼執(zhí)行效率場(chǎng)景實(shí)例探究
在Python編程語言中,yeild是一個(gè)非常實(shí)用的關(guān)鍵字,它不僅可以幫助你編寫更加簡(jiǎn)潔的代碼,還可以提高代碼的執(zhí)行效率,本文將詳細(xì)介紹yeild在Python中的使用方法,并通過示例代碼進(jìn)行演示,讓我們一起來探索這個(gè)強(qiáng)大的關(guān)鍵字吧2024-01-01
利用Python中的輸入和輸出功能進(jìn)行讀取和寫入的教程
這篇文章主要介紹了利用Python中的輸入和輸出功能進(jìn)行讀取和寫入的教程,本文來自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下2015-04-04

