0
点赞
收藏
分享

微信扫一扫

day16-requests接口测试

西风白羽 2022-01-04 阅读 31

1: 介绍

requests 是一个很实用的 Python HTTP 客户端库,编写爬虫和测试服务器响应数据时经常会用到,

Requests Python 语言的第三方的库,专门用于发送 HTTP 请求

2: 前提

pip install requests

 3:get的请求

3.1GET无参请求

r = requests.get('http://www.baidu.com')

3.2 GET 传参

payload = {'key1': 'value1', 'key2': 'value2', 'key3': None}

r = requests.get('http://www.baidu.com ', params=payload)

案例:测试聚合数据

 4:post请求

类似 python 中的表单提交

payload = {'key1': 'value1', 'key2': 'value2'}

r = requests.post("http://httpbin.org/post", data=payload)

 5:Requests响应

r.status_code 响应状态码

r.heards 响应头

r.cookies 响应 cookies

r.text 响应文本

r. encoding 当前编码

r. content 以字节形式(二进制)返回

最常用的是根据响应状态码判断接口是否连通,经常用于做接口中断言判断

6 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)

7.2 :模块总览

dataDemo( 存放数据 )>> readDemo( 读取数据 )

useRequests( 发送请求 )>>testDemo( 生成报告 )

 

 

 7.4:读取excle文件流程

 

 

 

作业:

再聚合报告中找出两个接口 (get/post) ,进行 csv/excel 文件读取,并生成测试报告

 

 

举报

相关推荐

0 条评论