pytest+allure +requests接口自动化

request是一个很实用的python HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到,Requests是python语言的第三方的库,专门用于发送HTTP请求
GET请求:

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

传参:

aa = {‘key1’: ‘value1’, ‘key2’: ‘value2’, ‘key3’: None}

r = requests.get(‘http://www.baidu.com’, params=aa)

POST请求:

类似于python中的表单提交:

aa = {‘key1’: ‘value1’, ‘key2’: ‘value2’}

r = requests.post(“http://v.juhe.cn/toutiao/index”, data=aa)

传递文件(了解)

url = “http://v.juhe.cn/toutiao/index”

files = {‘file’: open(‘report.xls’, ‘rb’)}

r = requests.post(url, files=files)

Requests响应

r.status_code 响应状态码
pytest+allure +requests接口自动化
r.heards 响应头:
pytest+allure +requests接口自动化
r.cookies 响应cookies:
pytest+allure +requests接口自动化
r.text 响应文本:
pytest+allure +requests接口自动化
r.encoding 当前编码:
pytest+allure +requests接口自动化
r.content 以字节形式(二进制)返回:
pytest+allure +requests接口自动化
发送无参数的get请求:
pytest+allure +requests接口自动化
发送有参数的get请求:
pytest+allure +requests接口自动化
Request扩充:
1:添加等待时间

requests.get(url, timeout=1) # 超过等待时间则报错

2:添加请求头信息

requests.get(url, headers=headers) # 设置请求头

3:添加文件

requests.post(url, files=files) # 添加文件

案例:

新建一个csv文件,把路径条件key都写进去:
pytest+allure +requests接口自动化
读取csv文件内的数据:
pytest+allure +requests接口自动化
获取接口状态码:
pytest+allure +requests接口自动化
再测试接口状态码是否正确,接口是否有问题,生成allure测试报告:
pytest+allure +requests接口自动化
网页测试报告展示:
pytest+allure +requests接口自动化