def __init__(self): global logPath, resultPath, proDir path = getpathInfo.get_path() #获取teseFile文件路径 proDir = os.path.dirname(path) #获取interface路径,也就是项目路径 # print(proDir) resultPath = os.path.join(proDir, "result") #把得到的结果result文件放入interface路径下 # print(resultPath) if not os.path.exists(resultPath): os.mkdir(resultPath) logPath = os.path.join( resultPath, str(datetime.now().strftime("%Y%m%d"))) #%Y%m%d%H%M%S # print(logPath) if not os.path.exists(logPath): os.mkdir(logPath) self.logger = logging.getLogger() #设置日志级别info以上的 self.logger.setLevel(logging.INFO) # defined handler,输出日志文件名为output.log,FileHandler用于写入日志文件 handler = logging.FileHandler(os.path.join(logPath, "output.log")) # defined formatter,按照年月日时分秒输出格式,formatter格式器 formatter = logging.Formatter( '%(asctime)s - %(name)s - %(levelname)s - %(message)s') #给handler添加formatter handler.setFormatter(formatter) # 给logger添加handler,也就是handler这个规则, 把文件日志对象添加到日志处理器logger中 self.logger.addHandler(handler)
class ReadConfig: path = get_path() config_path = os.path.join(path, 'config.ini') # 配置文件的路径 config = configparser.ConfigParser() config.read(config_path, encoding='utf-8') # 读取config.ini配置文件内容 def get_http(self, name): try: return self.config.get('HTTP', name) except Exception as e: print(e, '请检查配置名称') def get_email(self, name): try: return self.config.get('EMAIL', name) except Exception as e: print(e, '请检查配置名称') def get_database(self, name): try: return self.config.get('DATABASE', name) except Exception as e: print(e, '请检查配置名称') def get_ml(self, name): try: return self.config.get('ML', name) except Exception as e: print(e, '请j检查配置情况')
def logpath(self): pathdir = get_path() # 获取文件上一级 pathshang = os.path.dirname(pathdir) current_time = time.strftime('%Y%m%d%H%M', time.localtime(time.time())) # 返回当前时间 curren_path = os.path.join(pathshang, 'result') path3 = '' # 在该路径下新建下级目录 new_name = path3.join(curren_path) + '/logsss/' print(new_name + "==========") dir_time = time.strftime('%Y%m%d', time.localtime( time.time())) # 返回当前时间的年月日作为目录名称 isExists = os.path.exists(new_name + dir_time) # 判断该目录是否存在 if not isExists: os.makedirs(new_name + dir_time) print(new_name + dir_time + "目录创建成功") else: # 如果目录存在则不创建,并提示目录已存在 print(new_name + "目录 %s 已存在" % dir_time) log_name = new_name + dir_time + '/' + current_time + '.log' # 定义日志文件的路径以及名称 return log_name
class ReadExcle: path = get_path() def __init__(self, excle_name, sheet_name): self.sheet_name = sheet_name self.excle_name = excle_name # 获取文件路径 self.excle_path = os.path.join( self.path, 'CaseExcles', excle_name) # 文件定位为当前path路径/CaseExcles/exclename self.file = open_workbook(self.excle_path) # 打开excle文件 self.sheet = self.file.sheet_by_name(sheet_name) # 打开excle指定的sheet # 获取sheet的行、列数 self.nrows = self.sheet.nrows self.ncols = self.sheet.ncols def get_excle(self): # 获取excle的内容,并将数据以列表的形式读取出来,不包括casename这一行 cls = [] for i in range(self.nrows): # 如果这个Excel的这个sheet的第i行的第一列不等于case_name那么我们把这行的数据添加到cls[] if self.sheet.row_values(i)[0] != 'case_name': cls.append(self.sheet.row_values(i)) return cls
import os from xlrd import open_workbook from testFile.getpathInfo import get_path # 拿到该项目所在的绝对路径 path = get_path() print(path) print("-----------------------------") class readExcel(object): # 根据开始行 结束行 取值 @classmethod def get_xlsA(self, xls_name, sheet_name, strnum_hang, endnum_hang): # xls_name填写用例的Excel名称 sheet_name该Excel的sheet名称 cls = [] # 获取用例文件路径 xlsPath = os.path.join(path, 'case', xls_name) file = open_workbook(xlsPath) # 打开用例Excel sheet = file.sheet_by_name(sheet_name) # 获得打开Excel的sheet # 获取这个sheet内容行数 row_Num = sheet.nrows col_Num = sheet.ncols key = sheet.row_values(0) # 这是第一行数据,作为字典的key值 if row_Num <= 1: print("没数据") else: j = 1 for i in range(strnum_hang, endnum_hang): d = {}
import os import win32com.client as win32 import datetime import testFile.getpathInfo as getpathInfo import testFile.readConfig as readConfig read_conf = readConfig.ReadConfig() subject = read_conf.get_email('subject') # 从配置文件中读取,邮件主题 app = str(read_conf.get_email('app')) # 从配置文件中读取,邮件类型 addressee = read_conf.get_email('addressee') # 从配置文件中读取,邮件收件人 cc = read_conf.get_email('cc') # 从配置文件中读取,邮件抄送人 mail_pathA = os.path.join('D:/python_project/zidonghua', 'result', 'report.html') # 获取测试报告路径 mail_path = mail_pathA.replace('\\', '/') print(mail_path + "------") print(getpathInfo.get_path()) class send_email(): def outlook(self): olook = win32.Dispatch("%s.Application" % app) mail = olook.CreateItem(0) mail.To = addressee # 收件人 # mail.CC = cc # 抄送 mail.Subject = str( datetime.datetime.now())[0:19] + '%s' % subject # 邮件主题 mail.Attachments.Add(mail_path, 1, 1, "myFile") content = """ 执行测试中……AAAA 测试已完成!! 生成报告中……
#读取excel方法 import os from testFile import getpathInfo from xlrd import open_workbook path = getpathInfo.get_path() class readExcel(): def get_xls(self, xls_name, sheet_name): #xls_name 填写用例的Excel名称 ,sheet_name该Excel的sheet名称 cls = [] #获取用例路径 xlsPath = os.path.join(path, 'case', xls_name) book = open_workbook(xlsPath) #获得打开Excel的sheet #获取sheet内容行数 sheet = book.sheet_by_name(sheet_name) nrows = sheet.nrows #print (nrows) for i in range(nrows): #根据行数做循环 #如果这个Excel的这个sheet第i行的第一列 != case_name,则将该行添加至cls[] #table.row_values(rowx) 返回rowx行所有单元格数据list #sheet.row_values(i)[j] 返回i+1行第j+1个单元格数据(j从0开始计数) #print(f"start for loop:{i}") #print(sheet.row_values(i)[0]) if sheet.row_values(i)[0] != 'case_name': # print (f"{i}:",sheet.row_values(i)) cls.append(sheet.row_values(i)) #print(cls) return cls
logger = log.get_logger() A = readConfig.ReadConfig() #实例化对象,用来读取配置文件里的邮箱有关的内容 username = A.get_email("mail_user") passwd = A.get_email("mail_pass") recv = A.get_email("receiver") print(recv) title = A.get_email("subject") content="测试发送邮件 " \ "接口自动化测试" email_host = A.get_email("mail_host") port = A.get_email("mail_port") ssl_port = A.get_email("ssl_port") #获取输出的log文件的位置 path1 = getpathInfo.get_path() #获取Testfile文件路径 path = os.path.dirname(getpathInfo.get_path()) #获取interfacetest文件路径 result_Path = os.path.join( path, "result") #把得到的结果resultw文件放入interfacetest路径下report_path%Y%m%d%H%M%S report_path = os.path.join( result_Path, str(datetime.datetime.now().strftime( "%Y%m%d"))) ##获取interfacetest文件路径result/20200824 file1 = report_path + "\output.log" file2 = report_path + "\\report.html" file = [file1, file2] # file=report_path # print(file) m = SendEmail(username, passwd, recv, title,
import os import common.HTMLTestRunner as HTMLTestRunner import unittest from common.configEmail import send_email import common.log from testFile import getpathInfo, readConfig path = getpathInfo.get_path(__file__) absolute_path = '../../babTest' report_path = os.path.join(absolute_path, 'result') on_off = readConfig.ReadConfig().get_email('on_off') log = common.log.Logger().get_logger() class AllTest: #定义一个类AllTest def __init__(self): #初始化一些参数和数据 global resultPath resultPath = os.path.join(report_path, "report.html") #result/report.html self.caseListFile = os.path.join(path, "caselist.txt") #配置执行哪些测试文件的配置文件路径 self.caseFile = os.path.join(absolute_path, "testcase") #真正的测试断言文件路径 self.caseList = [] log.info('resultPath' + resultPath) #将resultPath的值输入到日志,方便定位查看问题 log.info('caseListFile' + self.caseListFile) #同理 log.info('caseList' + str(self.caseList)) #同理 def set_case_list(self): """ 读取caselist.txt文件中的用例名称,并添加到caselist元素组 :return:
import os import configparser from testFile.getpathInfo import get_path # 引入我们自己写的获取路径的类 path = get_path() # 返回项目根路径 config_path = os.path.join(path, 'testFile', 'config.ini') # 获取配置文件的绝对路径 config = configparser.ConfigParser() # 调用外部读取配置文件的方式 config.read(config_path, encoding='utf-8') # 还要读取配置文件 print(config_path) # 取值类似键值对的方式 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 if __name__ == '__main__': # 测试一下,我们读取配置文件的方法是否可用 print('HTTP中的baseurl值为:', ReadConfig().get_http('baseurl')) print('EMAIL中的开关on_off值为:', ReadConfig().get_email('on_off'))