增加关联参数多值支持

This commit is contained in:
Leozhanggg 2021-09-09 19:01:24 +08:00
parent c8e4a7a8c0
commit 36c5d5c814
5 changed files with 96 additions and 42 deletions

View File

@ -1,5 +1,12 @@
from config import read_yaml_data, DB_CONFIG, PROJECT_NAME
DC = read_yaml_data(DB_CONFIG)[PROJECT_NAME]
if 'solr_info' in DC:
from .querySolr import search_solr from .querySolr import search_solr
if 'hbase_info' in DC:
from .queryHBase import PhoenixServer from .queryHBase import PhoenixServer
if 'es_info' in DC:
from .queryES import elastic_search from .queryES import elastic_search
if 'mysql_info' in DC:
from .queryMysql import MysqlServer from .queryMysql import MysqlServer
if 'redis_info' in DC:
from .queryRedis import exec_redis from .queryRedis import exec_redis

View File

@ -22,17 +22,17 @@ def post(headers, address, mime_type, timeout=10, data=None, files=None, cookies
:param mime_type: 请求参数格式form_data,raw :param mime_type: 请求参数格式form_data,raw
:param timeout: 超时时间 :param timeout: 超时时间
:param data: 请求参数 :param data: 请求参数
:param files: 文件路径 :param files: 上传文件请求参数dict
:param cookies: :param cookies:
:return: :return:
""" """
# 判断请求参数类型 # 判断请求参数类型
if 'form_data' in mime_type: if 'form_data' in mime_type:
for i in files: for key in files:
value = files[i] value = files[key]
# 判定参数值是否为文件,如果是则替换为二进制值
if '/' in value: if '/' in value:
file_parm = i files[key] = (os.path.basename(value), open(value, 'rb'))
files[file_parm] = (os.path.basename(value), open(value, 'rb'))
enc = MultipartEncoder( enc = MultipartEncoder(
fields=files, fields=files,
boundary='--------------' + str(random.randint(1e28, 1e29-1)) boundary='--------------' + str(random.randint(1e28, 1e29-1))

View File

@ -6,6 +6,7 @@
import logging import logging
import time import time
import json import json
import os
from json import JSONDecodeError from json import JSONDecodeError
from config import PAGE_DIR, PROJECT_NAME, API_CONFIG from config import PAGE_DIR, PROJECT_NAME, API_CONFIG
from comm.unit import apiSend, readRelevance, replaceRelevance from comm.unit import apiSend, readRelevance, replaceRelevance
@ -60,8 +61,9 @@ def init_premise(test_info, case_data, case_path):
if pre_case_path: if pre_case_path:
# 获取前置接口用例 # 获取前置接口用例
logging.info("获取前置接口测试用例:{}".format(pre_case_path)) logging.info("获取前置接口测试用例:{}".format(pre_case_path))
pre_case_path = PAGE_DIR + pre_case_path pre_case_yaml = PAGE_DIR + pre_case_path
pre_case_dict = readYaml.read_yaml_data(pre_case_path) pre_case_path = os.path.dirname(pre_case_yaml)
pre_case_dict = readYaml.read_yaml_data(pre_case_yaml)
pre_test_info = pre_case_dict['test_info'] pre_test_info = pre_case_dict['test_info']
pre_case_data = pre_case_dict['test_case'][0] pre_case_data = pre_case_dict['test_case'][0]
# 判断前置接口是否也存在前置接口 # 判断前置接口是否也存在前置接口

View File

@ -6,7 +6,8 @@
import logging import logging
import re import re
__relevance = "" # __relevance = ""
__relevance = []
def get_value(data, value): def get_value(data, value):
@ -18,16 +19,23 @@ def get_value(data, value):
""" """
global __relevance global __relevance
if isinstance(data, dict): if isinstance(data, dict):
if value in data: # if value in data:
__relevance = data[value] # __relevance = data[value]
else: # else:
# for key in data:
# __relevance = get_value(data[key], value)
for key in data: for key in data:
__relevance = get_value(data[key], value) if isinstance(data[key], dict) or isinstance(data[key], list):
get_value(data[key], value)
else:
if key == value:
__relevance.append(data[key])
elif isinstance(data, list): elif isinstance(data, list):
for key in data: for key in data:
if isinstance(key, dict): if isinstance(key, dict):
__relevance = get_value(key, value) # __relevance = get_value(key, value)
break # break
get_value(key, value)
return __relevance return __relevance
@ -39,10 +47,20 @@ def get_relevance(data, relevance_list, relevance=None):
:param relevance: :param relevance:
:return: :return:
""" """
global __relevance
# 获取关联键列表 # 获取关联键列表
relevance_list = re.findall(r"\${(.*?)}", str(relevance_list)) relevance_list = re.findall(r"\${(.*?)}", str(relevance_list))
# 去除参数[n]标识
for index, value in enumerate(relevance_list):
mark = re.findall(r"\[\-?[0-9]*\]", value)
if mark:
relevance_list[index] = value.strip(mark[0])
# 去除重复参数
relevance_list = list(set(relevance_list)) relevance_list = list(set(relevance_list))
logging.debug("获取关联键列表:\n%s" % relevance_list) logging.debug("获取关联键列表:\n%s" % relevance_list)
# 判断关联键和源数据是否有值 # 判断关联键和源数据是否有值
if (not data) or (not relevance_list): if (not data) or (not relevance_list):
return relevance return relevance
@ -50,26 +68,29 @@ def get_relevance(data, relevance_list, relevance=None):
# 判断是否存在其他关联键对象 # 判断是否存在其他关联键对象
if not relevance: if not relevance:
relevance = dict() relevance = dict()
# 遍历关联键 # 遍历关联键
for each in relevance_list: for each in relevance_list:
if each in relevance: # 只考虑一个关联键一个值
pass # if each in relevance:
# # 考虑到一个关联键,多个值 # pass
# if isinstance(relevance[each], list):
# a = relevance[each]
# a.append(relevance_value)
# relevance[each] = a
# else: # else:
# a = relevance[each] # # 从结果中提取关联键的值
# b = list() # relevance[each] = get_value(data, each)
# b.append(a)
# b.append(relevance_value) # 考虑到一个关联键多个值
# relevance[each] = b relevance_value = get_value(data, each)
if relevance_value:
if each in relevance:
tmp = relevance[each]
if isinstance(tmp, list):
tmp += relevance_value
relevance[each] = tmp
else: else:
# 从结果中提取关联键的值 tmp2 = relevance_value.insert(0, tmp)
relevance[each] = get_value(data, each) relevance[each] = tmp2
else:
relevance[each] = relevance_value
__relevance = []
logging.debug("提取关联键对象:\n%s" % relevance) logging.debug("提取关联键对象:\n%s" % relevance)
return relevance return relevance

View File

@ -44,7 +44,15 @@ def replace_relevance(param, relevance=None):
else: else:
for each in result: for each in result:
try: try:
# 关联参数多值替换 # 关联值只考虑一个值
# value = relevance[each]
# pattern = re.compile(r'\${' + each + '}')
# try:
# param = re.sub(pattern, value, param)
# except TypeError:
# param = value
# 关联参数多值时一一对应替换
# relevance_index = 0 # relevance_index = 0
# if isinstance(relevance[each], list): # if isinstance(relevance[each], list):
# try: # try:
@ -54,8 +62,23 @@ def replace_relevance(param, relevance=None):
# relevance_index = 0 # relevance_index = 0
# param = re.sub(pattern, relevance[each][relevance_index], param, count=1) # param = re.sub(pattern, relevance[each][relevance_index], param, count=1)
# relevance_index += 1 # relevance_index += 1
# 关联参数多值时指定索引值替换
mark = re.findall(r"\[\-?[0-9]*\]", each)
# 判断关联参数是否指定索引值var[n]
if mark:
var = each.strip(mark[0])
n = int(mark[0].strip('[').strip(']'))
value = relevance[var][n]
each = each.replace('[', '\[').replace(']', '\]')
else:
if isinstance(relevance[each], list):
value = relevance[each][0]
else:
value = relevance[each] value = relevance[each]
pattern = re.compile(r'\${' + each + '}')
# 生成正在表达式并替换关联参数
pattern = re.compile('\${' + each + '}')
try: try:
param = re.sub(pattern, value, param) param = re.sub(pattern, value, param)
except TypeError: except TypeError:
@ -193,3 +216,4 @@ if __name__ == '__main__':
print('生成一个18岁伪身份证', replace("$GenNoid(y-18)")) print('生成一个18岁伪身份证', replace("$GenNoid(y-18)"))
print('生成下个月今天的日期:', replace("$GenDate(m+1)")) print('生成下个月今天的日期:', replace("$GenDate(m+1)"))
print('生成昨天此时的时间:', replace("$GenDatetime(d-1)")) print('生成昨天此时的时间:', replace("$GenDatetime(d-1)"))
print('通过索引指定关联值:', replace('${name[-1]}', {'name': ['test1', 'test2']}))