requests接口测试
一·介绍
Requests是一个很实用的Python HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到,Requests是Python语言的第三方的库,专门用于发送HTTP请求
二、前提
pip install requests
三、get的请求
GET无参请求
r=requests.get('http://www.baidu.com')
GET传参
payload={'key1':'value1','key2':'value2','key3':None}r=requests.get('http://www.baidu.com ',params=payload)
四、post请求
类似python中的表单提交
payload={'key1':'value1','key2':'value2'}r=requests.post("http://httpbin.org/post",data=payload)
五、Requests响应
r.status_code 响应状态码
r.heards 响应头
r.cookies 响应cookies
r.text 响应文本
r. encoding 当前编码
r. content 以字节形式(二进制)返回
Request扩充
1.添加等待时间requests.get(url,timeout=1)#超过等待时间则报错2.添加请求头信息requests.get(url,headers=headers)#设置请求头3.添加文件requests.post(url,files=files)#添加文件
七、requests+pytest+allure
流程如下
读取文件中的数据requests拿到数据请求接口返回状态码通过断言验证返回状态码和200对比生成allure的测试报告
模块总览
dataDemo(存放数据)>> readDemo(读取数据)
useRequests(发送请求)>>testDemo(生成报告