继续进步一点点,温故而知新
一、requests 介绍
1、requests 的官方文档:https://docs.python-requests.org/en/latest/
2、安装requests:pip install requests
二、requests请求
1、请求方法:post,get,put,delete等
以下为请求示例:
(1)加密手机号,并且获取已加密的手机号的值
import requests
import pytest
class TestencryptMobile:
def test_encryptMobile(self):
json_data={
"mobile": "180****0704"
}
url="{{url}}/memberActivity/tassHsmApiService/encryptMobile"
#使用requests.post方法请求
r=requests.post(url=url,json=json_data)
result =r.json()
print(result)
#设置断言,判断调用是否成功
assert r.json()['success'] == True
#获取加密手机号
return result.get("obj")
(2)查询活动数据,获取活动单独ID
def test_search_post(self):
url="{url}/high_disp_retrieve_two_month_new/high_disp_retrieve_two_month_new/_search"
json_data={
"query": {
"term": {
"mobile_secret": {
#调用上一接口返回的加密手机号
"value": TestencryptMobile.test_encryptMobile(self)
}
}
}
}
#headers = {'Content-Type': 'application/json'}
result=requests.get(url=url,json=json_data)
print(result.json())
assert result.json()['timed_out'] == False
assert result.status_code == 200
# print(result.json()['hits']['hits'])
inner_hits = result.json()['hits']['hits']
#获取结果中的唯一ID
for hit_data in inner_hits:
hit_data.get("_id")
print(hit_data.get("_id"))
return hit_data.get("_id")
(3)删除测试数据
def test_delete_target(self):
if self.test_search_post() == None:
return
url="{url}/high_disp_retrieve_two_month_new/high_disp_retrieve_two_month_new"
#本次删除无需data数据,只需要获取上个接口的id拼接起来,执行即可删除
delete_target=requests.delete(url=url + '/' + self.test_search_post())
delete_result=delete_target.json()
#判断返回结果,确认删除
assert delete_result['result'] == 'deleted'