def run(self, request, *args, **kwargs): """运行用例""" # 取出并构造参数 instance = self.get_object() # serializer = self.get_serializer(data=request.data) # serializer.is_valid(raise_exception=True) response = super().create(request, *args, **kwargs) env_id = response.data.serializer.validated_data.get('env_id') testcase_dir_path = os.path.join( settings.SUITES_DIR, datetime.strftime(datetime.now(), '%Y%m%d%H%M%S%f')) # 创建一个以时间戳命名的路径 os.mkdir(testcase_dir_path) env = Envs.objects.filter(id=env_id).first() # 生成yaml用例文件 common.generate_testcase_file(instance, env, testcase_dir_path) # 运行用例(生成报告) common.run_testcase(instance, testcase_dir_path)
def run(self, request, *args, **kwargs): # 取出并构造参数 instance = self.get_object() response = super().create(request, *args, **kwargs) env_id = response.data.serializer.validated_data.get('env_id') testcase_dir_path = os.path.join( settings.SUITES_DIR, datetime.strftime(datetime.now(), '%Y%m%d%H%M%S%f')) # 创建一个以时间戳命名的路径 os.mkdir(testcase_dir_path) env = Envs.objects.filter(id=env_id).first() interfaces_qs = Interfaces.objects.filter(instance) runner_testcases_obj = [] testcase_qs = Testcases.objects.filter(interface=interfaces_qs) if testcase_qs.exists(): runner_testcases_obj.extend(list(testcase_qs)) if len(runner_testcases_obj) == 0: data = {'ret': False, 'msg': '该项目下无用例存在,无法运行'} return Response(data, status=400) for testcase_obj in runner_testcases_obj: # 生成yaml用例文件 common.generate_testcase_file(testcase_obj, env, testcase_dir_path) # 运行用例(生成报告) common.run_testcase(instance, testcase_dir_path)
def run(self, request, *args, **kwargs): testcase = self.get_object() serializer = self.get_serializer(instance=testcase, data=request.data) serializer.is_valid(raise_exception=True) # 此时的data为校验后的数据 datas = serializer.validated_data env_id = datas.get('env_id') # 将文件夹时间戳处理化 testcase_dir_path = os.path.join(SUITES_DIR, datetime.strftime(datetime.now(), '%Y-%m-%d %H.%M.%S %f')) os.mkdir(testcase_dir_path) # 获取环境变量对象 env_obj = Envs.objects.get(id=env_id) # 生成yaml用例文件 common.generate_testcase_files(testcase, env_obj, testcase_dir_path) # 运行用例,返回报告 return common.run_testcase(testcase, testcase_dir_path)
def run(self, request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance, data=request.data) serializer.is_valid(raise_exception=True) datas = serializer.validated_data env_id = datas.get('env_id') testcase_dir_path = os.path.join(settings.SUITES_DIR, datetime.strftime(datetime.now(), '%Y%m%d%H%M%S%f')) os.mkdir(testcase_dir_path) env = Envs.objects.filter(id=env_id, is_delete=False).first() #生成yaml用例文件 common.generate_testcase_files(instance, env, testcase_dir_path) #运行用例 return common.run_testcase(instance, testcase_dir_path)
def run(self, request, *args, **kwargs): # 1. 获取模型类对象 instance = self.get_object() # 2. 校验 serializer = self.get_serializer(instance, data=request.data) serializer.is_valid(raise_exception=True) datas = serializer.validated_data env = Envs.objects.get(id=datas.get('env_id')) # 3. 生成yaml测试用例文件 testcase_dir_path = os.path.join(settings.SUITES_DIR, datetime.strftime(datetime.now(), '%Y%m%d%H%M%S%f')) common.generate_testcase_files(instance, env, testcase_dir_path) # 传入接口对象,环境 ,用例目录 # 4. 运行用例 return common.run_testcase(instance, testcase_dir_path)
def run(self, request, *args, **kwargs): # 1、取出用例的模型对象 instance = self.get_object() serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) env_id = serializer.validated_data.get('env_id') env = Envs.objects.get(id=env_id) # 2、创建一个以时间戳命名的目录 testcase_dir_path = os.path.join( settings.SUITES_DIR, datetime.strftime(datetime.now(), "%Y%m%d%H%M%S")) os.mkdir(testcase_dir_path) # 3、生成用例的yaml配置文件 common.generate_testcase_file(instance, env, testcase_dir_path) # 4、运行用例并生成测试报告 return common.run_testcase(instance, testcase_dir_path)
def run(self, request, *args, **kwargs): # 取出并构造参数 instance = self.get_object() response = super().create(request, *args, **kwargs) env_id = response.data.serializer.validated_data.get('env_id') testcase_dir_path = os.path.join( settings.SUITES_DIR, datetime.strftime(datetime.now(), '%Y%m%d%H%M%S%f')) # 创建一个以时间戳命名的路径 os.mkdir(testcase_dir_path) env = Envs.objects.filter(id=env_id).first() testcase_objs = Testcases.objects.filter(interface=instance) if not testcase_objs.exists(): # 如果此接口下没有用例, 则无法运行 data = {'ret': False, 'msg': '此接口下无用例, 无法运行'} return Response(data, status=400) for one_obj in testcase_objs: common.generate_testcase_file(one_obj, env, testcase_dir_path) # 运行用例 return common.run_testcase(instance, testcase_dir_path)
def run(self, request, *args, **kwargs): interface = self.get_object() serializer = self.get_serializer(instance=interface, data=request.data) serializer.is_valid(raise_exception=True) # 此时的data为校验后的数据 datas = serializer.validated_data env_id = datas.get('env_id') # 将文件夹时间戳处理化 testcase_dir_path = os.path.join(SUITES_DIR, datetime.strftime(datetime.now(), '%Y-%m-%d_%H.%M.%S_%f')) os.mkdir(testcase_dir_path) # 获取环境变量对象 env_obj = Envs.objects.get(id=env_id) # 获取接口下所有用例 testcase_objs = Testcases.objects.filter(interface=interface) if not testcase_objs.exists(): data = { 'detail': '此接口下无用例,无法运行' } return Response(data, status=status.HTTP_400_BAD_REQUEST) for case_obj in testcase_objs: common.generate_testcase_files(case_obj, env_obj, testcase_dir_path) # 运行用例 return common.run_testcase(interface, testcase_dir_path)
def run(self, request, *args, **kwargs): # 取出并构造参数 instance = self.get_object() serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) env_id = serializer.validated_data.get('env_id') env = Envs.objects.get(id=env_id) testcase_dir_path = os.path.join( settings.SUITES_DIR, datetime.strftime(datetime.now(), '%Y%m%d%H%M%S%f')) # 创建一个以时间戳命名的路径 os.mkdir(testcase_dir_path) interface_qs = Interfaces.objects.filter(project=instance) if not interface_qs.exists(): data = {'ret': False, 'msg': '此项目下无接口,无法运行'} return Response(data, status=400) runnable_testcase_obj = [] for interface_obj in interface_qs: # 当前接口项目的用例所在查询集对象 testcase_qs = Testcases.objects.filter(interface=interface_obj) if testcase_qs.exists(): # 将两个列表合并 runnable_testcase_obj.extend(list(testcase_qs)) if len(runnable_testcase_obj) == 0: data = {'ret': False, 'msg': '此项目下无用例,无法运行'} return Response(data, status=400) for testcase_obj in runnable_testcase_obj: # 生成yaml用例文件 common.generate_testcase_file(testcase_obj, env, testcase_dir_path) # 运行用例(生成报告) return common.run_testcase(instance, testcase_dir_path)
def run(self, request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance, data=request.data) serializer.is_valid() datas = serializer.validated_data env_id = datas.get('env_id') testcase_dir_path = os.path.join( settings.SUITES_DIR, datetime.strftime(datetime.now(), '%Y%m%d%H%M%S%f')) if not os.path.exists(testcase_dir_path): os.mkdir(testcase_dir_path) # first()返回queryset查询集第一项 env = Envs.objects.filter(id=env_id, is_delete=False).first() include = eval(instance.include) if len(include) == 0: data_dict = {'detail': '此套件下未添加用例,无法运行'} return Response(data_dict, status=status.HTTP_400_BAD_REQUEST) # 将include中的接口id转换为此接口下的id include = get_testcases_by_interface_ids(include) for testcase_id in include: testcase_objs = Testcases.objects.filter(is_delete=False, id=testcase_id).first() if testcase_objs: # 生成yml文件 common.generate_testcase_files(testcase_objs, env, testcase_dir_path) # 运行用例 return common.run_testcase(instance, testcase_dir_path)
def yb_run_testcase(instance, testcase_dir_path): run_testcase(instance, testcase_dir_path)
def debug(self, request): # 1. 获取模型类对象 env = Envs.objects.get(id=request.data["env_id"]) # 3. 生成yaml测试用例文件 testcase_dir_path = os.path.join( settings.SUITES_DIR, datetime.strftime(datetime.now(), '%Y%m%d%H%M%S%f' + "_debug")) common.generate_debug_files(request.data, env, testcase_dir_path) summary = common.run_testcase(request.data, testcase_dir_path, False, debug=True) for i, item in enumerate(summary['details']): for j, record in enumerate(item['records']): if 'files' in record['meta_data']['request'].keys(): files_request = record['meta_data']['request']['files'] summary['details'][i]['records'][j]['meta_data'][ 'request']['files'] = [ files_request[file][0] for file in files_request ] summary['details'][i]['records'][j]['meta_data'][ 'request']['body'] = record['meta_data']['request'][ 'files'] summary = bytes2str(summary) # 返回用例中全部请求的结果树数据 tree = [] case_metas = [] # 保存用例的元数据,包括运行是否成功,断言结果等 for record in summary['details'][0]['records']: # 调试结果树增加请求信息 result = [{ 'title': '请求', 'expand': True, 'children': tidy_tree_data(record['meta_data']['request'], [], disabled=True), 'disabled': True, }, { 'title': '响应', 'expand': True, 'children': tidy_tree_data(record['meta_data']['response'], []) }] # one_record = tidy_tree_data(record['meta_data']['response'], []) # one_request = tidy_tree_data(record['meta_data']['request'], []) # 当前请求是否成功的标志、traceback(attachment)、validators case_run_info = dict() case_run_info['flag'] = record['status'] == 'success' case_run_info['attachment'] = record['attachment'] case_run_info['validators'] = record['meta_data']['validators'] case_run_info['name'] = record['name'] # 此为附加信息,页面展示时要先取出该数据 case_metas.append(case_run_info) tree.append(result) return_info = { 'tree': tree, 'case_metas': case_metas, 'summary': summary } return Response(return_info)