def addtask(id):#定时任务执行的时候所用的函数 in_id=int(id) task=Task.query.filter_by(id=in_id).first() starttime = datetime.datetime.now() star = time.time() day = time.strftime("%Y%m%d%H%M", time.localtime(time.time())) basedir = os.path.abspath(os.path.dirname(__file__)) file_dir = os.path.join(basedir, 'upload') file = os.path.join(file_dir, (day + '.log')) if os.path.exists(file) is False: os.system('touch %s' % file) filepath = os.path.join(file_dir, (day + '.html')) if os.path.exists(filepath) is False: os.system(r'touch %s' % filepath) projecct_list = [] model_list = [] Interface_name_list = [] Interface_url_list = [] Interface_meth_list = [] Interface_pase_list = [] Interface_assert_list = [] Interface_headers_list = [] id_list = [] for task_yongli in task.interface.all(): id_list.append(task_yongli.id) projecct_list.append(task_yongli.projects) model_list.append(task_yongli.models) Interface_url_list.append(task_yongli.Interface_url) Interface_name_list.append(task_yongli.Interface_name) Interface_meth_list.append(task_yongli.Interface_meth) Interface_pase_list.append(task_yongli.Interface_pase) Interface_assert_list.append(task_yongli.Interface_assert) Interface_headers_list.append(task_yongli.Interface_headers) apitest = ApiTestCase(Interface_url_list, Interface_meth_list, Interface_pase_list, Interface_assert_list, file, Interface_headers_list) result_toal, result_pass, result_fail, relusts, bask_list = apitest.testapi() endtime = datetime.datetime.now() end = time.time() createHtml(titles=u'接口测试报告', filepath=filepath, starttime=starttime, endtime=endtime, passge=result_pass, fail=result_fail, id=id_list, name=projecct_list, headers=Interface_headers_list, coneent=Interface_url_list, url=Interface_meth_list, meth=Interface_pase_list, yuqi=Interface_assert_list, json=bask_list, relusts=relusts) hour = end - star user_id = User.query.filter_by(role_id=2).first().id new_reust = TestResult(Test_user_id=user_id, test_num=result_toal, pass_num=result_pass, fail_num=result_fail, test_time=starttime, hour_time=hour, test_rep=(day + '.html'), test_log=(day + '.log')) email = EmailReport.query.filter_by(role_id=2, default_set=True).first() send_emails(sender=email.send_email, receivers=task.taskrepor_to, password=email.send_email_password, smtp=email.stmp_email, port=email.port, fujian1=file, fujian2=filepath, subject=u'%s自动用例执行测试报告' % day, url='%stest_rep'%(request.url_root)) db.session.add(new_reust) db.session.commit()
def addtask(id):#定时任务执行的时候所用的函数 in_id=int(id) task=Task.query.filter_by(id=in_id).first() starttime = datetime.datetime.now() star = time.time() day = time.strftime("%Y%m%d%H%M", time.localtime(time.time())) basedir = os.path.abspath(os.path.dirname(__file__)) file_dir = os.path.join(basedir, 'upload') file = os.path.join(file_dir, (day + '.log')) if os.path.exists(file) is False: os.system('touch %s' % file) filepath = os.path.join(file_dir, (day + '.html')) if os.path.exists(filepath) is False: os.system(r'touch %s' % filepath) projecct_list = [] model_list = [] Interface_name_list = [] Interface_url_list = [] Interface_meth_list = [] Interface_pase_list = [] Interface_assert_list = [] Interface_headers_list = [] id_list = [] for task_yongli in task.interface.all(): id_list.append(task_yongli.id) projecct_list.append(task_yongli.projects) model_list.append(task_yongli.models) Interface_url_list.append(task_yongli.Interface_url) Interface_name_list.append(task_yongli.Interface_name) Interface_meth_list.append(task_yongli.Interface_meth) Interface_pase_list.append(task_yongli.Interface_pase) Interface_assert_list.append(task_yongli.Interface_assert) Interface_headers_list.append(task_yongli.Interface_headers) apitest = ApiTestCase(Interface_url_list, Interface_meth_list, Interface_pase_list, Interface_assert_list, file, Interface_headers_list) result_toal, result_pass, result_fail, relusts, bask_list = apitest.testapi() endtime = datetime.datetime.now() end = time.time() createHtml(titles=u'接口测试报告', filepath=filepath, starttime=starttime, endtime=endtime, passge=result_pass, fail=result_fail, id=id_list, name=projecct_list, headers=Interface_headers_list, coneent=Interface_url_list, url=Interface_meth_list, meth=Interface_pase_list, yuqi=Interface_assert_list, json=bask_list, relusts=relusts) hour = end - star user_id = User.query.filter_by(role_id=2).first().id new_reust = TestResult(Test_user_id=user_id, test_num=result_toal, pass_num=result_pass, fail_num=result_fail, test_time=starttime, hour_time=hour, test_rep=(day + '.html'), test_log=(day + '.log')) db.session.add(new_reust) db.session.commit() send_ding(content="%s定时任务执行完毕,测试时间:%s,\\n 通过用例:%s,失败用例:%s,\\n,详情见测试平台测试报告!" % ( task.taskname, starttime, result_pass, result_fail))
def dispatch_request(self): next = request.headers.get('Referer') starttime = datetime.datetime.now() star = time.time() day = time.strftime("%Y%m%d%H%M", time.localtime(time.time())) pad = os.getcwd() file_dir = pad + '\\app\\upload' file = os.path.join(file_dir, (day + '.log')) if os.path.exists(file) is False: os.system('touch %s' % file) filepath = os.path.join(file_dir, (day + '.html')) if os.path.exists(filepath) is False: os.system(r'touch %s' % filepath) if request.method == 'POST': f_dingding = request.form.get('dingding') me = request.form.getlist('yongli') testurl = request.form.get('urltest') if len(me) <= 1: flash(u'请选择一个以上的用例来执行') return redirect(next or url_for('yongli')) if testurl is None: flash(u'请选择测试环境') return redirect(next or url_for('yongli')) projecct_list = [] model_list = [] Interface_name_list = [] Interface_url_list = [] Interface_meth_list = [] Interface_pase_list = [] Interface_assert_list = [] Interface_headers_list = [] Interface_pid_list = [] Interface_yilai_list = [] Interface_save_list = [] Interface_is_data_list = [] Interface_mysql_list = [] Interface_msyql_ziduan_list = [] id_list = [] for case in me: case_one = InterfaceTest.query.filter_by(id=case).first() Interface_is_data_list.append(case_one.is_database) Interface_mysql_list.append(case_one.chaxunshujuku) Interface_msyql_ziduan_list.append(case_one.databaseziduan) id_list.append(case_one.id) projecct_list.append(case_one.projects) model_list.append(case_one.models) Interface_url_list.append(case_one.Interface_url) Interface_name_list.append(case_one.Interface_name) Interface_meth_list.append(case_one.Interface_meth) Interface_pase_list.append(case_one.Interface_pase) Interface_assert_list.append(case_one.Interface_assert) Interface_headers_list.append(case_one.Interface_headers) Interface_pid_list.append(case_one.pid) Interface_yilai_list.append(case_one.getattr_p) Interface_save_list.append(case_one.saveresult) if (len(set(projecct_list))) > 1: flash('目前单次只能执行一个项目') return redirect(next or url_for('duoyongli')) testevent = Interfacehuan.query.filter_by(url=testurl).first() try: apitest = ApiTestCase(inteface_url=Interface_url_list, inteface_meth=Interface_meth_list, inteface_parm=Interface_pase_list, inteface_assert=Interface_assert_list, file=file, headers=Interface_headers_list, pid=Interface_pid_list, yilaidata=Interface_yilai_list, saveresult=Interface_save_list, id_list=id_list, is_database=Interface_is_data_list, data_mysql=Interface_mysql_list, data_ziduan=Interface_msyql_ziduan_list, urltest=testevent) result_toal, result_pass, result_fail, relusts, bask_list, result_cashu, result_wei, result_except = apitest.testapi( ) endtime = datetime.datetime.now() end = time.time() createHtml(titles=u'接口测试报告', filepath=filepath, starttime=starttime, endtime=endtime, passge=result_pass, fail=result_fail, id=id_list, name=projecct_list, headers=Interface_headers_list, coneent=Interface_url_list, url=Interface_meth_list, meth=Interface_pase_list, yuqi=Interface_assert_list, json=bask_list, relusts=relusts, excepts=result_except, yuqis=result_cashu, weizhi=result_wei) hour = end - star user_id = current_user.id new_reust = TestResult(Test_user_id=user_id, test_num=result_toal, pass_num=result_pass, fail_num=result_fail, test_time=starttime, hour_time=hour, test_rep=(day + '.html'), test_log=(day + '.log'), Exception_num=result_except, can_num=result_cashu, wei_num=result_wei, projects_id=projecct_list[0].id) db.session.add(new_reust) db.session.commit() if f_dingding == 'email': email = EmailReport.query.filter_by( email_re_user_id=int(current_user.id), default_set=True).first() if email: m = send_emails(sender=email.send_email, receivers=email.to_email, password=email.send_email_password, smtp=email.stmp_email, port=email.port, fujian1=file, fujian2=filepath, subject=u'%s用例执行测试报告' % day, url='http://127.0.0.1:5000/test_rep') if m == False: flash(u'发送邮件失败,请检查您默认的邮件设置是否正确') return redirect(url_for('home.test_rep')) flash(u'测试已经完成,并且给您默认设置发送了测试报告') return redirect(url_for('home.test_rep')) flash(u'无法完成,需要去您的个人设置去设置一个默认的邮件发送') return redirect(url_for('home.yongli')) if f_dingding == 'dingding': send = send_ding( content="多用例测试已经完成,通过用例:%s,失败用例:%s,详情见测试报告" % (result_pass, result_fail)) if send is True: flash(u'测试报告已经发送钉钉讨论群,测试报告已经生成!') return redirect(url_for('home.yongli')) flash(u'测试报告发送钉钉讨论群失败!请检查相关配置!') return redirect(next or url_for('home.yongli')) flash(u'测试已经完成,测试报告已经生成') return redirect(url_for('home.test_rep')) except Exception as e: print(e) flash(u'测试失败,请检查您的测试用例单个执行是否出错') return redirect(next or url_for('home.yongli')) return redirect(url_for('home.yongli'))
def dispatch_request(self): if not session.get('username'): return redirect(url_for('login')) starttime = datetime.datetime.now() star = time.time() day = time.strftime("%Y%m%d%H%M", time.localtime(time.time())) basedir = os.path.abspath(os.path.dirname(__file__)) file_dir = os.path.join(basedir, 'upload') file = os.path.join(file_dir, (day + '.log')) if os.path.exists(file) is False: os.system('touch %s' % file) filepath = os.path.join(file_dir, (day + '.html')) if os.path.exists(filepath) is False: os.system(r'touch %s' % filepath) if request.method == 'POST': me = request.form.getlist('yongli') if len(me) <= 1: flash(u'请选择一个以上的用例来执行') return redirect(url_for('yongli')) projecct_list = [] model_list = [] Interface_name_list = [] Interface_url_list = [] Interface_meth_list = [] Interface_pase_list = [] Interface_assert_list = [] id_list = [] for case in me: case_one = InterfaceTest.query.filter_by(id=case).first() id_list.append(case_one.id) projecct_list.append(case_one.projects) model_list.append(case_one.models) Interface_url_list.append(case_one.Interface_url) Interface_name_list.append(case_one.Interface_name) Interface_meth_list.append(case_one.Interface_meth) Interface_pase_list.append(case_one.Interface_pase) Interface_assert_list.append(case_one.Interface_assert) try: apitest = ApiTestCase(Interface_url_list, Interface_meth_list, Interface_pase_list, Interface_assert_list, file) result_toal, result_pass, result_fail, relusts, bask_list = apitest.testapi( ) endtime = datetime.datetime.now() end = time.time() createHtml(titles=u'接口测试报告', filepath=filepath, starttime=starttime, endtime=endtime, passge=result_pass, fail=result_fail, id=id_list, name=projecct_list, key=model_list, coneent=Interface_url_list, url=Interface_meth_list, meth=Interface_pase_list, yuqi=Interface_assert_list, json=bask_list, relusts=relusts) hour = end - star user_id = User.query.filter_by( username=session.get('username')).first().id new_reust = TestResult(Test_user_id=user_id, test_num=result_toal, pass_num=result_pass, fail_num=result_fail, test_time=starttime, hour_time=hour, test_rep=(day + '.html'), test_log=(day + '.log')) db.session.add(new_reust) db.session.commit() flash(u'测试已经完成') return redirect(url_for('test_rep')) except: flash(u'测试失败,请检查您的测试用例单个执行是否出错') return redirect(url_for('yongli')) return redirect(url_for('yongli'))
def addtask(id):#定时任务执行的时候所用的函数 in_id=int(id) task=Task.query.filter_by(id=in_id).first() starttime = datetime.datetime.now() star = time.time() day = time.strftime("%Y%m%d%H%M", time.localtime(time.time())) basedir = os.path.abspath(os.path.dirname(__file__)) file_dir = os.path.join(basedir, 'upload') file = os.path.join(file_dir, (day + '.log')) if os.path.exists(file) is False: os.system('touch %s' % file) filepath = os.path.join(file_dir, (day + '.html')) if os.path.exists(filepath) is False: os.system(r'touch %s' % filepath) projecct_list = [] model_list = [] pid_list = [] yilaidata_list = [] saveresult_list = [] Interface_name_list = [] Interface_url_list = [] Interface_meth_list = [] Interface_pase_list = [] Interface_assert_list = [] Interface_headers_list = [] id_list = [] for task_yongli in task.interface.all(): id_list.append(task_yongli.id) pid_list.append(task_yongli.pid) projecct_list.append(task_yongli.projects) model_list.append(task_yongli.models) Interface_url_list.append(task_yongli.Interface_url) Interface_name_list.append(task_yongli.Interface_name) Interface_meth_list.append(task_yongli.Interface_meth) Interface_pase_list.append(task_yongli.Interface_pase) Interface_assert_list.append(task_yongli.Interface_assert) Interface_headers_list.append(task_yongli.Interface_headers) yilaidata_list.append(task_yongli.getattr_p) saveresult_list.append(task_yongli.saveresult) apitest = ApiTestCase(Interface_url_list, Interface_meth_list, Interface_pase_list, Interface_assert_list, file, Interface_headers_list,pid_list,yilaidata_list,saveresult_list,id_list) result_toal, result_pass, result_fail, relusts, bask_list, result_cashu, result_wei, result_except = apitest.testapi() endtime = datetime.datetime.now() end = time.time() createHtml(titles=u'接口测试报告', filepath=filepath, starttime=starttime, endtime=endtime, passge=result_pass, fail=result_fail, id=id_list, name=projecct_list, headers=Interface_headers_list, coneent=Interface_url_list, url=Interface_meth_list, meth=Interface_pase_list, yuqi=Interface_assert_list, json=bask_list, relusts=relusts, excepts=result_except, yuqis=result_cashu, weizhi=result_wei) hour = end - star new_reust = TestResult(Test_user_id=task.makeuser, test_num=result_toal, pass_num=result_pass, fail_num=result_fail, test_time=starttime, hour_time=hour, test_rep=(day + '.html'), test_log=(day + '.log'),Exception_num=result_except,can_num=result_cashu, wei_num=result_wei) db.session.add(new_reust) db.session.commit() email = EmailReport.query.filter_by(email_re_user_id=int(task.makeuser),default_set=True).first() if email: m = send_emails(sender=task.task_make_email, receivers=task.taskrepor_to, password=email.send_email_password, smtp=email.stmp_email, port=email.port, fujian1=file, fujian2=filepath, subject=u'%s用例执行测试报告' % day, url='http://127.0.0.1:5000/test_rep') if m == False: print('发送邮件失败,请检查您默认的邮件设置是否正确') print('测试已经完成,并且给您默认设置发送了测试报告') else: print('无法完成,需要去您的个人设置去设置一个默认的邮件发送')
def addtask(id): #定时任务执行的时候所用的函数 in_id = int(id) task = Task.query.filter_by(id=in_id).first() starttime = datetime.datetime.now() star = time.time() day = time.strftime("%Y%m%d%H%M", time.localtime(time.time())) basedir = os.path.abspath(os.path.dirname(__file__)) file_dir = os.path.join(basedir, 'upload') file = os.path.join(file_dir, (day + '.log')) if os.path.exists(file) is False: os.system('touch %s' % file) filepath = os.path.join(file_dir, (day + '.html')) if os.path.exists(filepath) is False: os.system(r'touch %s' % filepath) projecct_list = [] model_list = [] Interface_name_list = [] Interface_url_list = [] Interface_meth_list = [] Interface_pase_list = [] Interface_assert_list = [] Interface_headers_list = [] Interface_pid_list = [] Interface_yilai_list = [] Interface_save_list = [] Interface_is_data_list = [] Interface_mysql_list = [] Interface_msyql_ziduan_list = [] id_list = [] for task_yongli in task.interface.all(): id_list.append(task_yongli.id) projecct_list.append(task_yongli.projects) model_list.append(task_yongli.models) Interface_is_data_list.append(task_yongli.is_database) Interface_mysql_list.append(task_yongli.chaxunshujuku) Interface_msyql_ziduan_list.append(task_yongli.databaseziduan) Interface_url_list.append(task_yongli.Interface_url) Interface_name_list.append(task_yongli.Interface_name) Interface_meth_list.append(task_yongli.Interface_meth) Interface_pase_list.append(task_yongli.Interface_pase) Interface_assert_list.append(task_yongli.Interface_assert) Interface_headers_list.append(task_yongli.Interface_headers) Interface_pid_list.append(task_yongli.pid) Interface_yilai_list.append(task_yongli.getattr_p) Interface_save_list.append(task_yongli.saveresult) testevent = task.testevent apitest = ApiTestCase(inteface_url=Interface_url_list, inteface_meth=Interface_meth_list, inteface_parm=Interface_pase_list, inteface_assert=Interface_assert_list, file=file, headers=Interface_headers_list, pid=Interface_pid_list, is_database=Interface_is_data_list, data_mysql=Interface_mysql_list, data_ziduan=Interface_msyql_ziduan_list, urltest=testevent, yilaidata=Interface_yilai_list, saveresult=Interface_save_list, id_list=id_list) result_toal, result_pass, result_fail, relusts, bask_list, result_cashu, result_wei, result_except = apitest.testapi( ) endtime = datetime.datetime.now() end = time.time() createHtml(titles=u'定时任务接口测试报告', filepath=filepath, starttime=starttime, endtime=endtime, passge=result_pass, fail=result_fail, id=id_list, name=projecct_list, headers=Interface_headers_list, coneent=Interface_url_list, url=Interface_meth_list, meth=Interface_pase_list, yuqi=Interface_assert_list, json=bask_list, relusts=relusts, excepts=result_except, yuqis=result_cashu, weizhi=result_wei) hour = end - star user_id = User.query.filter_by(role_id=2).first().id new_reust = TestResult(Test_user_id=user_id, test_num=result_toal, pass_num=result_pass, fail_num=result_fail, test_time=starttime, hour_time=hour, test_rep=(day + '.html'), test_log=(day + '.log'), Exception_num=result_except, can_num=result_cashu, wei_num=result_wei) db.session.add(new_reust) db.session.commit() try: send_ding( content="%s定时任务执行完毕,测试时间:%s,\\n 通过用例:%s,失败用例:%s,\\n,详情见测试平台测试报告!" % (task.taskname, starttime, result_pass, result_fail)) except Exception as e: flash('定时任务的钉钉消息发送失败!原因:%s' % e)
def dispatch_request(self): starttime = datetime.datetime.now() star = time.time() day = time.strftime("%Y%m%d%H%M", time.localtime(time.time())) basedir = os.path.abspath(os.path.dirname(__file__)) file_dir = os.path.join(basedir, 'upload') file = os.path.join(file_dir, (day + '.log')) if os.path.exists(file) is False: os.system('touch %s' % file) filepath = os.path.join(file_dir, (day + '.html')) if os.path.exists(filepath) is False: os.system(r'touch %s' % filepath) if request.method == 'POST': f_dingding = request.form.get('dingding') me = request.form.getlist('yongli') if len(me) <= 1: flash(u'请选择一个以上的用例来执行') return redirect(url_for('yongli')) projecct_list = [] model_list = [] Interface_name_list = [] Interface_url_list = [] Interface_meth_list = [] Interface_pase_list = [] Interface_assert_list = [] Interface_headers_list = [] id_list = [] for case in me: case_one = InterfaceTest.query.filter_by(id=case).first() id_list.append(case_one.id) projecct_list.append(case_one.projects) model_list.append(case_one.models) Interface_url_list.append(case_one.Interface_url) Interface_name_list.append(case_one.Interface_name) Interface_meth_list.append(case_one.Interface_meth) Interface_pase_list.append(case_one.Interface_pase) Interface_assert_list.append(case_one.Interface_assert) Interface_headers_list.append(case_one.Interface_headers) if (len(set(projecct_list))) > 1: flash('目前单次只能执行一个项目') return redirect(url_for('duoyongli')) try: apitest = ApiTestCase(Interface_url_list, Interface_meth_list, Interface_pase_list, Interface_assert_list, file, Interface_headers_list) result_toal, result_pass, result_fail, relusts, bask_list = apitest.testapi( ) endtime = datetime.datetime.now() end = time.time() createHtml(titles=u'接口测试报告', filepath=filepath, starttime=starttime, endtime=endtime, passge=result_pass, fail=result_fail, id=id_list, name=projecct_list, headers=Interface_headers_list, coneent=Interface_url_list, url=Interface_meth_list, meth=Interface_pase_list, yuqi=Interface_assert_list, json=bask_list, relusts=relusts) hour = end - star user_id = User.query.filter_by( username=session.get('username')).first().id new_reust = TestResult(Test_user_id=user_id, test_num=result_toal, pass_num=result_pass, fail_num=result_fail, test_time=starttime, hour_time=hour, test_rep=(day + '.html'), test_log=(day + '.log')) db.session.add(new_reust) db.session.commit() if f_dingding == 'email': email = EmailReport.query.filter_by( email_re_user_id=int(current_user.id), default_set=True).first() if email: m = send_emails(sender=email.send_email, receivers=email.to_email, password=email.send_email_password, smtp=email.stmp_email, port=email.port, fujian1=file, fujian2=filepath, subject=u'%s用例执行测试报告' % day, url='http://127.0.0.1:5000/test_rep') if m == False: flash(u'发送邮件失败,请检查您默认的邮件设置是否正确') return redirect(url_for('home.test_rep')) flash(u'测试已经完成,并且给您默认设置发送了测试报告') return redirect(url_for('home.test_rep')) flash(u'无法完成,需要去您的个人设置去设置一个默认的邮件发送') return redirect(url_for('home.yongli')) if f_dingding == 'dingding': send = send_ding( content="多用例测试已经完成,通过用例:%s,失败用例:%s,详情见测试报告" % (result_pass, result_fail)) if send is True: flash(u'测试报告已经发送钉钉讨论群,测试报告已经生成!') return redirect(url_for('home.yongli')) flash(u'测试报告发送钉钉讨论群失败!请检查相关配置!') return redirect(url_for('home.yongli')) flash(u'测试已经完成,测试报告已经生成') return redirect(url_for('home.test_rep')) except Exception as e: print(e) flash(u'测试失败,请检查您的测试用例单个执行是否出错') return redirect(url_for('home.yongli')) return redirect(url_for('home.yongli'))