關於python實現requests接口測試的問題
requests接口測試的介紹
requests是一個很實用的Python HTTP客戶端庫,編寫爬蟲和測試服務器響應數據時經常會用到,Requests是Python語言的第三方的庫,專門用於發送HTTP請求
requests接口測試的使用前提
pip install requests
1.requests中的get請求
1 GET無參請求
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請求
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) #這是查看狀態碼的 a=Classrequset() a.XWTTMethodpost()
3.Requests響應
r.status_code 響應狀態碼 r.heards 響應頭 r.cookies 響應cookies r.text 響應文本 r. encoding 當前編碼 r. content 以字節形式(二進制)返回
最常用的是根據響應狀態碼判斷接口是否連通,經常用於做接口中斷言判斷
4.Request擴充
1:添加等待時間 requests.get(url,timeout=1) #超過等待時間則報錯 2:添加請求頭信息 requests.get(url,headers=headers) #設置請求頭 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實現requests+pytest+allure的操作
1 流程如下
讀取文件中的數據 requests拿到數據請求接口返回狀態碼通過斷言驗證返回狀態碼和200對比生成allure的測試報告
6.讀取csv文件流程
1 存儲數據(csv)
2 讀取數據(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請求接口返回狀態碼
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斷言設置並結合allure生成測試報告
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 測試報告展示
7.讀取excle文件流程
1 存儲數據(xlsx)
2 讀取數據(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 = []#把所有行的數據放到列表中 for i in range(2,sheet.max_row+1): sub_data = {}#把每行的數據放到字典中 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)#拼接每行單元格的數據 return test_data t = UseExcel() f = t.get_TestExcel() print(f)
3.request請求接口返回狀態碼
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斷言設置並結合allure生成測試報告
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 測試報告展示
到此這篇關於python實現requests接口測試的文章就介紹到這瞭,更多相關python requests接口測試內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Python中requests做接口測試的方法
- python單元測試之pytest的使用
- Python爬蟲學習之requests的使用教程
- Pytest接口自動化測試框架搭建模板
- python編寫接口測試文檔(以豆瓣搜索為例)