def load_project_case(id, time_path): """根据项目加载用例""" project = ProjectInfo.get(int(id)) if not project: raise DatabaseException('没有 module', id) modules = project.module_records if not modules: raise DatabaseException() for m in modules: load_module_case(m.id, time_path) return True
def load_module_case(id, time_path): """根据模块加载用例""" module = ModuleInfo.get(int(id)) if not module: raise DatabaseException() cases = module.case_records if not cases: raise DatabaseException() for c in cases: if not load_case_case(c.id, time_path): raise RunningTestException return True
def load_case_case(case_id, time_path): """ 加载单个case用例信息, httprunner, [] ==> yml.loads() :param index: int or str:用例索引 """ case = CaseInfo().get(int(case_id)) if not case: raise DatabaseException('没有 case ', case_id) # TODO: base_url 根据项目和接口拼接 config = { "config": { "name": case.name, "request": { "base_url": None } } } case_info = [] case_info.append(config) # 获取用例信息 # TODO: include 添加 if case.include: include = eval(case.include) for pre_case_list in include: pre_case = CaseInfo.get(int(pre_case_list[0])) request = json.loads(pre_case.request) name = pre_case.name pre_case = { "test": request } pre_case["test"]["name"] = name case_info.append(pre_case) # include = eval(case.include) request = json.loads(case.request) name = case.name project = case.module.project module = case.module the_case = { "test": request } the_case["test"]["name"] = name case_info.append(the_case) # 生成测试用例的文件夹 gen_case_file() project_dir = gen_project_dir(time_path, project.project_name) module_dir = os.path.join(project_dir, str(module.id) + module.module_name) if not os.path.exists(module_dir): os.mkdir(module_dir) case_file = os.path.join(module_dir, secure_filename(str(case.id) + name + '.yml')) f = open(case_file, 'w', encoding='utf-8') yaml.dump(case_info, f) return True
def insert(cls, form): # new = cls(email= form.email.data, _pwd=form.pwd.data) new = cls(email=form.email.data, pwd=form.pwd.data) try: db.session.add(new) db.session.commit() return new except: raise DatabaseException('数据库操作错误')
def save_by_form(cls, form): case_info = cls.case_info_by_form(form) case = cls(**case_info) try: db.session.add(case) db.session.commit() return case except: raise DatabaseException('数据库操作错误')
def delete(cls, id): """TODO: 删除项目下面所有的module 和 cases, suites""" project = cls.query.get(int(id)) project.status = cls.DELETE_STATUS try: db.session.add(project) db.session.commit() return project except Exception as e: raise DatabaseException('数据库操作异常,请联系管理员')
def edit_by_form(self, form): """修改项目""" self.project_name = form.project_name.data self.simple_desc = form.simple_desc.data try: db.session.add(self) db.session.commit() return self except Exception as e: current_app.logger.error('修改项目错误') raise DatabaseException('数据库操作异常,请联系管理员')
def update_by_form(self, form): """更新测试用例""" self.name = form.data.name self.includes = form.data.includes try: db.session.add(self) db.session.commit() return self except: raise DatabaseException('数据库操作错误')
def add_by_form(cls, form): suite = cls( name=form.name.data, includes=str(form.includes.data) ) try: db.session.add(suite) db.session.commit() return suite except: raise DatabaseException('数据库操作错误')
def update(self, form): """更新测试用例""" case_info = self.case_info_by_form(form) for k, v in case_info.items(): setattr(self, k, v) try: db.session.add(self) db.session.commit() return self except: raise DatabaseException('数据库操作错误')
def delete(self): """删除记录 bug: 1.项目还会展示 3.module,case还会展示? """ self.status = self.DELETE_STATUS try: db.session.add(self) db.session.commit() return self except Exception as e: current_app.logger.error('project 更新出错') return DatabaseException('project 数据异常')
def update(self, data): """更新项目 例子:data:{'project_name':'sf','simple_desc':'sf'} """ self.project_name = data.get('project_name', '') self.simple_desc = data.get('simple_desc', '') try: db.session.add(self) db.session.commit() return self except Exception as e: #logger current_app.logger.error('project 更新出错') # db.session.roolbaclk() # 抛出异常 return DatabaseException('project 数据异常')
def insert(cls, data): """添加新的项目 例子:data:{'project_name':'sf','simple_desc':'sf'} """ project = ProjectInfo(project_name=data.get('project_name', ''), simple_desc=data.get('simple_desc', '')) try: db.session.add(project) db.session.commit() return project except Exception as e: #logger current_app.logger.error('project 保存出错') # db.session.roolbaclk() # 抛出异常 return DatabaseException('project 数据异常')