excel用例转化字典-模块封装

数据模块,可以将excel转化成字典

#coding:utf-8
import requests,jsonpath #需要pip install jsonpath 安装,如果是mac需要pip3
import ast #系统的第三方库,不需要下载
from config import setting #博主自己定义的库
from tools import localconfig_Utils #博主自己封装的读取config文件的库
class RequestsUtils():
def init(self):
self.hosts = localconfig_Utils.conf.URL#通过读取config模块获取url
self.headers = {“Content-Type”:“application/json;charset=utf-8”}
self.session = requests.session() #声明一个对象,改对象保持登录
self.temp_variables = {}#声明一个空字典

def __get(self,get_info):
    url = self.hosts + get_info["请求地址"] #字典的键值对取值
    response = self.session.get(
        url= url,
        params= ast.literal_eval(get_info["请求参数(get)"]))# eval()函数,将字符串装换为指定格式,在这里是装换为字典,
    response.encoding = response.apparent_encoding # 将获取到的页面编码格式赋值给对象编码格式
    if get_info['取值方式'] == 'json取值':
        value = jsonpath.jsonpath(response.json(),get_info['取值代码'])[0]#使用jsonpath模块获取自己需要的数值
        self.temp_variables[get_info['传值变量']] = value
    result = {
        'code': 0,
        'response_reason':response.reason,
        'response_code':response.status_code,
        'response_headers':response.headers,
        'response_body':response.json()
    }
    return result

def __post(self,post_info):
    url = self.hosts + post_info["请求地址"]
    response = self.session.get(
        url= url,
        headers = self.headers,
        params = {"access_token":" "},
        data = ast.literal_eval(post_info["提交数据(post)"])
    )
    # eval()函数,将字符串装换为指定格式,在这里是装换为字典,但是比较危险,所以使用安全的ast.literal_eval
    response.encoding = response.apparent_encoding # 将获取到的页面编码格式赋值给对象编码格式
    if post_info['取值方式'] == 'json取值':
        value = jsonpath.jsonpath(response.json(),post_info['取值代码'])[0]
        self.temp_variables[post_info['传值变量']] = value
    result = {
        'code': 0,
        'response_reason':response.reason,
        'response_code':response.status_code,
        'response_headers':response.headers,
        'response_body':response.json()
    }
    return result

def request(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':3,'result':'请求方式不支持'}
    return result

def step_request(self,step_infos):
"""
测试用例可能存在多个步骤,故需要使用for循环遍历
"""
    for step_info in step_infos:
        print(step_info)
        result = self.request(step_info)
        if result['code'] != 0:
           break
    return result

if name == ‘main’:
testdata = Testdatautils(setting.files_path + “\” + “test_case.xlsx”) # 读取excel的数据
a = testdata.get_testcase_data_list() #将数据转化为字典
print(a) #输出整个excel表格的内容转化为字典以后的值
print(“#”*20)#分割线
get_infos = a[2][‘case_info’] #获取自己需要的数据
print(get_infos)# 输出获取到的数据
print(“#” * 20) #分割线
print(RequestsUtils().step_request(get_infos)) #执行接口测试用例并且输出执行结果到控制台

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值