def start_error_test_by_task(data: TaskRequest): """ 运行本次任务中的错误用例 :param data: selected_node记录了错误的用例 :return: """ if data.selected_node is None or len(data.selected_node) == 0: return make_error_response("执行错误:没有找到错误的页面") task = service.find_task_by_task_id(data.group_id, data.task_id) plan = service.find_plan_by_id(task.plan_id) version = service.find_version_by_id(plan.version_id) page_data = service.find_selected_pages_by_selected_node( version, data.selected_node) if len(page_data) == 0: return make_error_response('执行错误:没有找到错误的页面') for page_data_item in page_data: method_data_list = [] for d in page_data_item['method_data']: for method_data in d['data']: method_data_list.append(method_data) plan_data_cover_page_data(plan.data, page_data_item['data'], method_data_list) task.time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) task.group_id = random_id() task.task_id = random_id() task.page_data = page_data task.name = task.name + ":只执行错误用例" task.session_id = session_id task.wait() service.update_task(task) return make_no_data_response()
def del_page_data(data: SavePageDataRequest): """ 删除页面数据 :param data: :return: """ version = service.find_version_by_id(data.version_id) if version is None: return make_error_response("版本错误") page_info = service.find_pages_info(version) node = find_node_by_key(page_info, data.node_key) if node is None: return make_error_response("没有找到该节点信息") pending_del_id = [item for item in map(lambda item: item.id, data.data)] new_data = [ item for item in filter(lambda item: item.id not in pending_del_id, node.data) ] node.data = new_data if service.save_pages_info(version, page_info): return make_no_data_response("删除成功") else: return make_no_data_response("删除失败")
def save_page_data(data: SavePageDataRequest): """ 保存页面的数据 :param data: :return: """ names = [item for item in map(lambda item: item.name, data.data)] set_name = set(names) if len(set_name) != len(names): return make_error_response("不可以重复添加变量名") version = service.find_version_by_id(data.version_id) if version is None: return make_error_response("版本错误") page_info = service.find_pages_info(version) node = find_node_by_key(page_info, data.node_key) if node is None: return make_error_response("没有找到该节点信息") node.data = data.data service.save_pages_info(version, page_info) return make_no_data_response()
def start_test(data: StartTestRequest): """ 开始测试 :param data: 请求的参数 :return: """ if data.plan_id is None: return make_error_response('请选择计划') if data.app_id is None: return make_error_response('请选择应用包名') plans = service.get_test_plans(data.plan_id) if len(plans) == 0: return make_error_response('找不到该计划') group_id = random_id() # 随机分配一个group id groups: List[Task] = [] def start_for_task(plan: PlanModel): # 从测试计划中获取需要测试的页面 page_data = service.find_selected_pages_by_plan(plan) if len(page_data) == 0: return make_error_response('请先在`{0}`中选择要测试的页面'.format(plan.name)) # 获取页面的数据和测试方法的参数 for page_data_item in page_data: method_data_list = [] for d in page_data_item['method_data']: for method_data in d['data']: method_data_list.append(method_data) plan_data_cover_page_data(plan.data, page_data_item['data'], method_data_list) # 随机分配一个任务id task_id = random_id() device = Device(data.platform, data.ip, data.port, data.device_id) app_info = AppInfo(data.app_id) task = Task(session_id, group_id, task_id, plan.plan_id, plan.name, device, app_info, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), page_data) # 设置为等待测试状态 task.wait() groups.append(task) # 一个测试计划里面可能包含多个子测试计划 for item in plans: error_info = start_for_task(item) if error_info is not None: return error_info # 存进数据库中 service.update_tasks(groups) return make_no_data_response()
def start_for_task(plan: PlanModel): # 从测试计划中获取需要测试的页面 page_data = service.find_selected_pages_by_plan(plan) if len(page_data) == 0: return make_error_response('请先在`{0}`中选择要测试的页面'.format(plan.name)) # 获取页面的数据和测试方法的参数 for page_data_item in page_data: method_data_list = [] for d in page_data_item['method_data']: for method_data in d['data']: method_data_list.append(method_data) plan_data_cover_page_data(plan.data, page_data_item['data'], method_data_list) # 随机分配一个任务id task_id = random_id() device = Device(data.platform, data.ip, data.port, data.device_id) app_info = AppInfo(data.app_id) task = Task(session_id, group_id, task_id, plan.plan_id, plan.name, device, app_info, time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), page_data) # 设置为等待测试状态 task.wait() groups.append(task)
def copy_plan(data: PlanHandleRequest): """ 复制测试计划 :param data: :return: """ plan: PlanModel = service.find_plan_by_id(data.plan_id) if data.version_id is None and plan.parent_id is not None: parent_plan = service.find_plan_by_id(plan.parent_id) data.version_id = parent_plan.version_id version = service.find_version_by_id(data.version_id) if version is None: return make_error_response("版本错误") def set_id(p: PlanModel, parent_id): p.plan_id = random_id() p.version_id = version.version_id p.parent_id = parent_id for item in p.children: set_id(item, p.plan_id) set_id(plan, plan.parent_id) plan.name = data.name plan.version_id = version.version_id service.update_plan(plan) return make_no_data_response("创建成功")
def del_version(data: VersionHandleRequest): """ 删除版本 :param data: :return: """ version = service.find_version_by_id(data.version_id) if version is None: return make_error_response("版本错误") used = False plans = service.find_all_plan() def check_plan_version(plan: PlanModel): if plan.version_id == version.version_id: return True if plan.children.__len__() > 0: for child in plan.children: result = check_plan_version(child) if result: return True for item in plans: used = check_plan_version(item) if used: break if used: version.is_del = True service.update_pages_version_info(version) else: service.del_pages_versions(version) service.del_pages_info(version) return make_no_data_response("删除成功")
def start_test_by_task(data: TaskRequest): """ 通过一个任务启动测试,一般用于回测 :param data: :return: """ task = service.find_task_by_task_id(data.group_id, data.task_id) if task is None: return make_error_response('该任务已不存在') plan = service.find_plan_by_id(task.plan_id) if plan is None: return make_error_response('该计划已不存在') task.group_id = random_id() task.task_id = random_id() task.time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) task.session_id = session_id # 更新为本次的session task.wait() service.update_task(task) return make_no_data_response()
def save_plan_data(data: PlanHandleRequest): """ 保存测试计划中的页面数据, :param data: :return: """ names = [item for item in map(lambda item: item.name, data.data)] set_name = set(names) if len(set_name) != len(names): return make_error_response("不可以重复添加变量名") plan = service.find_plan_by_id(data.plan_id) if plan is None: return make_error_response("没有找到该计划") plan.data = data.data service.update_plan(plan) return make_no_data_response()
def update_selected_page_node(data: PlanHandleRequest): """ 更新测试计划中选择的测试页面 :param data: :return: """ names = [item for item in map(lambda item: item.name, data.data)] set_name = set(names) if len(set_name) != len(names): return make_error_response("不可以重复添加变量名") plan = service.find_plan_by_id(data.plan_id) if plan is None: return make_error_response("没有找到该计划") plan.selected_page_node = data.selected_page_node service.update_plan(plan) return make_no_data_response()
def add_version(data: VersionHandleRequest): """ 添加版本 :param data: :return: """ version = VersionModel.create(data.version_name, data.path) if not service.parse_pages_info_and_save(version): return make_error_response("添加失败,请检查你的代码路径(path)") service.update_pages_version_info(version) return make_no_data_response("添加成功")
def del_plan_data(data: PlanHandleRequest): """ 删除测试计划的页面数据 :param data: :return: """ plan = service.find_plan_by_id(data.plan_id) if plan is None: return make_error_response("没有找到该计划") pending_del_id = [item for item in map(lambda item: item.id, data.data)] new_data = [ item for item in filter(lambda item: item.id not in pending_del_id, plan.data) ] plan.data = new_data service.update_plan(plan) return make_no_data_response()
def add_plan(data: PlanHandleRequest): """ 添加测试计划 :param data: :return: """ version = service.find_version_by_id(data.version_id) if version is None: return make_error_response("版本错误") plan = PlanModel.create(data.name, data.version_id, data.parent_id) if data.parent_id is not None: parent_plan = service.find_plan_by_id(data.parent_id) if parent_plan is not None: plan.selected_page_node = parent_plan.selected_page_node for plan_data in parent_plan.data: plan.data.append(plan_data.copy()) service.update_plan(plan) return make_no_data_response("创建成功")
def copy_version(data: VersionHandleRequest): """ 复制一个版本 :param data: :return: """ version = VersionModel.create(data.version_name, data.path) pages_data = [] try: pages_data = dir_modules(version.path) except: pass if len(pages_data) == 0: return make_error_response("添加失败,请检查你的代码路径(path)") old_pages_data = service.find_pages_info(data) replace_pages_data(old_pages_data, pages_data) service.save_pages_info(version, pages_data) service.update_pages_version_info(version) return make_no_data_response("添加成功")