python-request接口自动化测试
一.概念
requests是一个很实用的Python HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到,Requests是Python语言的第三方的库,专门用于发送HTTP请求
二.前提
要在pycharm的黑窗口中下载requests
pip install requests
三.get请求
// get请求案例:
get第一种方式.url和参数都放在一起
url="http://apis.juhe.cn/mobile/get?phone=17319306584&dtype=&key=02e47c6e71e9445573d1a533748152aa"
response=requests.get(url)
print(response.text)
get第二种方式,url和参数分开
url="http://apis.juhe.cn/mobile/get"
phontparams={"phone":"17319306584","key":"02e47c6e71e9445573d1a533748152aa"}
response=requests.get(url,params=phontparams)
print(response.text)
四.post请求
// post请求案例:
url = "http://web.juhe.cn:8080/constellation/getAll"
phontparams = {"consName": "金牛座","type":"today", "key": "2665ea77f94444275a571b04483e0f31"}
response = requests.post(url, data=phontparams)
print(response.text)
五.requests的响应内容
// requests的响应内容
r.status_code 响应状态码
r.heards 响应头
r.cookies 响应cookies
r.text 响应文本
r. encoding 当前编码
r. content 以字节形式(二进制)返回
六.requests的扩充
1:添加等待时间
requests.get(url,timeout=1) #超过等待时间则报错
2:添加请求头信息
requests.get(url,headers=headers) #设置请求头
3:添加文件
requests.post(url, files=files) #添加文件
七.requests+pytest+allure
链接: python-requests+pytest+allure实现读取csv文件及excel文件
( ̄▽ ̄)~*------ ٩(๑❛ᴗ❛๑)۶谢谢阅读!!!!!!!!!!!!!!!