Support multiple parameters

This commit is contained in:
Leozhanggg 2021-08-31 12:07:03 +08:00
parent c8e4a7a8c0
commit 851534c35f
3 changed files with 80 additions and 32 deletions

View File

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

View File

@ -6,7 +6,8 @@
import logging
import re
__relevance = ""
# __relevance = ""
__relevance = []
def get_value(data, value):
@ -18,16 +19,23 @@ def get_value(data, value):
"""
global __relevance
if isinstance(data, dict):
if value in data:
__relevance = data[value]
else:
for key in data:
__relevance = get_value(data[key], value)
# if value in data:
# __relevance = data[value]
# else:
# for key in data:
# __relevance = get_value(data[key], value)
for key in data:
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):
for key in data:
if isinstance(key, dict):
__relevance = get_value(key, value)
break
# __relevance = get_value(key, value)
# break
get_value(key, value)
return __relevance
@ -39,10 +47,20 @@ def get_relevance(data, relevance_list, relevance=None):
:param relevance:
:return:
"""
global __relevance
# 获取关联键列表
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))
logging.debug("获取关联键列表:\n%s" % relevance_list)
# 判断关联键和源数据是否有值
if (not data) or (not relevance_list):
return relevance
@ -50,26 +68,29 @@ def get_relevance(data, relevance_list, relevance=None):
# 判断是否存在其他关联键对象
if not relevance:
relevance = dict()
# 遍历关联键
for each in relevance_list:
if each in relevance:
pass
# # 考虑到一个关联键,多个值
# if isinstance(relevance[each], list):
# a = relevance[each]
# a.append(relevance_value)
# relevance[each] = a
# else:
# a = relevance[each]
# b = list()
# b.append(a)
# b.append(relevance_value)
# relevance[each] = b
else:
# 从结果中提取关联键的值
relevance[each] = get_value(data, each)
# 只考虑一个关联键一个值
# if each in relevance:
# pass
# else:
# # 从结果中提取关联键的值
# relevance[each] = get_value(data, each)
# 考虑到一个关联键多个值
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:
tmp2 = relevance_value.insert(0, tmp)
relevance[each] = tmp2
else:
relevance[each] = relevance_value
__relevance = []
logging.debug("提取关联键对象:\n%s" % relevance)
return relevance

View File

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