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接口测试内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!