def execute(self, variables): # 如果没有设置前置或后置则无需执行 if self.expr_lines: for expr_line in self.expr_lines: if "=" not in expr_line: print YamlHelper.var_expr(variables, expr_line.strip()) else: self.add_var(variables, expr_line)
def data_combine(self): # 将Yaml提供的字典类型转换成列表类型 # yamlLst格式如: ["name", ["value,", "value2", "value3"], ["password", ["value1", "value2", "value3"]]] yamlLst = [] for key in self.body: if self.body[key].has_key(YamlTag.Values): # 如果有values标签,则使用该标签 values = self.body[key][YamlTag.Values] else: # 没有values标签,则使用该类型下的默认值 value_type = self.body[key][YamlTag.Type] values = YamlHelper.yaml_default(value_type) yamlLst.append([key, values]) lst = [self.lst_dict_lst(item) for item in yamlLst] dc = lst[0] if len(lst) == 1: return dc else: for i in range(1, len(lst)): dc = self.assemble_dict_lst(dc, lst[i]) return dc
def save_check_response(self, data_item, response): # 先判断文件夹和文件是否存在 if not os.path.exists(self.response_file): if not os.path.exists(os.path.split(self.response_file)[0]): os.makedirs(os.path.split(self.response_file)[0]) res_file = open(self.response_file, "w") res_file.close() # 读写文件 # 如果已经存在该配置了,则忽略 old_yml_dict = yaml.load(open(self.response_file, "r")) response_dict = yaml.load(response.text) res_file = open(self.response_file, "a") # 获取key值,由于dict具有无序性,再创建key时需要以key做一次排序 key_str = "key_" # iteritems() 返回一个字典键值对的元组集合 sorted_data_item = sorted(data_item.iteritems(), key=lambda k: k[0], reverse=False) for item in sorted_data_item: key_str = key_str + str(item[1]) + "_" key_str = key_str[0 : len(key_str) - 1] result_dict = {} # 先要判断key是否存在,如果存在则不做任何处理,不存在则插入 if (old_yml_dict is None) or (not old_yml_dict.has_key(key_str)): print "插入新的key: {0}".format(key_str) result_dict[key_str] = data_item result_dict[key_str][YamlTag.Response] = response_dict yaml.dump(result_dict, res_file, default_flow_style=False, indent=4) res_file.write("\n") else: print "已经存在key: {0}".format(key_str) print "已存在的key的内容为: {0}".format(old_yml_dict[key_str][YamlTag.Response]) # 存在该Key,就需要去比较该key的值和response的值 # 比较 old_yml_dict[YamlTag.Response] 与 response_dict 的值,以old_yml_dict为准 flag = YamlHelper.left_cmp_dict(old_yml_dict[key_str][YamlTag.Response], response_dict) res_file.close() return flag
def add_var(self, variables, variable_line): variable_name = variable_line.split("=")[0].strip() variables[variable_name] = YamlHelper.var_expr(variables, variable_line.split("=")[1].strip())
def __init__(self, yml_file_path): api = self.api_content(yml_file_path) # if api.has_key(YamlTag.Body) and api.has_key(YamlTag.Procedure): # print "不允许同时配置Body和Procedure标签" # return None # Yaml 文件名及输出结果的路径 file_name = os.path.split(yml_file_path)[1] root_dir = YamlHelper.same_prefix(os.path.abspath(yml_file_path), os.getcwd()) self.interface_name = file_name[0 : file_name.index(".")] self.response_file = "{0}/response/{1}.yml".format(root_dir, self.interface_name) # Yaml配置文件定义的全局变量,对应globa标签下的变量 self.variables = YamlVariables(api[YamlTag.Global]) # Yaml请求实例 self.request = YamlHttpRequest( YamlHelper.var_expr(self.variables.variables, YamlHelper.yaml_tag_value(api, YamlTag.Url)), YamlHelper.dict_var_expr(self.variables.variables, YamlHelper.yaml_tag_value(api, YamlTag.Auth)), YamlHelper.dict_var_expr(self.variables.variables, YamlHelper.yaml_tag_value(api, YamlTag.Header)), YamlHelper.var_expr(self.variables.variables, YamlHelper.yaml_tag_value(api, YamlTag.Method)), YamlHelper.var_expr(self.variables.variables, YamlHelper.yaml_tag_value(api, YamlTag.Action)), ) print "全局变量列表: " for key in self.variables.variables: print "key = {0} \t\t value = {1}".format(key, self.variables.variables[key]) # 前置操作 self.precondition = YamlStep(YamlHelper.yaml_tag_value(api, YamlTag.Precondition)) # 请求的主体,用来进行请求参数的组合 self.body = YamlHelper.yaml_tag_value(api, YamlTag.Body) # 执行过程 -- 为避免与body重复,可考虑不要该标签 # self.procedure = YamlStep() # 后置操作 self.postcondition = YamlStep(YamlHelper.yaml_tag_value(api, YamlTag.Postcondition)) # 参数数据组合 self.data_combination = self.data_combine()