def init_sys_argv(arg_string): """初始化参数传递 参数列表表达式:file=test.xlsx;case_line_list=1,2,3,4,5,8-11,14;report_path=../reports;conf=test;[email protected],[email protected]; file: 1/默认不传,那么就是运行Testcases目录下的所有用例excel文件,执行run_all. 2/传递目录,执行传递目录的所有Excel,即调用run_all.使用os判断参数是否为目录 3/传递多个文件用,间隔,生成list并执行run_suite. 4/传递一个Excel那么执行run_cases.使用os判断是否为有效文件. case_line_list: 当参数1 file到分支4时,此参数才有意义,不然可传''/null/0. 1/默认不传,或者传''/null/0. 2/传递多个line值用,间隔.例如: 1,3,5,6,7,10-15,18, 10-15表示10,11,12,13,14,15行 3/只传递一个值就执行单一用例.例如:4 就是只执行第四行用例 report_path: 报告存放路径 1/不传就使用默认参数存入到reports目录下 2/传递路径如果非法不存在此目录提示用户并终止执行. conf: 1/不传默认使用test.conf配置. 2/可传递参数时test dev stage,对应加载相应的conf文件. """ param_dict = {} param_dict['protocol'] = 'DUBBO' param_dict['file'] = 'Testcases/' param_dict['report_path'] = 'reports' param_dict['conf'] = 'test' param_dict['loglevel'] = 'INFO' param_dict['logfile'] = 'test.log' param_dict['is_send_mail'] = 'NO' param_dict['restart'] = 'YES' if arg_string != "": params_str = arg_string #sys.argv[1] # params_str = "file=test.xlsx;case_line_list=1,2,3,4,5,8-11,14;report_path=../reports;conf=test;[email protected],[email protected];" params_list = params_str.split("--") for param in params_list: if param != '': if 'report_filename=' in param or 'confpath=' in param or 'protocol=' in param or 'file=' in param or 'case_line_list=' in param or 'report_path=' in param or 'conf=' in param or 'is_send_mail=' in param or 'mail_list=' in param or 'loglevel=' in param or 'logfile=' in param or 'restart=' in param: param_list = param.split("=") if len(param_list) == 2: param_dict[param_list[0]] = param_list[1] else: print UsualTools().get_current_time( ) + u"=>run.py: init_sys_argv: 参数格式错误,%s未发现=号。参数示例:file=test.xlsx--case_line_list=1,2,3,4,5,8-11,14--report_path=../reports--conf=test" % param sys.exit(0) else: print UsualTools().get_current_time( ) + u"=>run.py: init_sys_argv: 详细参数错误,只能是report_filename/file/case_line_list/report_path/conf/email/loglevel/logfile/restart。示例:参数示例:file=test.xlsx--case_line_list=1,2,3,4,5,8-11,14--report_path=../reports--conf=test--restart=NO" sys.exit(0) else: print UsualTools().get_current_time( ) + u"=>run.py: init_sys_argv: 参数为空,使用默认值。" return param_dict
def process_init_param_dict(param_dict): return_param_dict = {} # process file try: proto = param_dict['protocol'] #print u"run.py: process_init_param_dict: %s" % file if proto == "DUBBO" or proto == "HTTP": return_param_dict['protocol'] = proto else: print UsualTools().get_current_time( ) + u"=>run.py: process_init_param_dict: protocol参数只能是DUBBO/HTTP!" sys.exit(0) except Exception, e: print UsualTools().get_current_time( ) + u"=>run.py: process_init_param_dict: 没有参数protocol,设置为默认值[DUBBO]。" return_param_dict['protocol'] = "DUBBO"
def run_suites(file_list=["Testcases/test.xlsx"], para_dict={}): """执行单个xlsx文件,并生成测试报告到reports目录下。 :param file_list: :param report_path: :param conf: :return: """ confpath = para_dict['confpath'] for i in range(0, len(file_list)): filepath = file_list[i] # 获取文件路径 通过参数传递或者Config默认配置 if os.path.isfile(filepath): logging.info(UsualTools().get_current_time() + u"=>run.py: run_suites: 开始执行用例文件%s".encode("utf8") % filepath) run_cases(filepath, para_dict) else: logging.info(UsualTools().get_current_time() + u"=>run.py: run_suites: 用例文件%s不存在,此套件测试终止。" % filepath)
def run_http_all(filepath="TestcasesForHttp/", para_dict={}): """执行所有Testcases/目录下的所有xlsx文件,并生成测试报告到reports目录下。 :param filepath: :param report_path: :param conf: :return: """ if os.path.exists(filepath) and os.path.isdir(filepath): logging.info(UsualTools().get_current_time() + u"=>run.py: run_all: 执行%s下所有用例。" % filepath) # 'testcase', pattern='Testcase*.xlsx' filepath = filepath[-1:] == '/' and filepath or filepath + '/' list_all_xlsx = [filepath + i for i in os.listdir(filepath)] logging.info(UsualTools().get_current_time() + u"=>run.py: run_all: 执行文件列表:%s " % list_all_xlsx) run_http_suites(list_all_xlsx, para_dict) else: logging.info(UsualTools().get_current_time() + u"=>run.py: run_all: 文件夹%s不存在,测试终止。" % filepath)
def run_http_cases(file="TestcasesForHttp/Testcases_http.xlsx", para_dict={}): """执行单个测试用例,并生成测试报告到reports目录下。 :param file: 1/默认不传,那么就是运行Testcases目录下的所有用例excel文件,并一一生成报告.2/传递多个文件用,间隔.3/传递一个只要写入名字即可. :param case_line_list: 1/默认不传,或者传递 null或者非数字均代表之行所有Excel中用例.2/传递多个line值用,间隔.3/只传递一个值就执行单一用例.此参数只在file为单一文件时生效. :param report_path:1/不传就使用默认参数存入到reports目录下 2/传递路径如果非法不存在此目录提示用户并终止执行. :param conf:1/不传默认使用test配置.2/可传递参数时test dev stage,对应家在相应的conf文件. :return: """ case_line_list = para_dict['case_line_list'] report_path = para_dict['report_path'] conf = para_dict['conf'] env = para_dict['env'] is_send_mail = para_dict['is_send_mail'] == "YES" and True or False mail_list = para_dict['mail_list'] confpath = para_dict['confpath'] report_filename = para_dict['report_filename'] result_dicts = {} if os.path.exists(file) and os.path.isfile(file): logging.info(UsualTools().get_current_time() + u"=>run.py: run_http_cases: 开始执行用例文件%s ".encode('utf8') % file) filepath = file # 获取文件路径 通过参数传递或者Config默认配置 time_before_get_case_list = time() case_list = [] try: case_list = ExcelForHttpProcess().get_case_list( filepath, case_line_list) except Exception, e: logging.info( UsualTools().get_current_time() + u"=>run.py: run_http_cases: ExcelProcess().get_case_list调用失败:%s" % e) time_after_get_case_list = time() if len(case_list) == 0: logging.info(UsualTools().get_current_time() + u"=>run.py: run_http_cases:用例列表获取失败!程序退出!") sys.exit(0) logging.info( UsualTools().get_current_time() + u"=>run.py: run_http_cases: ExcelForHttpProcess().get_case_list函数占用时间:%s" % str(time_after_get_case_list - time_before_get_case_list)) logging.info(UsualTools().get_current_time() + u"=>run.py: run_http_cases: case_list长度:%s" % len(case_list)) time_before_execute_testcases = time() logging.debug(u"confpath in run_http_cases: %s" % confpath) case_list_with_testresult = HttpService().execute_testcases( case_list, conf, env) time_after_execute_testcases = time() logging.info( UsualTools().get_current_time() + u"=>run.py: run_http_cases: DubboService().execute_testcases函数占用时间:%s" % str(time_after_execute_testcases - time_before_execute_testcases)) logging.info( UsualTools().get_current_time() + u"=>run.py: run_http_cases: case_list_with_testresult长度:%s" % len(case_list_with_testresult)) file_name_list = file.split('/') report_path = report_path[ -1:] == '/' and report_path or report_path + '/' report_file = report_filename == '' and "Report_%s.html" % file_name_list[ len(file_name_list) - 1] or report_filename report_file = report_path + report_file logging.info( UsualTools().get_current_time() + u"=>run.py: run_http_cases: 用例文件[%s]执行后报告文件路径: %s".encode('utf8') % (file, report_file)) time_before_generate_excel_test_report = time() try: result_dicts = HTML_Report().generate_report_for_http( case_list_with_testresult, report_file) time_after_generate_excel_test_report = time() logging.info( UsualTools().get_current_time() + u"=>run.py: run_http_cases: rp.generate_excel_test_report函数占用时间:%s" % str(time_after_generate_excel_test_report - time_before_generate_excel_test_report)) logging.info( UsualTools().get_current_time() + u"=>run.py: run_http_cases: 用例文件[%s]执行完成!".encode('utf8') % file) except Exception, e: logging.info(u"测试报告生成失败,原因:%s" % e) logging.info("EMAIL_NOT_SEND") if mail_list != "": for i in range(0, 10): try: UsualTools.send_mail(mail_list, u"HTTP接口测试报告", u"报告生成失败,已发送错误原因到管理员,请耐心等待。", "") UsualTools.send_mail(['*****@*****.**'], u"HTTP接口测试报告生成失败,请查看原因", u"报告[%s]生成失败,原因:%s。出错邮箱列表:%s" % (report_file, e, mail_list), "") is_mail_list_send = True break except Exception, e: print u"Email发送失败,尝试再次发送。原因:%s" % e
def ctl_and_report_emma( syslist=["stock", "sku", "trade"], conf=Config("../Config/dev.conf"), webroot='/data/tp/test/InterfaceTestFrameworkWeb/webroot', uri='http://10.0.18.31'): """ 重置emma,并重新插装。 最好是重启系统。 :param syslist: :param conf: :return: """ ret_url_dict = {} #init ret_url_dict for syskey in syslist: ret_url_dict[syskey] = {} ret_url_dict[syskey]['url'] = "None" tmp_dir = "/home/wangjiliang" for value in syslist: ip = conf.conf_dict[value]['host_addr'] port = int(conf.conf_dict[value]['ssh_port']) username = conf.conf_dict[value]['ssh_user'] password = conf.conf_dict[value]['ssh_passwd'] base_dir = conf.conf_dict[value]['base_dir'] # java_src = conf.conf_dict[value]['java_src'] # java_src_list = java_src.split(',') # java_src_final_str = "" # for srcvalue in java_src_list: # java_src_final_str += "%s%s," % (base_dir, srcvalue) # java_src_final_str = java_src_final_str[:-1] java_src_final_str = "%s/%s/%s-dao/src/main/java,%s/%s/%s-engine/src/main/java,%s/%s/%s-service/src/main/java" % ( base_dir, value, value, base_dir, value, value, base_dir, value, value) ctime = UsualTools.get_current_time_numstr() renamereportpath = "reportfor%s%s" % (value, ctime) shell_list = """source /etc/profile; cd %s; rm -rf log/test.ec; java emma ctl -connect localhost:47653 -command coverage.get,log/test.ec; #generate report html rm -rf report/*; #-sp /xxxx/xxxx/javaSources report have src java emma report -r html -sp %s -in log/test.em,log/test.ec -Dreport.html.out.file=report/test.html; #copy to webserver cp -r report %s; scp -P50022 -r %s [email protected]:%s/emma/%s/; rm -rf reportfor*; """ % (tmp_dir, java_src_final_str, renamereportpath, renamereportpath, webroot, value) try: # 生成ssh客户端实例 s = paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(ip, port, username, password, timeout=10) #初始化环境变量 #开始执行shell命令 先加载环境变量 stdin, stdout, stderr = s.exec_command( "cd %s;rm -rf tctlreportcp.sh;echo '%s' > tctlreportcp.sh;chmod +x tctlreportcp.sh;" % (tmp_dir, shell_list)) geneshstr = stdout.read() stdin, stdout, stderr = s.exec_command("cd %s;ls;" % tmp_dir) lsstr = stdout.read() if "tctlreportcp.sh" not in lsstr: logging.debug("ERROR: tctlreportcp.sh not generate!") stdin, stdout, stderr = s.exec_command("cd %s;./tctlreportcp.sh;" % tmp_dir) emmactlstr = stdout.read() if "EMMA: coverage.get: command completed in" in emmactlstr and "writing [html] report to [/home/wangjiliang/report/test.html]" in emmactlstr: url = "%s/emma/%s/%s/test.html" % (uri, value, renamereportpath) logging.debug( "SUCCESS: REPORT generate and send to remote webserver. report url:%s" % url) ret_url_dict[value]['url'] = url else: logging.debug("ERROR: REPORT generate failed; LOG:%s" % emmactlstr) s.close() except Exception, e: logging.debug(UsualTools().get_current_time() + u"=>run.py: ctl_and_report_emma: 发生异常。MSG:%s" % e)
def instr_and_reset_emma(syslist=["stock", "sku", "trade"], conf=Config("../Config/dev.conf")): """ 重置emma,并重新插装。 最好是重启系统。 :param syslist: :param conf: :return: """ tmp_dir = "/home/wangjiliang" for value in syslist: ip = conf.conf_dict[value]['host_addr'] port = int(conf.conf_dict[value]['ssh_port']) username = conf.conf_dict[value]['ssh_user'] password = conf.conf_dict[value]['ssh_passwd'] base_dir = conf.conf_dict[value]['base_dir'] telnet_port = conf.conf_dict[value]['host_port'] try: # 生成ssh客户端实例 s = paramiko.SSHClient() s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(ip, port, username, password, timeout=10) #获取插装jar列表。 stdin, stdout, stderr = s.exec_command("ls %s/lib | grep %s-dao-" % (base_dir, value)) libdaostr = stdout.read().strip() #print libdaostr stdin, stdout, stderr = s.exec_command( "ls %s/lib | grep %s-service-" % (base_dir, value)) libservicestr = stdout.read().strip() #print libservicestr stdin, stdout, stderr = s.exec_command( "ls %s/lib | grep %s-engine-" % (base_dir, value)) libenginestr = stdout.read().strip() #print libenginestr instr_jar_final_str = "%s/lib/%s,%s/lib/%s,%s/lib/%s" % ( base_dir, libdaostr, base_dir, libenginestr, base_dir, libservicestr) #生成shelllist string。 shell_list = """source /etc/profile; cd %s/bin; ./stop.sh; rm -rf %s/log/test.em; #recover lib start. cd %s; rm -rf lib/*; cp -rf libbak/* lib/; #recover lib end. java emma instr -m overwrite -cp %s -ix +com.pzj.* -Dmetadata.out.file=%s/log/test.em; cd %s/bin; ./startup.sh;""" % (base_dir, tmp_dir, base_dir, instr_jar_final_str, tmp_dir, base_dir) #开始执行shell命令 先加载环境变量 stdin, stdout, stderr = s.exec_command( "cd %s;rm -rf tinstr.sh;echo '%s' > tinstr.sh;chmod +x tinstr.sh;" % (tmp_dir, shell_list)) geneshstr = stdout.read() stdin, stdout, stderr = s.exec_command("cd %s;ls;" % tmp_dir) lsstr = stdout.read() if "tinstr.sh" not in lsstr: logging.debug("ERROR: tinstr.sh not generate!") return False, "ERROR: [%s]tinstr.sh not generate!" % value stdin, stdout, stderr = s.exec_command("cd %s;./tinstr.sh;" % tmp_dir) emmastartstr = stdout.read() if "EMMA: processing instrumentation path" not in emmastartstr: logging.debug("ERROR: No EMMA instr executed!") return False, "ERROR: [%s]No EMMA instr executed!" % value logging.debug("%s started." % value) #java进程是否启动 isJavaStarted = False for i in range(0, 10): stdin, stdout, stderr = s.exec_command("ps -ef |grep java") return_value = stdout.read() logging.debug(return_value) if libservicestr in return_value: logging.debug("java started successful!") isJavaStarted = True break else: sleep(2) if isJavaStarted == False: return False, "ERROR: [%s]java started FAIL! LOG:%s" % ( value, return_value) #检测端口是否可用 isPort20880Listened = False for i in range(0, 10): stdin, stdout, stderr = s.exec_command( "netstat -anp --ip |grep 0.0.0.0:%s |grep LISTEN" % telnet_port) return_value = stdout.read() logging.debug(return_value) if ("0.0.0.0:%s" % telnet_port) in return_value: logging.debug( "telnet service on port %s started successful!" % telnet_port) isPort20880Listened = True break else: sleep(2) if isPort20880Listened == False: return False, "ERROR: [%s]telnet service on port %s started FAIL! LOG:%s" % ( value, telnet_port, return_value) stdin, stdout, stderr = s.exec_command( "netstat -anp --ip |grep 47653") return_value = stdout.read() logging.debug(return_value) if ":47653" in return_value: logging.debug("EMMA instr started successful!") else: logging.debug("EMMA instr FAIL!") return False, "ERROR: [%s]EMMA instr FAIL! LOG:%s" % ( value, return_value) s.close() except Exception, e: logging.debug(UsualTools().get_current_time() + u"=>run.py: ctl_and_report_emma: 发生异常。MSG:%s" % e) return False, u"发生异常:%s" % e
except Exception, e: print "No param log, use default[DEBUG]!" logLevel = "DEBUG" try: testenv = argDict['webenv'] == "release" and "release" or "test" except Exception, e: print "No param webenv, use default[test]!" testenv = "test" except Exception, e: print "No args. Use default!" logLevel = 'DEBUG' testenv = 'test' if UsualTools.isWindowsSystem(): print u"windows 只能运行测试环境!" logLevel = 'DEBUG' testenv = 'test' elif UsualTools.isMacOS(): print u"macOS 只能运行测试环境!" logLevel = 'DEBUG' testenv = 'test' ################################################################################################## #初始化数据库配置conf sc = ServiceConf(testenv) db = DBTool(sc.dbhost, sc.dbport, sc.dbusername, sc.dbpassword, sc.dbname) conf_path = "" if UsualTools.isLinuxSystem():#正式环境
except Exception, e: print "No param log, use default[DEBUG]!" logLevel = "DEBUG" try: testenv = argDict['webenv'] == "release" and "release" or "test" except Exception, e: print "No param webenv, use default[test]!" testenv = "test" except Exception, e: print "No args. Use default!" logLevel = 'DEBUG' testenv = 'test' if UsualTools.isWindowsSystem(): print u"windows不能运行任务取消!" elif UsualTools.isMacOS(): print u"macOS不能运行任务取消!" ################################################################################################## #初始化数据库配置conf sc = ServiceConf(testenv) db = DBTool(sc.dbhost, sc.dbport, sc.dbusername, sc.dbpassword, sc.dbname) conf_path = "" if UsualTools.isLinuxSystem(): #正式环境 conf_path = "/home" elif UsualTools.isWindowsSystem(): #王吉亮测试环境 conf_path = "D:/devcodes/DubboInterfaceTestFramework/DubboInterfaceTestFramework" elif UsualTools.isMacOS(): #liyaochao测试环境
[email protected],[email protected] #接收报告的邮件地址,多个邮箱中间用英文半角逗号隔开 report_path=reports #本地存放报告的路径 report_filename=ReportTestcases_http.xlsxHTTPtest.html #报告文件名 confpath=/home/InterfaceFrameworkOnline #Config目录的父目录路径 """ sys.exit() print sysparam #sysparam.decode(MyEncode().os_encoding) param_dict = chardet.detect( sysparam)['encoding'] == 'ascii' and init_sys_argv( sysparam.decode('ascii')) or init_sys_argv( sysparam.decode(MyEncode().os_encoding)) print param_dict except Exception, e: print UsualTools().get_current_time() + u"=>run.py: main: 没有参数:%s" % e param_dict = init_sys_argv( "protocol=DUBBO--file=TestcasesForDubbo/--report_path=reports--conf=test;loglevel=DEBUG--logfile=test.log" .decode('ascii')) processed_param_dict = process_init_param_dict(param_dict) print UsualTools().get_current_time( ) + u"=>run.py: main: 配置dict:%s" % processed_param_dict print UsualTools().get_current_time( ) + u"=>run.py: main: Config conf_dict:%s" % processed_param_dict[ 'conf'].conf_dict for k in processed_param_dict.keys(): print UsualTools().get_current_time() + u"=>run.py: main: 配置:%s:%s" % ( k, processed_param_dict[k])
def generate_excel_test_report(self, list_with_assertion=[], filepath="../reports/TestReport.xlsx", step=0, thread_num=0): """输出excel测试报告 接口参数为传进来的完整的excellist,和测试报告要输出的路径,""" #获取路径名称和文件名称 left, right, top, bottom = [Side(style='thin', color='000000')] * 4 border = Border(left=left, right=right, top=top, bottom=bottom) title_font = Font(bold=True, size='13', color=colors.BLUE) fill = PatternFill(start_color=colors.BLUE, end_color=colors.BLUE) al = Alignment(horizontal="center", vertical="center") return_value = self.statistics(list_with_assertion) list = filepath.split("/") filename_len = len(list[len(list) - 1]) dirname_len = len(filepath) - filename_len filename = list[len(list) - 1] dirname = filepath[0:dirname_len - 1] logging.info( UsualTools().get_current_time() + u"=>ResultProcess.py: generate_excel_test_report: 目录名称为: %s". encode('utf8') % dirname) logging.info( UsualTools().get_current_time() + u"=>ResultProcess.py: generate_excel_test_report: 文件名称为: %s". encode('utf8') % filename) #判断有没有目录下的这个文件 if not os.path.isfile(filepath): #如果没有这个文件,就判断有没有这个目录 if not os.path.isdir(dirname): #没有这个目录,创建目录 os.makedirs(dirname) #创建文件 # 新建一个workbook wb = Workbook() # 第一个sheet是ws else: wb = Workbook() #设置表头 list = [ u'用例编号', u'名称', u'描述', u'系统', u'服务', u'接口', u'数据初始化', u'数据恢复', u'参数', u'预期结果', u'实际结果', u'断言结果', u'测试结果', u'执行时间' ] # 新建一个workbook # 第一个sheet是ws ws = wb.worksheets[0] ws.print_options.horizontalCentered = True ws.title = u"interface test" ws.column_dimensions["A"].width = 10.0 ws.column_dimensions["B"].width = 16.0 ws.column_dimensions["C"].width = 30.0 ws.column_dimensions["D"].width = 13.0 ws.column_dimensions["E"].width = 25.0 ws.column_dimensions["F"].width = 15.0 ws.column_dimensions["G"].width = 12.0 ws.column_dimensions["H"].width = 12.0 ws.column_dimensions["I"].width = 20.0 ws.column_dimensions["J"].width = 20.0 ws.column_dimensions["K"].width = 20.0 ws.column_dimensions["L"].width = 20.0 ws.column_dimensions["M"].width = 10.0 ws.column_dimensions["N"].width = 10.0 #写表头 for i in range(1, len(list) + 1): ws.cell(row=1, column=i).alignment = al ws.cell(row=1, column=i).font = title_font # ws.cell(row=1, column=i).fill = fill ws.cell(row=1, column=i, value=list[i - 1]).border = border # num = 0 # failnum = 0 # system_list = {} # step = 5000 # len_of_list = len(list_with_assertion) # thread_num = len_of_list%step == 0 and len_of_list/step or len_of_list/step+1 # last_num = len_of_list % step # # # start_line = 2 # # for i in range(0,thread_num): # # end_line = (i == thread_num - 1 and last_num != 0) and start_line+last_num or start_line + step # print "ResultProcess.py: generate_excel_test_report: INTO last_num%s" % last_num # print "ResultProcess.py: generate_excel_test_report: INTO LOOP WRITE" # t1 = threading.Thread(target=self.generate_report, args=(ws,start_line,end_line,list_with_assertion)) # t1.start() # t1.join() # start_line += step self.generate_report(ws, 2, len(list_with_assertion) + 2, list_with_assertion, border) fail_num = 0 pass_num = 0 error_num = 0 for i in range(len(list_with_assertion)): if list_with_assertion[i].get('test_result') == 'FAIL': fail_num += 1 elif list_with_assertion[i].get('test_result') == 'PASS': pass_num += 1 else: error_num += 1 if pass_num != 0: prints = u'共执行%s条,成功%s条,失败%s条,通过率为%.2f%%' % ( pass_num + fail_num, pass_num, fail_num, 100 * float(pass_num) / float(pass_num + fail_num)) else: prints = u'共执行%s条,成功%s条,失败%s条,通过率为%s%%' % ( pass_num + fail_num, pass_num, fail_num, "0.00") logging.info(UsualTools().get_current_time() + u"=>ResultProcess.py: generate_excel_test_report: %s" % prints) ws.merge_cells(start_row=len(list_with_assertion) + 2, start_column=1, end_row=len(list_with_assertion) + 2, end_column=14) ws.merge_cells(start_row=len(list_with_assertion) + 4, start_column=1, end_row=len(list_with_assertion) + 4 + return_value.count('\n'), end_column=14) logging.info(UsualTools().get_current_time() + u"=>ResultProcess.py: generate_excel_test_report: %s" % return_value.count('\n')) ws.cell(row=len(list_with_assertion) + 2, column=1, value=prints) # print u"ResultProcess.py: generate_excel_test_report: %s" % return_value vertAlign = Font(vertAlign='subscript') ws.cell(row=len(list_with_assertion) + 4, column=1, value=return_value) wb.save(filepath)
def generate_report(self, ws, start_row=0, end_row=0, list_with_assertion=[], border=Border): """将结果写入excel""" logging.info( UsualTools().get_current_time() + u"=>ResultProcess.py: generate_excel_test_report: 写入excel最后的行数为%s" % end_row) pass_font = Font(color='458B00', bold=True) fail_font = Font(color='8B2323', bold=True) error_font = Font(color='8B5A00', bold=True) for i in range(start_row, end_row): j = i - 2 tmpCase = {} ws.cell(row=i, column=1).border = border tmpCase['case_id'] = ws.cell( row=i, column=1, value=list_with_assertion[j].get('case_id')).font = Font( bold=True) tmpCase['name'] = ws.cell( row=i, column=2, value=list_with_assertion[j].get('name')).border = border tmpCase['desc'] = ws.cell( row=i, column=3, value=list_with_assertion[j].get('desc')).border = border tmpCase['system'] = ws.cell( row=i, column=4, value=list_with_assertion[j].get('system')).border = border tmpCase['service'] = ws.cell( row=i, column=5, value=list_with_assertion[j].get('service')).border = border tmpCase['method'] = ws.cell( row=i, column=6, value=list_with_assertion[j].get('method')).border = border tmpCase['data_init'] = ws.cell( row=i, column=7, value=list_with_assertion[j].get('data_init')).border = border tmpCase['data_recover'] = ws.cell( row=i, column=8, value=list_with_assertion[j].get( 'data_recover')).border = border tmpCase['params'] = ws.cell( row=i, column=9, value=list_with_assertion[j].get('params')).border = border tmpCase['expect'] = ws.cell( row=i, column=10, value=list_with_assertion[j].get('expect')).border = border tmpCase['return_msg'] = ws.cell( row=i, column=11, value=list_with_assertion[j].get('return_msg')).border = border tmpCase['assert_msg'] = ws.cell( row=i, column=12, value=list_with_assertion[j].get('assert_msg')).border = border tmpCase['test_result'] = ws.cell(row=i, column=13).border = border if list_with_assertion[j].get('test_result') == "PASS": ws.cell(row=i, column=13, value=list_with_assertion[j].get( 'test_result')).font = pass_font elif list_with_assertion[j].get('test_result') == "FAIL": ws.cell(row=i, column=13, value=list_with_assertion[j].get( 'test_result')).font = fail_font else: tmpCase['test_result'] = ws.cell( row=i, column=13, value=list_with_assertion[j].get( 'test_result')).font = error_font tmpCase['perform_time'] = ws.cell( row=i, column=14, value=list_with_assertion[j].get( 'perform_time')).border = border
# print "%s" % system_list[k][k2][k3] if system_dicts[k][k2][k3]['pass'] != 0: test_num += u"|------✧接口%s中有用例%d条,成功%d条,失败%d条,通过率为%.2f%%\n" % ( k3, system_dicts[k][k2][k3]['count'], system_dicts[k][k2][k3]['pass'], system_dicts[k][k2][k3]['fail'], 100 * float(system_dicts[k][k2][k3]['pass']) / float(system_dicts[k][k2][k3]['count'])) else: test_num += u"|------✧接口%s中有用例%d条,成功%d条,失败%d条,通过率为%s%%\n" % ( k3, system_dicts[k][k2][k3]['count'], system_dicts[k][k2][k3]['pass'], system_dicts[k][k2][k3]['fail'], u"0.00") logging.info(UsualTools().get_current_time() + u"=>ResultProcess.py: statistics测试统计为 \n%s" % test_num) return test_num def send_mail(self, receiver, title, content, file): """ :param receiver: :param title: :param content: :param file: :return: """ pass
def thread_run_dubbo_debug(res,confDict,db): try: para_dict = {} para_dict['case_list'] = [{}] para_dict['case_list'][0]['case_id'] = "TC_%s_1" % res[4].strip() para_dict['case_list'][0]['name'] = res[2].replace("\r\n", '') para_dict['case_list'][0]['desc'] = res[3].strip() para_dict['case_list'][0]['system'] = res[4].replace("\r\n", '') para_dict['case_list'][0]['service'] = res[5].replace("\r\n", '') para_dict['case_list'][0]['method'] = res[6].replace("\r\n", '') para_dict['case_list'][0]['data_init'] = res[7].strip() para_dict['case_list'][0]['data_recover'] = res[8].strip() para_dict['case_list'][0]['params'] = res[9].replace("\r\n", '') para_dict['case_list'][0]['expect'] = res[10].strip() para_dict['case_list'][0]['varsconf'] = res[11].strip() para_dict['env'] = res[12].strip() print u"%s:当前调试环境[%s],调试人员[%s],协议[DUBBO]。" % (UsualTools.get_current_time(), para_dict['env'], res[13]) logging.info(u"当前调试环境[%s],调试人员[%s],协议[DUBBO]。" % (para_dict['env'], res[13])) confKey = para_dict['env'].strip() para_dict['conf'] = confDict[confKey] # 构建用例 logging.info( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s" % PromptMsg.startDebugTask) case_list_with_testresult, take_time = debug_case(para_dict) logging.debug( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: case_list_with_testresult::%s" % case_list_with_testresult) logging.debug( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: =>takeTime:%s" % str( take_time)) if case_list_with_testresult[0]['test_result'] in [PASS, FAIL, ERROR]: logging.info( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s " % PromptMsg.execSuccess) sqlupdate = """update tb_dubbo_debug set status=2,returnMsg='%s',assertMsg='%s',takeTime='%s',initTakeTime='%s',execTakeTime='%s',recoverTakeTime='%s', dataInit='%s',dataRecover='%s',parameter='%s',expct='%s' where id=%s""" \ % (case_list_with_testresult[0]['return_msg'].replace("'", '"'), case_list_with_testresult[0]['assert_msg'].replace("'", '"'), str(case_list_with_testresult[0]['initTakeTime'] + case_list_with_testresult[0][ 'execTakeTime'] + case_list_with_testresult[0]['recoverTakeTime']), str(case_list_with_testresult[0]['initTakeTime']), str(case_list_with_testresult[0]['execTakeTime']), str(case_list_with_testresult[0]['recoverTakeTime']), case_list_with_testresult[0]['data_init'].replace("'", '"'), case_list_with_testresult[0]['data_recover'].replace("'", '"'), case_list_with_testresult[0]['params'].replace("'", '"'), case_list_with_testresult[0]['expect'].replace("'", '"'), res[0]) logging.debug(UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s " % sqlupdate) db.execute_sql(sqlupdate) logging.info( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s " % PromptMsg.intoDbSuccess) logging.info( "=====================================================================================================") else: logging.info(UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: EXECUTED failed!") tmpText = PromptMsg.debugFailMsg logging.info(UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s " % tmpText) sqlupdate = "update tb_dubbo_debug set status=2,returnMsg='%s',assertMsg='%s',takeTime='%s' where id=%s" \ % (tmpText, tmpText, '0', res[0]) logging.debug(UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s" % sqlupdate) db.execute_sql(sqlupdate) logging.info( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: EXECUTED failed INTODB successfully!") logging.info( "=====================================================================================================") except Exception, e: logging.info(UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s " % e) logging.info(UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: EXECUTED exception!") tmpText = u"%s:%s" % (PromptMsg.exceptionAndCheck, e) logging.info(UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s " % tmpText) sqlupdate = "update tb_dubbo_debug set status=2,returnMsg='%s',assertMsg='%s',takeTime='%s' where id=%s" \ % (tmpText, tmpText, '0', res[0]) logging.debug(UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s" % sqlupdate) db.execute_sql(sqlupdate) logging.info( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s " % PromptMsg.intoDbSuccess) logging.info( "=====================================================================================================")
def thread_run_http_group_debug(group_case_list,confDict,env,db): try: tmpId = group_case_list[0].tgcStepList[0].id tmpGCId = group_case_list[0].tgcStepList[0].caseId para_dict = {} para_dict['case_list'] = group_case_list para_dict['env'] = env addBy = group_case_list[0].tgcDesc.addBy # print para_dict print u"%s:当前调试环境[%s],调试人员[%s],协议[HTTP]。" % (UsualTools.get_current_time(), para_dict['env'], addBy) logging.info(u"当前调试环境[%s],调试人员[%s],协议[HTTP]。" % (para_dict['env'], addBy)) para_dict['conf'] = confDict['http'] # print para_dict # 构建用例 logging.info( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s" % PromptMsg.startHttpDebugTask) case_list_with_testresult, take_time = debug_http_group_case(para_dict) logging.debug( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: case_list_with_testresult::%s" % case_list_with_testresult) logging.debug( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: =>takeTime:%s" % str( take_time)) tgc = case_list_with_testresult[0] tgcDesc = tgc.tgcDesc tgcStepList = tgc.tgcStepList for tmpTgcStep in tgcStepList: tmpId = tmpTgcStep.id if tmpTgcStep.test_result in [PASS, FAIL, ERROR]: headIntoDb = """URI:[%s]; METHOD:[%s]; HEADER:[%s];""" % (tmpTgcStep.host,tmpTgcStep.method,tmpTgcStep.header) logging.info( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s " % PromptMsg.execSuccess) sqlupdate = """update tb_http_group_case_step_debug set status=2,returnMsg='%s',assertMsg='%s',takeTime='%s',initTakeTime='%s',execTakeTime='%s',recoverTakeTime='%s',parameter='%s', dataInit='%s',dataRecover='%s',httpRequestHead='%s',expect='%s' where id=%s""" \ % (tmpTgcStep.return_msg.replace("'", '"'), tmpTgcStep.assert_msg.replace("'", '"'), str(tmpTgcStep.totalTakeTime), str(tmpTgcStep.initTakeTime), str(tmpTgcStep.execTakeTime), str(tmpTgcStep.recoverTakeTime), tmpTgcStep.params.replace("'", '"'), tmpTgcStep.data_init.replace("'", '"'), tmpTgcStep.data_recover.replace("'", '"'), headIntoDb.replace("'", '"'), tmpTgcStep.expect.replace("'", '"'), tmpId) logging.debug( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s " % sqlupdate) db.execute_sql(sqlupdate) logging.info( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s" % PromptMsg.intoDbSuccess) logging.info( "=====================================================================================================") else: logging.info( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: EXECUTED failed!") tmpText = PromptMsg.debugFailMsg logging.info( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s " % tmpText) sqlupdate = "update tb_http_group_case_step_debug set status=2,returnMsg='%s',assertMsg='%s',takeTime='%s' where id=%s" \ % (tmpText, tmpText, '0', tmpId) logging.debug( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s" % sqlupdate) db.execute_sql(sqlupdate) logging.info( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: EXECUTED failed INTODB successfully!") logging.info( "=====================================================================================================") sqlupdate = """update tb_http_group_case_step_debug set status=2 where addBy="%s" """ % addBy logging.debug( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s " % sqlupdate) db.execute_sql(sqlupdate) except Exception, e: logging.debug(traceback.format_exc()) logging.info(UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s " % e) logging.info( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: EXECUTED exception!") tmpText = u"%s:%s" % (PromptMsg.exceptionAndCheck, e) logging.info(UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s " % tmpText) sqlupdate = "update tb_http_group_case_step_debug set status=2,returnMsg='%s',assertMsg='%s',takeTime='%s' where id=%s" \ % (tmpText, tmpText, '0', tmpId) logging.debug(UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s" % sqlupdate) db.execute_sql(sqlupdate) logging.info( UsualTools().get_current_time() + u"=>runCaseDebugTask.py: run_on_linux: %s" % PromptMsg.intoDbSuccess) logging.info( "=====================================================================================================")
def run_cases(file="TestcasesForDubbo/Testcases_stock.xlsx", para_dict={}): """执行单个测试用例,并生成测试报告到reports目录下。 :param file: 1/默认不传,那么就是运行Testcases目录下的所有用例excel文件,并一一生成报告.2/传递多个文件用,间隔.3/传递一个只要写入名字即可. :param case_line_list: 1/默认不传,或者传递 null或者非数字均代表之行所有Excel中用例.2/传递多个line值用,间隔.3/只传递一个值就执行单一用例.此参数只在file为单一文件时生效. :param report_path:1/不传就使用默认参数存入到reports目录下 2/传递路径如果非法不存在此目录提示用户并终止执行. :param conf:1/不传默认使用test配置.2/可传递参数时test dev stage,对应家在相应的conf文件. :return: """ case_line_list = para_dict['case_line_list'] report_path = para_dict['report_path'] conf = para_dict['conf'] env = para_dict['env'] is_send_mail = para_dict['is_send_mail'] == "YES" and True or False mail_list = para_dict['mail_list'] confpath = para_dict['confpath'] report_filename = para_dict['report_filename'] result_dicts = {} if os.path.exists(file) and os.path.isfile(file): logging.info(UsualTools().get_current_time() + u"=>run.py: run_cases: 开始执行用例文件%s ".encode('utf8') % file) filepath = file # 获取文件路径 通过参数传递或者Config默认配置 time_before_get_case_list = time() case_list = [] try: case_list = ExcelProcess().get_case_list(filepath, case_line_list) except Exception, e: logging.info( UsualTools().get_current_time() + u"=>run.py: run_cases: ExcelProcess().get_case_list调用失败:%s" % e) time_after_get_case_list = time() if len(case_list) == 0: logging.info(UsualTools().get_current_time() + u"=>run.py: run_cases:用例列表获取失败!程序退出!") sys.exit(0) logging.info( UsualTools().get_current_time() + u"=>run.py: run_cases: ExcelProcess().get_case_list函数占用时间:%s" % str(time_after_get_case_list - time_before_get_case_list)) logging.info(UsualTools().get_current_time() + u"=>run.py: run_cases: case_list长度:%s" % len(case_list)) time_before_execute_testcases = time() #根据case_list生成系统list syslist = [] for casevalue in case_list: if casevalue['system'] not in syslist: syslist.append(casevalue['system']) if para_dict['restart'] == "YES": #resetEmma and instrEmma bool_ret, msg = instr_and_reset_emma(syslist, conf) case_list_with_testresult = DubboService().execute_testcases( case_list, conf, env) #generate emmaReport and scp to testwebserver,并返回系统对应的web地址的覆盖率网页。 #初始化urldict url_dict = {} # init ret_url_dict for syskey in syslist: url_dict[syskey] = {} url_dict[syskey]['url'] = "None" if para_dict['restart'] == "YES": url_dict = ctl_and_report_emma(syslist, conf) #TODO 不同系统下根据url获取网页数据并使用beautifulSoap解析出来覆盖率信息。(也可在生成报告时获取) time_after_execute_testcases = time() logging.info( UsualTools().get_current_time() + u"=>run.py: run_cases: DubboService().execute_testcases函数占用时间:%s" % str(time_after_execute_testcases - time_before_execute_testcases)) logging.info(UsualTools().get_current_time() + u"=>run.py: run_cases: case_list_with_testresult长度:%s" % len(case_list_with_testresult)) file_name_list = file.split('/') report_path = report_path[ -1:] == '/' and report_path or report_path + '/' report_file = report_filename == '' and "Report_%s.html" % file_name_list[ len(file_name_list) - 1] or report_filename report_file = report_path + report_file logging.info( UsualTools().get_current_time() + u"=>run.py: run_cases: 用例文件[%s]执行后报告文件路径: %s".encode('utf8') % (file, report_file)) time_before_generate_excel_test_report = time() try: result_dicts = HTML_Report().generate_report( case_list_with_testresult, url_dict, report_file) time_after_generate_excel_test_report = time() logging.info( UsualTools().get_current_time() + u"=>run.py: run_cases: rp.generate_excel_test_report函数占用时间:%s" % str(time_after_generate_excel_test_report - time_before_generate_excel_test_report)) logging.info(UsualTools().get_current_time() + u"=>run.py: run_cases: 用例文件[%s]执行完成!".encode('utf8') % file) except Exception, e: logging.info(u"测试报告生成失败,原因:%s" % e) logging.info("EMAIL_NOT_SEND") print "EMAIL_NOT_SEND" if mail_list != "": for i in range(0, 10): try: UsualTools.send_mail(mail_list, u"HTTP接口测试报告", u"报告生成失败,已发送错误原因到管理员,请耐心等待。", "") UsualTools.send_mail(['*****@*****.**'], u"HTTP接口测试报告生成失败,请查看原因", u"报告[%s]生成失败,原因:%s。出错邮箱列表:%s" % (report_file, e, mail_list), "") is_mail_list_send = True break except Exception, e: print u"Email发送失败,尝试再次发送。原因:%s" % e
sys.exit() #发送邮件这里 #is_send_mail_send = False is_mail_list_send = False if is_send_mail: for k, v in result_dicts.items(): if type(v) != type({}): continue if v['fail'] > 0: email_list = conf.conf_dict[k]['email_list'] title = u"Dubbo接口测试--系统%s出现失败用例%d个" % ( k, result_dicts[k]['fail']) content = u"详情见附件测试报告。" for i in range(0, 10): try: UsualTools.send_mail(email_list, title, content, report_file) #is_send_mail_send = True break except Exception, e: print u"Email发送失败,尝试再次发送。原因:%s" % e if mail_list != "": for i in range(0, 10): try: UsualTools.send_mail(mail_list, u"Dubbo接口测试报告", u"详情见附件。", report_file) is_mail_list_send = True break except Exception, e: print u"Email发送失败,尝试再次发送。原因:%s" % e #第一阶段,必须有mail_list去接收报告才算测试完成。
import sys reload(sys) sys.setdefaultencoding('utf8') sys.path.append("..") from Lib.UsualTools import UsualTools import os, subprocess from Lib.DBTool import DBTool import logging import time from Config.Config import Config if __name__ == "__main__": if UsualTools.isLinuxSystem(): logging.basicConfig( level='INFO', format= '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='/home/InterfaceFrameworkOnline/logs/runtask.log', filemode='w') # 循环遍历t_task表,顺序执行任务,执行时把state变成2,执行后变成3. conf = Config("../ConfFile/platform.conf").conf_dict while True: db = DBTool(conf['db']['host'], int(conf['db']['port']), conf['db']['username'], conf['db']['password'], conf['db']['dbname']) results = db.execute_sql( "select * from tb_taskpython where state=1 or state=2 order by create_time asc"