def lookup(self): while True: time.sleep(2) n = len(self.tasking) logger.info(f'当前正在执行的任务数为{n}') inds = [] for i in range(n): html_path = os.path.join( cfg.getConfig('report_path'), self.tasking[i]["file_name"]) + '.html' logger.info(html_path) if not os.path.exists(html_path): if time.time( ) - self.tasking[i]["start_time"] < self.interval: continue else: logger.error(f'测试任务执行超时') inds.append(i) html = f'<html><body>' \ f'<h3>异常提醒:{self.tasking[i]["build_file"]} 测试任务执行超时,请检查!</h3>' \ f'<p style="color:blue;">此邮件自动发出,请勿回复。</p></body></html>' try: sendMsg(html, self.tasking[i], is_path=False) except Exception as err: logger.error(err) else: time.sleep(1) logger.info('测试任务执行完成') flag = self.post_deal(self.tasking[i]) if flag: inds.append(i) for j in range(len(inds) - 1, -1, -1): self.tasking.pop(inds[j])
def run(self, case_email_path): """ 执行测试任务 :param case_email_path: 列表,第一个元素是测试用例文件路径,第二个元素是收件人的txt文件路径 :return: """ if int(cfg.getConfig('is_git')): logger.info('准备从git上拉取最新版本') repo = Repo(cfg.getConfig('git_path')) remote = repo.remote() remote.pull() logger.info('从git上拉取版本成功') file_name = None case_path = case_email_path[0] email_path = case_email_path[1] build_path = os.path.join(case_path, 'build.xml') logger.info(f'开始执行测试任务{build_path}') start_time = time.strftime('%Y-%m-%d %H:%M:%S') res = os.popen( 'ant -f {}'.format(build_path)).readlines() # 执行测试,并等待测试完成 for i in range(len(res)): if 'Build failed' in res[i]: # 如果有失败日志,打印出 logger.error('{}\n{}'.format(res[i - 1], res[i])) break if 'xslt' in res[i] and 'Processing' in res[i] and 'to' in res[ i]: # 获取测试报告文件名 line = res[i].strip() logger.debug(line) if '/' in line: file_name = line.split('/')[-1] else: file_name = line.split('\\')[-1] logger.info(file_name) break if file_name: logger.info('测试任务执行完成') time.sleep(2) msg = self.parse_html(file_name, case_path) # 重组html sendMsg(msg['fail_case'], email_path) # 发送邮件 string = f"{start_time},{build_path},{msg['total_num']},{msg['failure_num']}\n" self.lock.acquire() logger.info(f'写测试记录到本地, {string}') with open(os.path.join(case_path, cfg.getConfig('record_name')), 'a', encoding='utf-8') as f: f.write(string) self.lock.release() logger.info('测试完成') else: logger.error('测试任务执行失败')
async def sendEmail(request): """ get请求,用于发送邮件,用于客户端异常时发送邮件提醒 :param request: :return: """ name = request.match_info['name'] port = request.match_info['port'] ind = request.match_info['ind'] IP = request.match_info['IP'] email_file = glob.glob( os.path.join(cfg.getConfig('case_path'), name, 'email_*.txt')) if len(email_file) == 0: return web.Response(body=json.dumps( { 'code': 0, 'message': '没有设置收件人邮箱地址的txt文件,测试任务执行失败', 'data': None }, ensure_ascii=False)) elif len(email_file) > 1: return web.Response(body=json.dumps( { 'code': 0, 'message': '应该只有一个收件人邮箱地址的txt文件,但是找到了多个,测试任务执行失败', 'data': None }, ensure_ascii=False)) if int(ind) == 1: msg = f'{IP} 服务器上的 {port} 端口已经停了,无法执行 {name} 的接口自动化测试,请及时检查端口状态' else: msg = f'{IP} 服务器上的 {name} 接口自动化测试执行异常,请检查测试用例,或手动执行get请求 http://{IP}:{PORT}/run?systemName={name} ' html = f'<html><body>' \ f'<h3>异常提醒:{msg}!</h3>' \ f'<p style="color:blue;">此邮件自动发出,请勿回复。</p></body></html>' try: sendMsg(html, email_file[0], is_path=False) return web.Response( body=json.dumps({ 'code': 1, 'message': '邮件提醒发送成功!', 'data': None }, ensure_ascii=False)) except Exception as err: return web.Response(body=json.dumps( { 'code': 0, 'message': err, 'data': None }, ensure_ascii=False))
def post_deal(self, paths): msg = self.parse_html(paths["file_name"] + '.html', paths["case_path"]) # 重组html sendMsg(msg['fail_case'], paths, failure_num=msg['failure_num']) # 发送邮件 string = f"{paths['start_time']},{paths['build_file']},{msg['total_num']},{msg['failure_num']}\n" logger.info(f'写测试记录到本地, {string}') with open(paths["record_path"], 'a', encoding='utf-8') as f: f.write(string) logger.info('测试完成') return True
async def sendEmail(request): """ get请求,用于发送邮件,用于客户端异常时发送邮件提醒 :param request: :return: """ name = request.match_info['name'] port = request.match_info['port'] email_file = glob.glob( os.path.join(cfg.getConfig('case_path'), name, 'email_*.txt')) if len(email_file) == 0: return web.Response(body=json.dumps( { 'code': 0, 'message': '没有设置收件人邮箱地址的txt文件,测试任务执行失败', 'data': None }, ensure_ascii=False)) elif len(email_file) > 1: return web.Response(body=json.dumps( { 'code': 0, 'message': '应该只有一个收件人邮箱地址的txt文件,但是找到了多个,测试任务执行失败', 'data': None }, ensure_ascii=False)) html = f'<html><body>' \ f'<h3>异常提醒:{name}的接口自动化测试环境对应的{port}端口已经停了,请及时重启或更换端口!</h3>' \ f'<p style="color:blue;">此邮件自动发出,请勿回复。</p></body></html>' try: sendMsg(html, email_file[0], is_path=False) return web.Response( body=json.dumps({ 'code': 1, 'message': '邮件提醒发送成功', 'data': None }, ensure_ascii=False)) except Exception as err: return web.Response(body=json.dumps( { 'code': 0, 'message': err, 'data': None }, ensure_ascii=False))
def run(self, case_email_path): """ 执行测试任务 :param case_email_path: 列表,第一个元素是测试用例文件路径,第二个元素是收件人的txt文件路径 :return: """ try: if int(cfg.getConfig('is_git')): logger.info('准备从git上拉取最新版本') repo = Repo(cfg.getConfig('git_path')) remote = repo.remote() remote.pull() logger.info('从git上拉取版本成功') except Exception as err: logger.error(err) file_name = None error_msg = None case_path = case_email_path[0] email_path = case_email_path[1] build_path = os.path.join(case_path, 'build.xml') logger.info(f'开始执行测试任务{build_path}') try: start_time = time.strftime('%Y-%m-%d %H:%M:%S') res = os.popen( 'ant -f {}'.format(build_path)).readlines() # 执行测试,并等待测试完成 logger.debug(res) length = len(res) for i in range(length, -1, -1): if 'Failed' in res[i]: # 如果有失败日志,打印出 error_msg = '{}\n{}'.format(res[i], res[i - 1]) logger.error(error_msg) break if 'xslt' in res[i] and 'Processing' in res[i] and 'to' in res[ i]: # 获取测试报告文件名 line = res[i].strip() logger.debug(line) if '/' in line: file_name = line.split('/')[-1] else: file_name = line.split('\\')[-1] logger.info(file_name) break del res if file_name: logger.info('测试任务执行完成') time.sleep(2) msg = self.parse_html(file_name, case_path) # 重组html sendMsg(msg['fail_case'], email_path, failure_num=msg['failure_num']) # 发送邮件 string = f"{start_time},{build_path},{msg['total_num']},{msg['failure_num']}\n" self.lock.acquire() logger.info(f'写测试记录到本地, {string}') with open(os.path.join(case_path, cfg.getConfig('record_name')), 'a', encoding='utf-8') as f: f.write(string) self.lock.release() logger.info('测试完成') else: error_msg = 'html格式的测试报告未找到' except Exception as err: error_msg = err logger.error(err) if error_msg: logger.error(f'测试任务执行失败,失败信息:{error_msg}') html = f'<html><body>' \ f'<h3>异常提醒:{build_path} 测试任务执行失败,请重新执行! 失败信息:{error_msg}</h3>' \ f'<p style="color:blue;">此邮件自动发出,请勿回复。</p></body></html>' try: sendMsg(html, email_path, is_path=False) except Exception as err: logger.error(err)