def __init__(self):  #初始化一些参数和数据
     global resultPath
     #resultPath = os.path.join(report_path, "report.html")#result/report.html
     # resultPath = getpathInfo.MakePath().get_resultpath('result')
     resultPath = getpathInfo.MakePath().set_reportPath(
         'result', getpathInfo.nowTime + '.html')
     self.caseListFile = os.path.join(path,
                                      "caselist.txt")  #配置执行哪些测试文件的配置文件路径
     self.caseFile = os.path.join(path, "testCase")  #真正的测试断言文件路径
     self.caseList = []
     log.info('resultPath' + resultPath)  #将resultPath的值输入到日志,方便定位查看问题
     log.info('caseListFile' + self.caseListFile)  #同理
     log.info('caseList' + str(self.caseList))  #同理
import os
import common.HTMLTestRunner as HTMLTestRunner
import getpathInfo
import unittest
import readConfig
import common.Log
from common.emailDemo import sendReport
from common.configEmail import Email
from common import Log
# send_mail = sendReport().send_email()

path = getpathInfo.MakePath().get_Path()
# on_off = readConfig.ReadConfig().get_email('on_off')
log = Log.MyLog().get_log().get_logger()
# resultPath = getpathInfo.set_reportPath()


class AllTest:  #定义一个类AllTest
    def __init__(self):  #初始化一些参数和数据
        global resultPath
        #resultPath = os.path.join(report_path, "report.html")#result/report.html
        # resultPath = getpathInfo.MakePath().get_resultpath('result')
        resultPath = getpathInfo.MakePath().set_reportPath(
            'result', getpathInfo.nowTime + '.html')
        self.caseListFile = os.path.join(path,
                                         "caselist.txt")  #配置执行哪些测试文件的配置文件路径
        self.caseFile = os.path.join(path, "testCase")  #真正的测试断言文件路径
        self.caseList = []
        log.info('resultPath' + resultPath)  #将resultPath的值输入到日志,方便定位查看问题
        log.info('caseListFile' + self.caseListFile)  #同理
        log.info('caseList' + str(self.caseList))  #同理
            return True
        else:
            return False

    def config_html(self, resultPath):
        f = open(resultPath,
                 'rb')  # HTML文件默认和当前文件在同一路径下,若不在同一路径下,需要指定要发送的HTML文件的路径
        mail_body = f.read()
        f.close()
        message = MIMEText(mail_body, 'html', 'utf-8')
        self.msg.attach(message)

    def send_email(self, resultPath):
        self.config_header()
        self.config_html(resultPath)
        try:
            smtp = smtplib.SMTP()
            smtp.connect(host)
            smtp.login(user, password)
            smtp.sendmail(sender, self.receiver, self.msg.as_string())
            smtp.quit()
            print('The test report has send to developer by email')
            self.logger.info("The test report has send to developer by email.")
        except Exception as ex:
            self.logger.error(str(ex))


if __name__ == "__main__":
    reportpath1 = getpathInfo.MakePath().set_reportPath(
        'result', nowTime + '.html')
    Email().send_email(reportpath1)
import os
import configparser
import getpathInfo

path = getpathInfo.MakePath().get_Path()#调用实例化,记得这个类返回的路径为
config_path = os.path.join(path, 'config.ini')#这句话是在path路径下再加一级
config = configparser.ConfigParser()#调用外部的读取配置文件的方法
config.read(config_path, encoding='utf-8')

class ReadConfig():
    def get_http(self, name):
        value = config.get('HTTP', name)
        return value
    def get_email(self, name):
        value = config.get('EMAIL', name)
        return value
    def get_mysql(self, name): #写好,留以后备用。但是因为我们没有对数据库的操作,所以这个可以屏蔽掉
        value = config.get('DATABASE', name)
        return value
    def get_userData(self,token):
        token = config.get('USER', token)
        return token
    def set_value(self, option, value):
        try:
            config.set('USER', option, value)
            config.write(open(config_path, "w"))
        except Exception as e:
            print(e)

if __name__ == '__main__':#测试一下,我们读取配置文件的方法是否可用
    print('HTTP中的baseurl值为:', ReadConfig().get_http('baseurl'))
Example #5
0
import os
import logging
import threading
from logging.handlers import TimedRotatingFileHandler
import getpathInfo
import time

path = getpathInfo.MakePath().get_resultpath('result')


#log_path = os.path.join(path, 'result')  # 存放log文件的路径
class Logger(object):
    def __init__(self, logger_name='logs'):
        self.logger = logging.getLogger(logger_name)
        logging.root.setLevel(logging.NOTSET)
        self.log_file_name = 'logs.' + time.strftime(
            "%Y-%m-%d", time.localtime())  # 日志文件的名称

        self.backup_count = 5  # 最多存放日志的数量
        # 日志输出级别
        self.console_output_level = 'WARNING'
        self.file_output_level = 'DEBUG'
        # 日志输出格式
        self.formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')

    def get_logger(self):
        """在logger中添加日志句柄并返回,如果logger已有句柄,则直接返回"""
        if not self.logger.handlers:  # 避免重复日志
            console_handler = logging.StreamHandler()
            console_handler.setFormatter(self.formatter)