Ejemplo n.º 1
0
    def __init__(self, loglevel, logger):
        self.logger = logger
        self.time = globalTime.getGlobalTime()
        self.logname = rootpath + relpath + self.time + '.log'
        # logger 输出格式
        format_dict = \
            {
                1: logging.Formatter('%(asctime)s - %(filename)s - %(levelname)s - %(message)s'),
                2: logging.Formatter('%(asctime)s - %(funcName)s - %(levelname)s - %(message)s'),
                3: logging.Formatter('%(asctime)s - %(module)s - %(levelname)s - %(message)s'),
                4: logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
            }
        # 这里进行判断,如果logger.handlers列表为空,则添加,否则,直接去写日志

        # 创建一个logger,默认级别是DEBUG
        self.logger = logging.getLogger(logger)
        self.logger.setLevel(logging.DEBUG)

        # 创建一个handler,用于写入日志文件
        self.fh = logging.FileHandler(self.logname)
        self.fh.setLevel(logging.DEBUG)

        # 再创建一个handler,用于输出到控制台
        self.ch = logging.StreamHandler()
        self.ch.setLevel(logging.DEBUG)

        # 定义handler的输出格式
        formatter = format_dict[int(loglevel)]
        self.fh.setFormatter(formatter)
        self.ch.setFormatter(formatter)

        # 给logger添加handler
        self.logger.addHandler(self.fh)
        self.logger.addHandler(self.ch)
Ejemplo n.º 2
0
    def __init__(self, configfile):
        self.time = globalTime.getGlobalTime()
        self.filename = rootpath + relpath + self.time + '.xls'

        self.config = configparser.ConfigParser()
        self.config.read(configfile)
        self.host = self.config.get('MAIL', 'HOST')
        self.user = self.config.get('MAIL', 'USER')
        self.pwd = self.config.get('MAIL', 'PWD')
        self.receivers = self.config.get('MAIL', 'RECEIVERS')
        self.list = self.receivers.split(';')

        self.getClass = getMailContent.getMailContent()
        self.content = self.getClass.readAndJudge()
        self.getClass.closeRead()

        self.logger = writeLog.writeLogger(loglevel=4, logger="sendMail")
        if not self.getClass.getFlag():
            self.mailContent.append(
                "                        测试情况说明如下(具体情况请查看分析文件)\n")
        else:
            self.mailContent.append("                            测试情况说明如下 \n")

        for i in range(0, len(self.content)):
            self.mailContent.append(self.content[i])
        self.msg = '\n'.join(self.mailContent)

        self.mailContent.clear()
        self.getClass.clearVar()

        message = MIMEMultipart('related')
        message['From'] = formataddr(["Search Result", self.user])  # 发送者
        for i in range(0, len(self.list)):
            message['To'] = formataddr(
                ["{receiver}".format(receiver=self.list[i]),
                 self.list[i]])  # 接收者
        if not self.getClass.getFlag():
            self.flag = "Warning"
        else:
            self.flag = "Success"
        message['Subject'] = '{Date}自动测试报告-{state}'.format(Date=self.time,
                                                           state=self.flag)
        message.attach(MIMEText(self.msg, 'plain', 'utf-8'))
        if not self.getClass.getFlag():
            att = MIMEText(open(self.filename, 'rb').read(), 'base64', 'utf-8')
            att["Content-Type"] = 'application/octet-stream'
            att.add_header('Content-Disposition',
                           'attachment',
                           filename='分析文件.xls')
            message.attach(att)

        try:
            server = smtplib.SMTP_SSL(self.host, 465)
            server.login(self.user, self.pwd)
            server.sendmail(self.user, self.list, message.as_string())
            server.quit()
            self.logger.info("邮件发送成功")
        except Exception as e:
            self.logger.error("{Error}: 无法发送邮件".format(Error=e))
Ejemplo n.º 3
0
 def __init__(self):
     self.workbook = xlwt.Workbook()
     self.time = globalTime.getGlobalTime()
     self.reportname = rootpath + relpath + self.time + '.xls'
     self.logger = writeLog.writeLogger(loglevel=4, logger="writeExcel")
Ejemplo n.º 4
0
 def __init__(self):
     self.time = globalTime.getGlobalTime()
     self.pathName = rootpath + relpath + self.time + '.txt'
     self.file = open(self.pathName, 'w')