def module_hrun(name, base_url, module, receiver): """ 异步运行模块 :param env_name: str: 环境地址 :param project: str:项目所属模块 :param module: str:模块名称 :return: """ logger.setup_logger('INFO') kwargs = { "failfast": False, } runner = HttpRunner(**kwargs) module = list(module) testcase_dir_path = os.path.join(os.getcwd(), "suite") testcase_dir_path = os.path.join(testcase_dir_path, get_time_stamp()) try: for value in module: run_by_module(value[0], base_url, testcase_dir_path) except ObjectDoesNotExist: return '找不到模块信息' runner.run(testcase_dir_path) shutil.rmtree(testcase_dir_path) runner._summary = timestamp_to_datetime(runner._summary) report_path = add_test_reports(runner, report_name=name) if receiver != '': send_email_reports(receiver, report_path) os.remove(report_path)
def project_hrun(name, base_url, project, receiver): """ 异步运行整个项目 :param env_name: str: 环境地址 :param project: str :return: """ logger.setup_logger('INFO') kwargs = { "failfast": False, } runner = HttpRunner(**kwargs) id = ProjectInfo.objects.get(project_name=project).id testcase_dir_path = os.path.join(os.getcwd(), "suite") testcase_dir_path = os.path.join(testcase_dir_path, get_time_stamp()) run_by_project(id, base_url, testcase_dir_path) runner.run(testcase_dir_path) shutil.rmtree(testcase_dir_path) runner._summary = timestamp_to_datetime(runner._summary) report_path = add_test_reports(runner, report_name=name) if receiver != '': send_email_reports(receiver, report_path) os.remove(report_path)
def run_test(request): """ 运行用例 :param request: :return: """ kwargs = { "failfast": False, } runner = HttpRunner(**kwargs) testcase_dir_path = os.path.join(settings.BASE_DIR, "suite") testcase_dir_path = os.path.join(testcase_dir_path, get_time_stamp()) account = request.session["now_account"] if request.is_ajax(): kwargs = json.loads(request.body.decode('utf-8')) id = kwargs.pop('id') suite_name = TestSuite.objects.get(id=id).suite_name cfg_id = kwargs.pop('cfg_id') base_url = kwargs.pop('env_name') type = kwargs.pop('type') run_test_by_type(id, base_url, testcase_dir_path, type, cfg_id) report_name = kwargs.get('report_name', None) request.session["base_url"] = base_url request.session["cfg_id"] = cfg_id main_hrun.delay(testcase_dir_path, report_name) return HttpResponse('用例执行中,请稍后查看报告即可,默认时间戳命名报告') else: id = request.POST.get('id') base_url = request.POST.get('env_name') type = request.POST.get('type', 'test') cfg_id = request.POST.get('cfg_id') request.session["base_url"] = base_url request.session["cfg_id"] = cfg_id run_test_by_type(id, base_url, testcase_dir_path, type, cfg_id) if type == "suite": suite_name = TestSuite.objects.get(id=id).suite_name runner.run( os.path.join(testcase_dir_path, "{}.yml".format(suite_name))) else: runner.run(testcase_dir_path) #删除yml文件,异常的时候不删除,可以用来debug shutil.rmtree(testcase_dir_path) runner._summary = timestamp_to_datetime(runner.summary, type=False) #data = request.body.decode('utf-8') # kwargs = json.loads(request.body.decode('utf-8')) #report_name = kwargs.get('report_name', None) report_name = get_test_name(id, type) report_id = add_test_reports(runner, account, report_name) if request.POST.get('account') == "Jenkins": result = download_report(request, report_id) return result else: runner.summary.setdefault("report_id", report_id) return render_to_response('report_template.html', runner.summary)
def main_hrun(testset_path, report_name): """ 用例运行 :param testset_path: dict or list :param report_name: str :return: """ logger.setup_logger('INFO') kwargs = { "failfast": False, } runner = HttpRunner(**kwargs) runner.run(testset_path) shutil.rmtree(testset_path) runner._summary = timestamp_to_datetime(runner._summary) report_path = add_test_reports(runner, report_name=report_name) os.remove(report_path)
def run_batch_test(request): """ 批量运行用例 :param request: :return: """ kwargs = { "failfast": False, } runner = HttpRunner(**kwargs) testcase_dir_path = os.path.join(os.getcwd(), "suite") testcase_dir_path = os.path.join(testcase_dir_path, get_time_stamp()) if request.is_ajax(): kwargs = json.loads(request.body.decode('utf-8')) test_list = kwargs.pop('id') base_url = kwargs.pop('env_name') type = kwargs.pop('type') report_name = kwargs.get('report_name', None) run_by_batch(test_list, base_url, testcase_dir_path, type=type) main_hrun.delay(testcase_dir_path, report_name) return HttpResponse('用例执行中,请稍后查看报告即可,默认时间戳命名报告') else: type = request.POST.get('type', None) base_url = request.POST.get('env_name') test_list = request.body.decode('utf-8').split('&') if type: run_by_batch(test_list, base_url, testcase_dir_path, type=type, mode=True) else: run_by_batch(test_list, base_url, testcase_dir_path) runner.run(testcase_dir_path) shutil.rmtree(testcase_dir_path) runner._summary = timestamp_to_datetime(runner._summary, type=False) return render_to_response('report_template.html', runner._summary)
def run_test(request): """ 运行用例 :param request: :return: """ kwargs = { "failfast": False, } runner = HttpRunner(**kwargs) testcase_dir_path = os.path.join(os.getcwd(), "suite") testcase_dir_path = os.path.join(testcase_dir_path, get_time_stamp()) if request.is_ajax(): kwargs = json.loads(request.body.decode('utf-8')) id = kwargs.pop('id') base_url = kwargs.pop('env_name') type = kwargs.pop('type') run_test_by_type(id, base_url, testcase_dir_path, type) report_name = kwargs.get('report_name', None) main_hrun.delay(testcase_dir_path, report_name) return HttpResponse('用例执行中,请稍后查看报告即可,默认时间戳命名报告') else: id = request.POST.get('id') base_url = request.POST.get('env_name') type = request.POST.get('type', 'test') testcase_dir_path1 = os.path.join(testcase_dir_path, 'guest项目') run_test_by_type(id, base_url, testcase_dir_path, type) runner.run(testcase_dir_path1) # shutil.rmtree(testcase_dir_path) # 递归删除所有文件 runner._summary = timestamp_to_datetime(runner._summary, base_url, type=False) with open('summary.json', 'w', encoding='utf-8') as f: json.dump(runner._summary, f, ensure_ascii=False, sort_keys=True, indent=4) return render_to_response('report_template.html', runner._summary)