Ejemplo n.º 1
0
 def initdata(args):
     test_cases=[]#总接口用例列表
     case_counts=0
     sqlfield=[]
     log.info('执行接口数:%s'%args.interfaces.all().count())
     for inter in args.interfaces.all():
         cl={}#单个接口用例字典
         d={}#单用例字典
         cdl=[]#单个接口用例列表
         
         d.update(inter.to_dict())
         d.update(env.to_dict())
         cases=inter.case.all()
         case_counts +=cases.count()
         for case in cases:
             #筛选要执行的用例数据
             if case.exec_flag==1:
                 assert_list=[]#单个用例断言列表
                 cd=case.to_dict()
                 asserts=case.caseid.all()
                 for a in asserts:
                     assert_list.append(a.to_dict())
                 
                 cd.update(d)
                 cd['assert_list']=assert_list
                 cdl.append(cd)
         if cdl:
             cl['inter']=str(inter.module)
             cl['cases']=cdl
             test_cases.append(cl)
         else:
             #log.warning('%s接口无用例执行'%inter.module)
             pass
             
     log.info('执行总用例数:%s'%case_counts)
     log.info('*************************')
     log.info('总用例数据:%s'%test_cases)
     return test_cases
Ejemplo n.º 2
0
 def action(self,id):
     global log_file,log_p
     import time,os,sys
     log_p=1
     CURRENT_PATH=os.path.abspath(os.path.dirname(__file__))+os.path.sep+".."+os.path.sep
     sys.path.append(CURRENT_PATH)
     from project_autotest import log
     from project_autotest.main import ews_interface_test
     log_file=CURRENT_PATH+'project_autotest'+os.path.sep+'logs'+os.path.sep+time.strftime("%Y%m%d%H%M%S",time.localtime())+'.log'
     log.set_logger(log_file)
     try:
         t=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())      
         obj=functions.get_object('ews_task',id)
         obj.flag=True
         obj.counts +=1
         obj.last_date=t
         obj.save()
         log.info('*************************')
         log.info('执行任务:%s'%obj.task)
         env=functions.get_object('test_environ',obj.environ)
         log.info('测试环境:%s'%env.environ)
         log.info('运行时间:%s'%t)
         if request.user:
             log.info('执行者:%s'%request.user.username)
             obj.last_user=request.user.username
         else:
             obj.last_user='******'
         obj.save()
             
         def initdata(args):
             test_cases=[]#总接口用例列表
             case_counts=0
             sqlfield=[]
             log.info('执行接口数:%s'%args.interfaces.all().count())
             for inter in args.interfaces.all():
                 cl={}#单个接口用例字典
                 d={}#单用例字典
                 cdl=[]#单个接口用例列表
                 
                 d.update(inter.to_dict())
                 d.update(env.to_dict())
                 cases=inter.case.all()
                 case_counts +=cases.count()
                 for case in cases:
                     #筛选要执行的用例数据
                     if case.exec_flag==1:
                         assert_list=[]#单个用例断言列表
                         cd=case.to_dict()
                         asserts=case.caseid.all()
                         for a in asserts:
                             assert_list.append(a.to_dict())
                         
                         cd.update(d)
                         cd['assert_list']=assert_list
                         cdl.append(cd)
                 if cdl:
                     cl['inter']=str(inter.module)
                     cl['cases']=cdl
                     test_cases.append(cl)
                 else:
                     #log.warning('%s接口无用例执行'%inter.module)
                     pass
                     
             log.info('执行总用例数:%s'%case_counts)
             log.info('*************************')
             log.info('总用例数据:%s'%test_cases)
             return test_cases
         
         def runtask(obj):
             caseslist=initdata(obj)
             report,result=ews_interface_test.main(log,caseslist)
             return report,result
              
         report,result=runtask(obj)
         passes = result.success_count
         fails = result.failure_count+result.error_count
         try:
             #成功率
             rate=result.success_count*100/(result.success_count+result.failure_count+result.error_count)
         except:
             rate=0
         rs = '/logs/'+os.path.basename(log_file)
         rt = '/report/ews/'+os.path.basename(report)
         if request.user and obj.send_mail==True:
             mail_to_list=[]
             email=request.user.email
             mail_to_list.append(email)
             tasklog=self.tasklog(task=obj.task,env=env.environ,passes=passes,fails=fails,rate=rate,begin_time=t,end_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),run_user=request.user.username,result=rs,report=rt,flag='成功')
             log.info(u'发送测试报告')
             ews_interface_test.sendmail(report,mail_to_list)
         else:
             tasklog=self.tasklog(task=obj.task,env=env.environ,passes=passes,fails=fails,rate=rate,begin_time=t,end_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),run_user='******',result=rs,report=rt,flag='成功')  
         end_time=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
         log.info('^^^^^%s Test end!^^^^^'%end_time)
         tasklog.save()
         obj.flag=False
         obj.save()
         return json({'success':True, 'message':'任务执行成功,稍后邮件通知您测试结果,也可以通过本页面查看测试结果和报告'})
     except Exception,e:
         log.error(e)
         rs = '/logs/'+os.path.basename(log_file)
         if request.user:
             user=request.user.username
         else:
             user='******'
         tasklog=self.tasklog(task=obj.task,env=env.environ,passes='0',fails='0',rate='0',begin_time=t,end_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),run_user=user,result=rs,report='',flag='失败')
         tasklog.save()
         obj.flag=False
         obj.save()
         return json({'success':False,'message':'启动任务失败,请联系管理员'})