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_param(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 # 打印到报告的日志中 print "handle_param-data:%s && type(data):%s" % (data, type(data)) return demjson.decode(data)
def set_global(self, data): # 先读取数据 yamldata = data_read.get_yaml() if yamldata == None: yamldata = data else: # 如果有读取数据,则更新数据 yamldata.update(data) # 数据写入文件 data_read.set_yaml(yamldata)
def testmain(self, case_id, setup, header, case_name, url, method, path, param, teardown, test_assert): # 打印到报告的日志中 try: print "case_%s" % case_id print "yaml_data:%s" % data_read.get_yaml() self.path = self._handle.handle_url(path) self.url = "%s%s" % (url, self.path) print "url:%s" % self.url print("--------------------------------------------") # 处理param,将其中参数替换成具体值 # 此处当手动修改excel中admin_pwd的值为11111时无法获取到值,当复制黏贴user_pwd的值过去时可以获取,当为非数字时也可以 if "{" in param: self.param = self._handle.handle_param(param) else: self.param = param # 处理前置hender,从yaml中获取token if header != "": self.header = self._handle.handle_header(header) else: self.header = header # 获取接口返回 res_code, res_content, res_headers = self._handle.handle_request( method, self.url, self.param, self.header) # 打印到报告的日志中 print "res.code:%s, type:%s" % (res_code, type(res_code)) print "res.content:%s" % res_content print "res.headers:%s" % res_headers # 断言,res_code返回值在这个范围则用例成功,否则失败 assert res_code >= 200 and res_code < 300 # 处理返回值,将其中后续接口需要用到的参数保存到yaml,如token,cardid等 if teardown != "": self._handle.handle_teardown(teardown, res_content) except Exception, e: print 'traceback.print_exc():%s,%s' % (traceback.print_exc(), e) self.assertTrue(0)
def get_header(self, key): yamldata = data_read.get_yaml() token = yamldata[key] header = "{'authorization': 'Bearer %s'}" % token return demjson.decode(header)