字符串转化成字典:
convert_to_dict.py:
import json
str1 = '{"grant_type":"client_credential","appid":"wx55614004f367f8ca","secret":"65515b46dd758dfdb09420bb7db2c67f"}'
print(type(str1))
jsondata = json.loads(str1) # 把字符串转化成字典
print(jsondata)
print( type(jsondata) )
dict1 = eval(str1)
print(dict1)
print(type(dict1))
str2 = '3+3'
c = eval(str2)
print(c)
eval函数具体参考:https://www.cnblogs.com/dream66/p/13264725.html
ast模块的eval函数也可以把字符串转化成字典。
import json
import ast
str1 = '{"grant_type":"client_credential","appid":"wx55614004f367f8ca","secret":"65515b46dd758dfdb09420bb7db2c67f"}'
print(type(str1))
# jsondata = json.loads(str1)
# print( type(jsondata) )
# dict1 = eval(str1)
dict1 = ast.literal_eval(str1)
print(dict1)
print(type(dict1))
# str2 = '3+3'
# c = ast.literal_eval(str2)
# print(c)
封装get方法
import jsonpath
import requests
import json
from utils.config_utils import local_config
class RequestsUtils:
def __init__(self):
self.hosts = local_config.HOSTS
self.session = requests.session()
def get(self, requests_info):
url = self.hosts + requests_info['请求地址']# 取出下面字典中的请求地址。
# {'测试用例编号': 'api_case_01', '测试用例名称': '获取access_token接口测试', '用例执行': '是', '用例步骤': 'step_01', '接口名称': '获取access_token接口', '请求方式': 'get', '请求头部信息': '', '请求地址': '/cgi-bin/token', '请求参数(get)': '{"grant_type":"client_credential","appid":"wx55614004f367f8ca","secret":"65515b46dd758dfdb09420bb7db2c67f"}', '请求参数(post)': '', '取值方式': '无', '取值代码': '', '取值变量': '', '断言类型': 'body_regexp', '期望结果': '"access_token":"(.+?)"'}
response = self.session.get(url = url,
params = json.loads(requests_info['请求参数(get)']),
headers = requests_info['请求头部信息']
) #参数名 =参数值
result = {
'code':0,
'response_code':response.status_code,
'response_reason':response.reason,
'response_headers':response.headers,
'response_body':response.text
}
return result
if __name__=='__main__': # get
req_dict = {'测试用例编号': 'api_case_01', '测试用例名称': '获取access_token接口测试', '用例执行': '是', '用例步骤': 'step_01', '接口名称': '获取access_token接口', '请求方式': 'get', '请求头部信息': '', '请求地址': '/cgi-bin/token', '请求参数(get)': '{"grant_type":"client_credential","appid":"wx55614004f367f8ca","secret":"65515b46dd758dfdb09420bb7db2c67f"}', '请求参数(post)': '', '取值方式': '无', '取值代码': '', '取值变量': '', '断言类型': 'body_regexp', '期望结果': '"access_token":"(.+?)"'}
requestsUtils = RequestsUtils()
v = requestsUtils.get(req_dict)
print(v)
运行结果:
运行结果如下:
{'code': 0, 'response_code': 200, 'response_reason': 'OK', 'response_headers': {'Connection': 'keep-alive', 'Content-Type': 'application/json; encoding=utf-8', 'Date': 'Wed, 09 Dec 2020 07:32:26 GMT', 'Content-Length': '194'}, 'response_body': '{"access_token":"40_-cjwO740UNzOWgHVGaxMjPCRe425jDOAgTl2Y5_eZk84-JVVA3G457hf5jRu8CGQ7WvwkIt5IqnPwV_pUg6TRwEAquY-UQhirdKcaqKGeiVzhVYQc7iboYht6TfQqMF26jMn3gJPnlc04HNdPWAjAGAELP","expires_in":7200}'}
这里需要注意一个坑:就是config.utils里面如果少写了@property
那么运行会报如下错误:
config_utils.py
import os
import configparser
current_path =os.path.dirname(__file__)#获取config当前文件路径
config_file_path = os.path.join(current_path,'..','conf','localconfig.ini')#获取配置文件的路径
class ConfigUtils: #类封装、驼峰式命名法
def __init__(self,cfg_path=config_file_path):
self.cfg =configparser.ConfigParser()#创建一个配置文件对象
self.cfg.read(cfg_path) #创建好后去读取cfg_path
@property #这个如果不写requests.utils就获取不到,
def HOSTS(self):
hosts_value = self.cfg.get('default','hosts') #获取节和key
return hosts_value #@property的属性名这里必须有下划线,不然会报错
#
local_config = ConfigUtils() #创建对象,测试代码
if __name__=='__main__':
print(local_config.HOSTS)
封装post方法():
import requests
import json
from utils.config_utils import local_config
class RequestsUtils:
def __init__(self):
self.hosts = local_config.HOSTS
self.session = requests.session()
def get(self, requests_info):
url = self.hosts + requests_info['请求地址']# 取出下面字典中的请求地址。
# {'测试用例编号': 'api_case_01', '测试用例名称': '获取access_token接口测试', '用例执行': '是', '用例步骤': 'step_01', '接口名称': '获取access_token接口', '请求方式': 'get', '请求头部信息': '', '请求地址': '/cgi-bin/token', '请求参数(get)': '{"grant_type":"client_credential","appid":"wx55614004f367f8ca","secret":"65515b46dd758dfdb09420bb7db2c67f"}', '请求参数(post)': '', '取值方式': '无', '取值代码': '', '取值变量': '', '断言类型': 'body_regexp', '期望结果': '"access_token":"(.+?)"'}
response = self.session.get(url = url,
params = json.loads(requests_info['请求参数(get)']),
headers = requests_info['请求头部信息']
) #参数名 =参数值
result = {
'code':0,
'response_code':response.status_code,
'response_reason':response.reason,
'response_headers':response.headers,
'response_body':response.text
}
return result
def post(self, requests_info):
url = self.hosts + requests_info['请求地址']# 取出下面字典中的请求地址。
# {'测试用例编号': 'api_case_01', '测试用例名称': '获取access_token接口测试', '用例执行': '是', '用例步骤': 'step_01', '接口名称': '获取access_token接口', '请求方式': 'get', '请求头部信息': '', '请求地址': '/cgi-bin/token', '请求参数(get)': '{"grant_type":"client_credential","appid":"wx55614004f367f8ca","secret":"65515b46dd758dfdb09420bb7db2c67f"}', '请求参数(post)': '', '取值方式': '无', '取值代码': '', '取值变量': '', '断言类型': 'body_regexp', '期望结果': '"access_token":"(.+?)"'}
response = self.session.post(url = url,
#params = json.loads(requests_info['请求参数(get)']),
params= json.loads(requests_info['请求参数(post)'])
,
json = json.loads(requests_info['请求参数(post)'])
) #参数名 =参数值
response.encoding = response.apparent_encoding
result = {
'code':0,
'response_code':response.status_code,
'response_reason':response.reason,
'response_headers':response.headers,
'response_body':response.text
}
return result
def requests(self,step_info):
request_type =step_info['请求方式']
if request_type=="get":
result=self.get(step_info)
elif request_type == "post":
result =self.post(step_info)
else:
result ={'code':1,'result':'请求方式不支持'}
return result
if __name__=='__main__': # 测试封装的get()方法
req_dict = {'测试用例编号': 'api_case_01', '测试用例名称': '获取access_token接口测试', '用例执行': '是', '用例步骤': 'step_01', '接口名称': '获取access_token接口', '请求方式': 'get', '请求头部信息': '', '请求地址': '/cgi-bin/token', '请求参数(get)': '{"grant_type":"client_credential","appid":"wx55614004f367f8ca","secret":"65515b46dd758dfdb09420bb7db2c67f"}', '请求参数(post)': '', '取值方式': '无', '取值代码': '', '取值变量': '', '断言类型': 'body_regexp', '期望结果': '"access_token":"(.+?)"'}
requestsUtils = RequestsUtils()
v = requestsUtils.get(req_dict)
print(v)
if __name__ == '__main__':# 测试封装的post()方法
req_post_dict = {'测试用例编号': 'api_case_03', '测试用例名称': '删除标签接口测试', '用例执行': '是', '用例步骤': 'step_03', '接口名称': '删除标签接口', '请求方式': 'post', '请求头部信息': '', '请求地址': '/cgi-bin/tags/delete', '请求参数(get)': '{"access_token":"39_ZlzNDPma7qLWpLJ4K0ir_cSahJ_fg9aevBpGvqRp9VNjqRE6hSkBOSUFla-mFjSGKyF-YFx28sM4Ch1rJISPGVSTahZ8l_xQ9M7CnAFoqUfibusAdeOI4lHEIzB6zhXJQHN5b9as9zhcGtSbBYKeAGAEBN"}', '请求参数(post)': '{ "tag":{ "id" : 456 } }'}
requestsUtils = RequestsUtils()
v = requestsUtils.post( req_post_dict)
print( v )