def disconf_execute_new(disconf_obj): ''' 操作disconf新版本 :param disconf_obj: :return: ''' logger.debug("操作disconf入参为: {0}".format(disconf_obj)) try: disconf_method = disconf_obj.pop("disconf_method") disconf_id = disconf_obj.pop("disconf_id") disconf_name = disconf_obj.pop("disconf_name") disconf_value = disconf_obj.pop("disconf_value") disconf_host = disconf_obj.pop("disconf_host") except KeyError: return make_response({"code": "100", "desc": "入参校验失败"}) if disconf_method == "edit": return edit_disconf_value(disconf_host, disconf_id, disconf_name, disconf_value) elif disconf_method == "add": return add_disconf_value(disconf_host, disconf_id, disconf_name, disconf_value) elif disconf_method == "delete": return del_disconf_value(disconf_host, disconf_id, disconf_name, disconf_value) elif disconf_method == "query": return query_disconf_value(disconf_host, disconf_id, disconf_name) else: return make_response({"code": "100", "desc": "入参校验失败"})
def disconf_execute(disconf_obj): ''' 操作disconf :param disconf_obj: :return: ''' logger.debug("操作disconf入参为: {0}".format(disconf_obj)) try: disconf_method = disconf_obj.pop("disconf_method") disconf_id = disconf_obj.pop("disconf_id") disconf_name = disconf_obj.pop("disconf_name") disconf_value = disconf_obj.pop("disconf_value") env_name = disconf_obj.pop("env_name") except KeyError: return make_response({"code": "100", "desc": "入参校验失败"}) try: obj = EnvInfo.query.filter_by(env_name=env_name.upper()).first() disconf_host = obj.disconf_host except Exception as err: raise Exception('\n'.join([str(err), traceback.format_exc()])) if disconf_method == "edit": return edit_disconf_value(disconf_host, disconf_id, disconf_name, disconf_value) elif disconf_method == "add": return add_disconf_value(disconf_host, disconf_id, disconf_name, disconf_value) elif disconf_method == "delete": return del_disconf_value(disconf_host, disconf_id, disconf_name, disconf_value) elif disconf_method == "query": return query_disconf_value(disconf_host, disconf_id, disconf_name) else: return make_response({"code": "100", "desc": "入参校验失败"})
def disconf_signin(disconf_host, cookie): url = disconf_host + '/api/account/signin' heard_info = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Cookie': cookie} body = "name=admin&password=admin&remember=0" try: response = requests.post(url=url, headers=heard_info, data=body) logger.debug("response.text: {0}".format(response.text)) except Exception as err: raise err return cookie
def disconf_session(disconf_host): url = disconf_host + '/api/account/session' cookie = None heard_info = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'} # 请求头 try: response = requests.get(url=url, headers=heard_info) logger.debug("response.headers: {0}".format(response.headers)) cookie = response.headers['Set-Cookie'] # 获取JSESSIONID logger.debug("cookie: {0}".format(cookie)) except Exception as err: raise err return cookie
def disconf_put_config(disconf_host, cookie, config_id, body): url = disconf_host + '/api/web/config/filetext/' + str(config_id) heard_info = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Cookie': cookie} body = "fileContent=" + parse.quote(body.encode('utf-8')) # urlencode str_response = None try: response = requests.put(url=url, headers=heard_info, data=body) logger.debug("上传disconf配置返回结果: {0}".format(response.text)) str_response = json.loads(response.text) str_response = str_response['success'] except Exception as err: raise err return str_response
def disconf_get_config(disconf_host, cookie, config_id): url = disconf_host + '/api/web/config/' + str(config_id) heard_info = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Cookie': cookie} value = None try: response = requests.get(url=url, headers=heard_info) logger.debug("response.text: {0}".format(response.text)) response_dict = json.loads(response.text) logger.info('response_dict: {}'.format(response_dict)) value = response_dict['result']['value'] except Exception as err: raise err return value
def server_upload_file(ssh_connect, local_path, remote_path): """ 向指定服务器上传文件 :param ssh_connect: :param local_path: :param remote_path: :return: """ if isinstance(ssh_connect, str): ssh_info = eval(ssh_connect) else: ssh_info = ssh_connect # 实例化Transport ssh = paramiko.Transport(ssh_info[0], ssh_info[1]) # 建立连接 ssh.connect(username=ssh_info[2], password=ssh_info[3]) # 实例化一个sftp对象 transport = paramiko.SFTPClient.from_transport(ssh) try: if os.path.isdir(local_path): # 判断本地参数是目录还是文件 local_path_list = os.listdir(local_path) for f in local_path_list: # 遍历本地目录 transport.put(os.path.join(local_path + f), os.path.join(remote_path + f)) # 上传目录中的文件 logger.debug("上传{0}目录文件成功:{1}".format(local_path, local_path_list)) else: transport.put(local_path, remote_path) # 上传文件 logger.debug("上传文件成功:{}".format(local_path)) except Exception as e: logger.debug('上传文件异常:', e) ssh.close()
def product_line_subtree_old(self): """根据公司id查询配置在该公司下的产品线-全链路用例""" try: company_id = self.data.pop('companyId') except KeyError: return make_response({"code": "100", "desc": CODE_DESC_MAP["100"]}) subtree = [] index_id = 0 result_list = self.acim.query_api_product_line_subtree(company_id) logger.debug(result_list) result_dic = db_result_to_map(result_list) logger.debug(result_dic) for p_k, p_dic in result_dic.items(): p_name = p_dic.pop('name') index_id += 1 p_tree = { 'id': index_id, 'label': p_name, 'productLineId': p_k, 'children': [] } for t_k, t_dic in p_dic.items(): t_name = t_dic.pop('name') index_id += 1 t_tree = { 'id': index_id, 'label': t_name, 'testcaseId': t_k, 'children': [] } p_tree['children'].append(t_tree) subtree.append(p_tree) return make_response({"code": "000", "data": subtree})
def api_run_test(**kwargs): report_id = kwargs.pop('report_id', None) plan_name = kwargs.pop('plan_name', None) project_id = kwargs.pop('project_id', None) testcase_main_id_list = kwargs.get('testcase_main_id_list', None) failfast = kwargs.pop('failfast', False) if testcase_main_id_list: is_main = True else: is_main = False try: logger.debug( '=============================={dir}run_{report_id}.log'.format( dir=run_case_log_dir, report_id=report_id)) hr_kwargs = { "failfast": failfast, "log_path": '{dir}run_{report_id}.log'.format(dir=run_case_log_dir, report_id=report_id) } runner = HttpRunner(**hr_kwargs) # res = load_test(**kwargs) # testset = res[0] # test_meta_list = res[1] # project_id = res[2] loader = ApiTestLoader(**kwargs) testset = loader.get_testset_list() test_meta_list = loader.get_test_meta_list() if not testset: raise LoadCaseError('没有可执行的用例') logger.debug("{1} testset:{0}".format(testset, type(testset))) except Exception as err: save_report(report_path=None, runner_summary=None, project_id=project_id, report_id=report_id) hr_logger.log_error("【ERROR】组装用例出错!") hr_logger.log_error('\n'.join([str(err), traceback.format_exc()])) hr_logger.log_info("【END】测试结束!") hr_logger.remove_handler(runner.handler) raise LoadCaseError try: # summary = run(testset, report_name='testMock') start_time = time.strftime('%Y-%m-%d %H-%M-%S', time.localtime(time.time())) hr_logger.log_info("【START】测试开始! (ง •_•)ง") hr_logger.log_info("【环境】: {}".format(kwargs.get('env_name', None))) # time.sleep(3) try: testset_json = json_dumps(testset) except Exception: testset_json = testset hr_logger.log_debug("【调用HttpRunner】: {0}".format(testset_json)) runner.run(testset) hr_logger.log_info("【结束调用HttpRunner】") # raise RunCaseError perfect_summary(runner.summary, test_meta_list) """记录用例复用记录""" summary_remove_file_obj(runner.summary) summary_for_reuse = copy.deepcopy(runner.summary) summary_for_reuse = add_memo(summary_for_reuse) # 识别错误 summary_for_reuse = identify_errors(summary_for_reuse) # 更新api_testcase_reuse_record表, 并获取covered_intf_id_set, run_cases, success_cases save_testcase_reuse_record([{ "summary": json_loads(json_dumps(summary_for_reuse)) }]) del summary_for_reuse # hr_logger.log_info("【runner.summary】: {}".format(runner.summary)) '''报告优化:1、汉化(包括日志里面的字段) 2、开始时间和持续时间合并成一行 3、增加一个字段“错误类型”,如果用例错误,显示该字段,并说明期望与预期值; 否则该字段不显示 4.log里面去掉一些数据重复和不重要的;行和字段(请求headers,返回体的headers,reason,url,“”ok”) 5.将请求体和返回值数据缩进,且字典里面的key颜色加粗 6.新增接口请求类型字段,http、dubbo、mq''' for detail in runner.summary["details"]: for record in detail["records"]: '''增加用例类型:test_meta_list["intf_type"]''' record["intf_type"] = test_meta_list[0]["intf_type"] '''删除报告一些无需关注的字段''' request_keys = ["json", "start_timestamp"] response_keys = [ "elapsed_ms", "encoding", 'ok', 'url', 'reason', 'cookies' ] for request_key in request_keys: if request_key in record["meta_data"]["request"]: del record["meta_data"]["request"][request_key] for respones_key in response_keys: if respones_key in record["meta_data"]["response"]: del record["meta_data"]["response"][respones_key] '''record.status出现error, 抛出错误信息''' if record['status'] == 'error': error_msg = record['attachment'] raise Exception(error_msg) # '''将body和content字节类型转换dic''' # if "body" in record["meta_data"]["request"].keys() and "content" in record["meta_data"]["response"].keys(): # request_body = record["meta_data"]["request"].pop("body") # response_content = record["meta_data"]["response"].pop("content") # if not request_body: # request_body_dic = {} # else: # try: # request_body_dic = json.loads(request_body) # except TypeError: # request_body_dic = json.loads(request_body.decode('utf-8')) # # 增加捕获异常 # except UnicodeDecodeError: # if isinstance(request_body, bytes): # request_body_dic = {} # # request_body_dic = request_body.decode('utf-8', 'ignore') # else: # request_body_dic = {} # # if not response_content: # response_content_dic = {} # else: # try: # response_content_dic = json.loads(response_content) # except TypeError: # response_content_dic = json.loads(response_content.decode('utf-8')) # except json.decoder.JSONDecodeError: # response_content_dic = {} # # record["meta_data"]["request"]["body"] = request_body_dic # record["meta_data"]["response"]["content"] = response_content_dic # # '''将files去除,避免报告超长影响展示效果''' # if "files" in record["meta_data"]["request"].keys(): # record["meta_data"]["request"].pop("files") '''报告增加一列:错误类型:''' for validate in record["meta_data"]["validators"]: if validate["comparator"] == "json_contains": check_value = validate["check_value"] expect_value = validate["expect"] if json_contains(check_value, expect_value) is not True: validate["check_result"] = "fail" record["status"] = "failure" detail["stat"]["failures"] += 1 detail["stat"]["successes"] -= 1 runner.summary["stat"]["failures"] += 1 runner.summary["stat"]["successes"] -= 1 error_log = ("预期:{}未在返回报文内".format(expect_value)) validate["error_log"] = { "json_contains": error_log } elif validate["comparator"] == "db_validate": check_value = validate["check_value"] expect_value = validate["expect"] if db_validate(check_value, expect_value) is not True: validate["check_result"] = "fail" record["status"] = "failure" detail["stat"]["failures"] += 1 detail["stat"]["successes"] -= 1 runner.summary["stat"]["failures"] += 1 runner.summary["stat"]["successes"] -= 1 error_log = ("预期:{0},实际是:{1}".format( expect_value, check_value)) validate["error_log"] = {"db_validate": error_log} elif validate["comparator"] == "db_json_validate": check_value = validate["check_value"] expect_value = validate["expect"] if not db_json_validate(check_value, expect_value): validate["check_result"] = "fail" record["status"] = "failure" detail["stat"]["failures"] += 1 detail["stat"]["successes"] -= 1 runner.summary["stat"]["failures"] += 1 runner.summary["stat"]["successes"] -= 1 error_log = ("预期:{0},实际是:{1}".format( expect_value, json.dumps(check_value).encode('utf-8').decode( 'unicode_escape'))) validate["error_log"] = { "db_json_validate": error_log } hr_logger.log_info("【runner.summary】: {}".format( json_dumps(runner.summary))) runner_summary = copy.deepcopy(runner.summary) """把每条用例执行成功与否记录到testcase_info.last_run""" try: save_last_run(runner_summary, is_main=is_main) except Exception as e: logger.error('\n'.join([str(e), traceback.format_exc()])) # hr_logger.log_error("【ERROR】运行用例出错!") # hr_logger.log_error('\n'.join([str(e), traceback.format_exc()])) # logger.debug("runner_summary_list{}".format(runner.summary)) # report_path = runner.gen_html_report( # html_report_name=plan_name if plan_name else 'default', # html_report_template=config.REPORT_TEMPLATE_PATH, # html_report_dir=config.REPORT_DIR # ) # logger.debug('report_path:{}'.format(report_path)) # report_path = report_path.split('reports')[1] # report_url = get_host() + r':8899/reports' + report_path # # logger.debug('AC report_path:{}'.format(report_path)) report_url = '不生成报告' save_report(report_url, runner_summary, project_id, report_id=report_id, is_main=is_main) except Exception as err: save_report(report_path=None, runner_summary=runner.summary, project_id=project_id, report_id=report_id) hr_logger.log_error("【ERROR】运行用例出错!") hr_logger.log_error('\n'.join([str(err), traceback.format_exc()])) raise RunCaseError finally: hr_logger.log_info("【END】测试结束!") hr_logger.remove_handler(runner.handler) return report_url