def tearDown(self): # 忽略掉io告警 warnings.simplefilter("ignore", ResourceWarning) Log.debug(self.result) # 保存到db if self.result: get_test_info = self._getTestInfo() self._db_all.inster({ 'method_name': get_test_info['method_name'], 'data': self.data, 'result': self.result # 返回提交的data的json }) if self.yaml_config.get('interruptContinue') is True: # Log.debug('-----------------------') # demo = self._db_complete.find('method_name',get_test_info['method_name']) # Log.debug(demo) # 保存到 结果队列 self._db_complete.inster({ 'status': 1, 'method_name': get_test_info['method_name'] })
def run(args=None): path = os.getcwd() + '/run.py' is_exists = os.path.exists(path) if not is_exists: # 如果不存在则创建目录 Log.debug("当前目录没有run.py文件,请进入正确目录") else: # 如果目录存在则不创建,并提示目录已存在 os.system("python3 " + path)
def run(self): Log.debug("开始执行") # 每次进入时清除db self.__db_clean() # 获取开始时间 start_time = time.time() Log.debug("2-开始时间:" + Utils.get_time(start_time)) # 执行所有用例 get_result = self.__get_runner_result() # 结束时间 end_time = time.time() Log.debug("7-结束时间" + Utils.get_time(end_time)) # 获取用例返回的各种数据 get_case_data = self.__get_case_return_data(get_result['method_names'], get_result['result']) # 生成报告 self.__build_report(start_time, end_time, get_case_data) Log.debug("9-总共用时" + str(end_time - start_time) + 's')
def __get_runner_discover(self): Log.debug("4-获取所有用例") # 重写TestLoader 用于后期队列及报错连续执行 discover = TestLoader().discover(self.project_path + '/apis/', pattern='*_st.py', top_level_dir=None) # 获取所有用例和方法名 method_names = self.__get_discover_cases(discover) # 读取config interruptContinue:中断后开始是否继续开始 if self.yaml_config.get("interruptContinue") is True: # 拿到方法名和结果队列比较 discover = self.__get_case_incomplete(method_names) return discover, method_names
def __get_case_return_data(self, method_names, results): Log.debug("8-重组数据") db = self.db_all.select() all_data = [] # 遍历所有用例方法 for name in method_names: info = {'status_type': 'success'} for result in results: # 匹配db里对应的方法 for item in db: if name['method_name'] in item['method_name']: # 提交 info['name'] = item['data']['name'] info['url'] = item['data']['url'] info['mode'] = item['data']['mode'] info['submit_data'] = item['data']['data'] # 返回 info['code'] = item['result']['status_code'] info['status'] = item['result']['status'] # 状态类型 正确:success 错误:error 跳过:skipped info['status_type'] = item['result']['status'] info['data'] = item['result']['response'] info['time'] = item['result']['time'] info['msg'] = item['result']['msg'] # 获取跳过的 for item in result.skipped: if name['method_name'] in self.__get_result_name(item): info['status_type'] = 'skipped' # 获取失败的 for item in result.failures: if name['method_name'] in self.__get_result_name(item): info['status_type'] = 'error' # failures # 获取错误的 for item in result.errors: if name['method_name'] in self.__get_result_name(item): info['status_type'] = 'error' all_data.append(info) return all_data
def __get_runner_result(self): Log.debug("3-执行runner") discover, method_names = self.__get_runner_discover() Log.debug("5-执行测试用例") for i in discover: self.__runner_run(i) # 在没有全局值的情况下不需要这两东西 # self.db_all.inster_all(self.db_all.select()) # self.db_complete.inster_all(self.db_complete.select()) Log.debug("6-获取用例结果") return {'method_names': method_names, 'result': self.all_result}
# 统一引用lan里的TaskSet和task from locust import TaskSet, task from lan import Log # 初始化Log logfile=true 生成log文件 log = Log(logfile=True) # 所有任务 class Tasks(TaskSet): # 任务1 @task(1) def task1(self): response = self.client.get("/api/getlist/1") result = response.json() if result['code'] == 200: log.info("请求成功") else: log.error("失败")
def __db_clean(self): Log.debug("1-清除缓存") self.db_all.remove() # 中断后开始是否继续开始 if self.yaml_config.get("interruptContinue") is False: self.db_complete.remove()
def __build_report(self, start_time, end_time, case_data): Log.debug("8-生成报告") ReporeHtml(start_time, end_time, case_data).build()