def plan_run(request): if request.method == 'POST': plan_id = request.POST['plan_id'] plan = Plan.objects.get(plan_id=plan_id) env_id = plan.environment.env_id case_id_list = eval(plan.content) case_num = len(case_id_list) content = [] pass_num = 0 fail_num = 0 error_num = 0 for case_id in case_id_list: execute = Execute(case_id, env_id) case_result = execute.run_case() content.append(case_result) if case_result["result"] == "pass": pass_num += 1 if case_result["result"] == "fail": fail_num += 1 if case_result["result"] == "error": error_num += 1 report_name = plan.plan_name + "-" + time.strftime("%Y%m%d%H%M%S") if Report.objects.filter(plan=plan): Report.objects.filter(plan=plan).update(report_name=report_name, content=content, case_num=case_num, pass_num=pass_num, fail_num=fail_num, error_num=error_num) else: report = Report(plan=plan, report_name=report_name, content=content, case_num=case_num, pass_num=pass_num, fail_num=fail_num, error_num=error_num) report.save() return HttpResponse(plan.plan_name + " 执行成功!")
def test_httprunner(env_id, case_id_list, plan="", username="******"): logger.log_info('test_httprunner------->HttpRunner执行测试计划中<--------------') start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) begin_time = time.clock() case_num = len(case_id_list) content = [] j = 0 execute = Test_execute(env_id, case_id_list, run_mode="1", plan=plan) case_result = execute.test_case if "error" in case_result.keys(): logger.log_error(case_result["msg"]) return report_path = case_result['report_path'] for i in range(len(case_result['summary']['details'])): for records in case_result['summary']['details'][i]['records']: j += 1 records['id'] = j for data in records.get('meta_datas', {}).get('data', {}): body = json.dumps(data.get('request', {}).get('body', {}), ensure_ascii=False).replace('Markup', '').replace( '"', '') if body: import urllib.parse data['request']['body'] = urllib.parse.unquote( body.encode('utf-8').decode('unicode_escape').encode( 'utf-8').decode('unicode_escape')) content.append(case_result) summary = case_result.get('summary', {}) stat = summary.get('stat', {}).get('teststeps', {}) pass_num = stat.get('successes', 0) fail_num = stat.get('failures', 0) error_num = stat.get('errors', 0) skip_num = stat.get('skipped', 0) end_time = time.clock() totalTime = str(end_time - begin_time)[:6] + ' s' # pic_name = DrawPie(pass_num, fail_num, error_num, skip_num) report_name = plan.plan_name + "-" + str(start_time).replace(':', '-') report = Report(plan_id=plan.plan_id, report_name=report_name, content=content, case_num=case_num, pass_num=pass_num, fail_num=fail_num, error_num=error_num, pic_name="", skip_num=skip_num, totalTime=totalTime, startTime=start_time, update_user=username, make=1, report_path=report_path) report.save() if fail_num or error_num: title = plan.plan_name report_id = Report.objects.get(report_name=report_name).report_id send_email(title=title, report_id=report_id) logger.log_info('HttpRunner执行测试计划完成!')
def save_report(request, report_name): if request.method != 'POST': return HttpResponse(json.dumps({'status': 'error'}), content_type='application/json', status=400) params = request.POST x_axis = params.get('x_axis') y_axis = params.get('y_axis') fixed = get_third_table_func(x_axis, y_axis) fixed_name = fixed["name"] fixed_type = fixed["type"] if "fixed[values]" in params: fixed_str = params["fixed[values]"] else: fixed_str = "%s %s" % (params['fixed[start]'], params['fixed[end]']) report = Report(name=report_name, x_axis=x_axis, y_axis=y_axis, fixed=fixed_name, fixed_type=fixed_type, fixed_str=fixed_str) report.save() return HttpResponse(json.dumps({'status': 'ok'}), content_type='application/json', status=200)
def test_plan(env_id, case_id_list, plan="", username="******"): logger.log_info('test_plan------->默认方式执行测试计划中<--------------') start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) begin_time = time.clock() case_num = len(case_id_list) content = [] pass_num = 0 fail_num = 0 error_num = 0 i = 0 for case_id in case_id_list: execute = Test_execute(env_id, case_id_list, case_id=case_id, run_mode="0") case_result = execute.test_case content.append(case_result) end_time = time.clock() totalTime = str(end_time - begin_time)[:6] + ' s' for step in content: for s in step['step_list']: if s["result"] == "pass": pass_num += 1 i += 1 s['id'] = i if s["result"] == "fail": fail_num += 1 i += 1 s['id'] = i if s["result"] == "error": error_num += 1 i += 1 s['id'] = i pic_name = DrawPie(pass_num, fail_num, error_num) report_name = plan.plan_name + "-" + str(start_time) report = Report(plan_id=plan.plan_id, report_name=report_name, content=content, case_num=case_num, pass_num=pass_num, fail_num=fail_num, error_num=error_num, pic_name=pic_name, totalTime=totalTime, startTime=start_time, update_user='******', make=0) report.save() Plan.objects.filter(plan_id=plan.plan_id).update( update_user=username, update_time=datetime.now()) if fail_num or error_num: _to = [] user = User.objects.filter(is_superuser=1).filter(is_staff=1).filter( is_active=1).values() for u in user: _to.append(u['email']) if _to: title = plan.plan_name report_id = Report.objects.get(report_name=report_name).report_id send_email(_to=_to, title=title, report_id=report_id) else: logger.log_warning( '收件人邮箱为空,无法发送邮件!请在 EasyTeat接口测试平台 - 用户管理 模块中设置.') logger.log_info('默认方式测试计划执行完成!')
def test_httprunner(env_id, case_id_list, plan="", username="******"): logger.log_info('test_httprunner------->HttpRunner执行测试计划中<--------------') start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) begin_time = time.clock() case_num = len(case_id_list) content = [] j = 0 execute = Test_execute(env_id, case_id_list, run_mode="1", plan=plan) case_result = execute.test_case report_path = case_result['report_path'] for i in range(len(case_result['summary']['details'])): for records in case_result['summary']['details'][i]['records']: j += 1 records['id'] = j for data in records.get('meta_datas', {}).get('data', {}): body = json.dumps(data.get('request', {}).get('body', {}), ensure_ascii=False).replace('Markup', '').replace( '"', '') if body: import urllib.parse data['request']['body'] = urllib.parse.unquote( body.encode('utf-8').decode('unicode_escape').encode( 'utf-8').decode('unicode_escape')) content.append(case_result) summary = case_result.get('summary', {}) stat = summary.get('stat', {}).get('teststeps', {}) pass_num = stat.get('successes', 0) fail_num = stat.get('failures', 0) error_num = stat.get('errors', 0) end_time = time.clock() totalTime = str(end_time - begin_time)[:6] + ' s' pic_name = DrawPie(pass_num, fail_num, error_num) report_name = plan.plan_name + "-" + str(start_time).replace(':', '-') report = Report(plan_id=plan.plan_id, report_name=report_name, content=content, case_num=case_num, pass_num=pass_num, fail_num=fail_num, error_num=error_num, pic_name=pic_name, totalTime=totalTime, startTime=start_time, update_user=username, make=1, report_path=report_path) report.save() if fail_num or error_num: _to = [] user = User.objects.filter(is_superuser=1).filter(is_staff=1).filter( is_active=1).values() for u in user: _to.append(u['email']) if _to: title = plan.plan_name report_id = Report.objects.get(report_name=report_name).report_id send_email(_to=_to, title=title, report_id=report_id) else: logger.log_warning( '收件人邮箱为空,无法发送邮件!请在 EasyTeat接口测试平台 - 用户管理 模块中设置.') logger.log_info('HttpRunner执行测试计划完成!')
def test_plan(env_id, case_id_list, plan="", username="******"): logger.log_info('test_plan------->默认方式执行测试计划中<--------------') start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) begin_time = time.clock() case_num = len(case_id_list) content = [] pass_num = 0 fail_num = 0 error_num = 0 skip_num = 0 i = 0 for case_id in case_id_list: execute = Test_execute(env_id, case_id_list, case_id=case_id, run_mode="0") case_result = execute.test_case if "error" in case_result.keys(): logger.log_error(case_result["msg"]) return content.append(case_result) end_time = time.clock() totalTime = str(end_time - begin_time)[:6] + ' s' for step in content: for s in step['step_list']: if s["result"] == "pass": pass_num += 1 i += 1 s['id'] = i if s["result"] == "fail": fail_num += 1 i += 1 s['id'] = i if s["result"] == "error": error_num += 1 i += 1 s['id'] = i if s["result"] == "skipped": skip_num += 1 i += 1 s['id'] = i # pic_name = DrawPie(pass_num, fail_num, error_num, skip_num) report_name = plan.plan_name + "-" + str(start_time) report = Report(plan_id=plan.plan_id, report_name=report_name, content=content, case_num=case_num, pass_num=pass_num, fail_num=fail_num, error_num=error_num, pic_name="", totalTime=totalTime, startTime=start_time, update_user=username, make=0, skip_num=skip_num) report.save() Plan.objects.filter(plan_id=plan.plan_id).update( update_user=username, update_time=datetime.now()) if fail_num or error_num: title = plan.plan_name report_id = Report.objects.get(report_name=report_name).report_id send_email(title=title, report_id=report_id) logger.log_info('默认方式测试计划执行完成!')
def run_plan(*args, **kwargs): logger.log_info('run plan------->执行定时任务中<------{}----{}----'.format( args, kwargs)) start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) if not args and not kwargs: logger.log_error('查询定时任务计划为空!') return if "data" in str(kwargs): task_name = eval(kwargs["data"])["task_name"] task_id = eval(kwargs["data"])["task_id"] else: task_name = kwargs["task_name"] task_id = kwargs["task_id"] if "[" in str(args): args = eval(args[0]) begin_time = time.clock() content = [] report_path = [] pass_num = 0 fail_num = 0 error_num = 0 skip_num = 0 j = 0 case_num = len(args) for plan_id in args: try: plan = Plan.objects.get(plan_id=plan_id) except Plan.DoesNotExist: logger.log_error('设置定时任务的计划不存在!') return env_id = plan.environment_id case_id_list = eval(plan.content) begin_time = time.clock() execute = Test_execute(env_id, case_id_list, run_mode="1", plan=plan) case_result = execute.test_case if "error" in case_result.keys(): logger.log_error(case_result["msg"]) return report_path.append(case_result['report_path']) for i in range(len(case_result['summary']['details'])): for records in case_result['summary']['details'][i]['records']: j += 1 records['id'] = j for data in records.get('meta_datas', {}).get('data', {}): body = json.dumps(data.get('request', {}).get('body', {}), ensure_ascii=False).replace( 'Markup', '').replace('"', '') if body: import urllib.parse data['request']['body'] = urllib.parse.unquote( body.encode('utf-8').decode('unicode_escape'). encode('utf-8').decode('unicode_escape')) content.append(case_result) summary = case_result.get('summary', {}) stat = summary.get('stat', {}).get('teststeps', {}) pass_num += stat.get('successes', 0) fail_num += stat.get('failures', 0) error_num += stat.get('errors', 0) skip_num += stat.get('skipped', 0) end_time = time.clock() totalTime = str(end_time - begin_time)[:6] + ' s' # pic_name = DrawPie(pass_num, fail_num, error_num, skip_num) report_name = task_name + "-" + str(start_time).replace(':', '-') report = Report(plan_id="", report_name=report_name, content=content, case_num=case_num, pass_num=pass_num, fail_num=fail_num, error_num=error_num, pic_name="", totalTime=totalTime, skip_num=skip_num, startTime=start_time, update_user="******", make=1, report_path=report_path, task_id=task_id) report.save() if fail_num or error_num: title = task_name report_id = Report.objects.get(report_name=report_name).report_id send_email(title=title, report_id=report_id) logger.log_info('HttpRunner执行定时任务完成!{}--{}--'.format(args, kwargs))
def run_plan(): log.info('run plan------->执行测试计划中<--------------') start_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) plan = Plan.objects.filter(is_task=1).all().values() if plan == None: log.error('查询定时任务计划为空!') return plan_id = plan[0]['plan_id'] env_id = plan[0]['environment_id'] case_id_list = eval(plan[0]['content']) begin_time = time.clock() case_num = len(case_id_list) content = [] pass_num = 0 fail_num = 0 error_num = 0 i = 0 for case_id in case_id_list: execute = Test_execute(env_id, case_id_list, case_id=case_id) case_result = execute.test_case content.append(case_result) end_time = time.clock() totalTime = str(end_time - begin_time)[:6] + ' s' for step in content: for s in step['step_list']: if s["result"] == "pass": pass_num += 1 i += 1 s['id'] = i if s["result"] == "fail": fail_num += 1 i += 1 s['id'] = i if s["result"] == "error": error_num += 1 i += 1 s['id'] = i pic_name = DrawPie(pass_num, fail_num, error_num) report_name = plan[0]['plan_name'] + "-" + str(start_time) report = Report(plan_id=plan_id, report_name=report_name, content=content, case_num=case_num, pass_num=pass_num, fail_num=fail_num, error_num=error_num, pic_name=pic_name, totalTime=totalTime, startTime=start_time, update_user='******', make=0) report.save() if fail_num or error_num: _to = [] user = User.objects.filter(is_superuser=1).filter(is_staff=1).filter( is_active=1).values() for u in user: _to.append(u['email']) if _to: title = plan[0]['plan_name'] report_id = Report.objects.get(report_name=report_name).report_id send_email(_to=_to, title=title, report_id=report_id) else: log.warning('收件人邮箱为空,无法发送邮件!请在 EasyTeat接口测试平台 - 用户管理 模块中设置.') log.info('测试任务执行完成!')