def delete_fp(): try: lg = [Path.father_path + '\\log', Path.report_path()] for j in lg: if os.path.exists(j): ls = os.listdir(j) for i in range(0, len(ls)): path = os.path.join(lg, ls[i]) if os.path.exists(path): create_time = time.localtime( os.stat(path).st_ctime) # 文件最后访问时间 y = time.strftime('%Y', create_time) m = time.strftime('%m', create_time) d = time.strftime('%d', create_time) h = time.strftime('%H', create_time) M = time.strftime('%M', create_time) d2 = datetime.datetime(int(y), int(m), int(d), int(h), int(M)) # 格式化时间 time_difference = (datetime.datetime.now() - d2).days # 计算时间差 if time_difference >= 2: # 时间差超过10天,则删除 shutil.rmtree(path) else: logging.warning('日志或测试报告文件夹不存在') except Exception as e: logging.error(e)
def send_email(test_time): address = configemail.ConfigEmail() # 发件人地址 From = address.get_sender() # 收件人地址,多个收件人用逗号隔开 To = address.get_addressee() # 附件名 file_name = Path.report_path() + test_time + '.html' server = smtplib.SMTP(address.get_smtp()) # 仅smtp服务器需要验证时 server.login(address.get_login(), address.get_authorization_code()) # 构造MIMEMultipart对象做为根容器 main_msg = email.MIMEMultipart.MIMEMultipart() # 构造MIMEText对象做为邮件显示内容并附加到根容器 text_msg = email.MIMEText.MIMEText("Nidone测试报告", charset="utf-8") main_msg.attach(text_msg) # 构造MIMEBase对象做为文件附件内容并附加到根容器 ctype, encoding = mimetypes.guess_type(file_name) if ctype is None or encoding is not None: ctype = 'application/octet-stream' maintype, subtype = ctype.split('/', 1) file_msg = email.MIMEImage.MIMEImage(open(file_name, 'rb').read(), subtype) logging.info(ctype, encoding) # 设置附件头 basename = os.path.basename(file_name) # 修改邮件头 file_msg.add_header('Content-Disposition', 'attachment', filename=basename) main_msg.attach(file_msg) # 设置根容器属性 main_msg['From'] = From main_msg['To'] = To main_msg['Subject'] = "Nidone测试报告 " main_msg['Date'] = email.Utils.formatdate() # 得到格式化后的完整文本 fulltext = main_msg.as_string() # 用smtp发送邮件 try: server.sendmail(From, To, fulltext) finally: server.quit()
def report(info, devices): try: logging.info('初始化测试报告') workbook = xlsxwriter.Workbook(Path.report_path() + ''.join(devices.split(':')) + '报告.xlsx') bo = OperateReport(workbook) logging.info('生成监控报告') bo.monitor(info) logging.info('生成错误日志') bo.crash() logging.info('生成详细报告') bo.analysis(info) bo.close() logging.info('报告生成成功') except Exception as e: logging.error('生成测试报告失败') logging.error(e) raise
def exception_handling(e, index=None, test_name=None, method_name=None, op=None): """ 错误处理 :param e: 报错内容 :param index: 用例编号 :param test_name: 测试用例名称 :param method_name: 测试用例对应方法 :param op: 操作驱动 :return: """ global l logging.error(e) path = Path.report_path() + runtime.test_start_time() + '_error' mkdir_log_directory.mk_dir(path) # 创建错误日志目录 path1 = Path.log_path() + runtime.test_start_time() + '.log' if index: log_error = path + '\\' + test_name.decode('utf8') + '.txt' # 记录错误日志文件 way = path + '\\' + method_name + runtime.test_start_time() + '.png' op.screen(way, path) # 截图 log.error_log(path1, log_error, test_name) if 'AssertionError' in e: for i in range(0, len(l)): if index == l[i][0]: l[i].append('fail') l[i].append(log_error) l[i].append(way) else: for i in range(0, len(l)): if index == l[i][0]: l[i].append('error') l[i].append(log_error) l[i].append(way) else: log_error = path + '\\' + 'error.txt' # 记录错误日志文件 log.error_log(path1, log_error)