• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
msg_data
帮助2

数据模块,可以将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)) #执行接口测试用例并且输出执行结果到控制台

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhieakhf
系列文章
更多 icon
同类精品
更多 icon
继续加载