def set_global(self, data): yaml_data = data_read.get_yaml() if yaml_data is None: yaml_data = data else: yaml_data.update(data) data_read.set_yaml(yaml_data)
def handle_url(self, data): if "{" in data: param = re.findall('{(.*?)}', data) # 正则匹配出参数 for i in param: yamldata = data_read.get_yaml() u = data.replace("{"+i+"}", yamldata[i]) data = u return data
def handle_result(self, data): if "{" in data: result = re.findall('{(.*?)}', data) for i in result: yamldata = data_read.get_yaml() h = data.replace("{" + i + "}", '"' + yamldata[i] + '"') data = h return demjson.decode(data)
def handle_param(self, data): if "{" in data: param = re.findall('{(.*?)}', data) # 正则匹配出参数,结果为list类型 for i in param: # 将data中各个参数替换成yaml中对应的值 yamldata = data_read.get_yaml() b = data.replace("{"+i+"}", '"'+yamldata[i]+'"') data = b # print("handle_param_data: %s && type(data):%s" % (data, type(data))) return demjson.decode(data)
def test_main(self, case_id, header, case_name, url, method, path, param, teardown, test_assert, code, result): try: # 输出日志到日志文件 case_name = 'test_' + case_id + '_' + case_name self.log.build_start_line(case_name) # 处理path、url self.path = self._handle.handle_url(path) self.url = "%s%s" % (url, self.path) # 处理code self.code = (code) # 处理result if "{" in result: self.result = self._handle.handle_result(result) else: self.result = result # 判断param中是否有参数化 if "{" in param: self.param = self._handle.handle_param(param) else: self.param = param # 判断是否有前置header需求 if header != "": self.header = self._handle.handle_header(header) else: self.header = header # 提交接口数据,调用接口,获取返回数据 self.res_code, self.res_content, self.res_headers = \ self._handle.handle_request(method, self.url, self.param, self.header) # 根据res_code断言 # assert self.res_code >=200 and self.res_code <300 # 根据返回结果与用例中预期结果对比进行断言 # 使用unittest的assert断言 self.assertIn(self.code, self.res_content.decode()) self.assertIn(self.result, self.res_content.decode()) # 使用python的assert断言 # assert self.code in self.res_content.decode() # assert self.result in self.res_content.decode() # 如果teardown不为空,进行处理 if teardown != "": self._handle.handle_teardown(teardown, self.res_content) except Exception as e: self.logger.error(traceback.format_exc()) print('traceback.print_exc(): %s,%s' % (traceback.print_exc(), e)) self.assertTrue(0) # 此处在用例抛出异常时加入断言,assertTrue(0)必定返回fasle,说明用例失败 finally: # 无论是否异常,输出url等信息到日志文件方便排查问题 self.logger.info("code: %s" % self.code) self.logger.info("result: %s" % self.result) self.logger.info("url: %s" % self.url) self.logger.info("yaml_data: %s" % data_read.get_yaml()) self.logger.info( "-----------------------------------------------------------") self.logger.info("res_code: %s" % self.res_code) self.logger.info("res_content: %s" % self.res_content.decode()) self.logger.info("res.headers: %s" % self.res_headers) self.log.build_end_line(case_name)
def get_header(self, key): yamldata = data_read.get_yaml() token = yamldata[key] header = "{'token': '%s'}" % token return demjson.decode(header)