def read_json(file_name=None): if file_name == None: file_path = get_Path() + "/Config/user_data.json" else: file_path = get_Path() + file_name with open(file_path, encoding='UTF-8') as f: data = json.load(f) return data
def test01(self): self.caseList = ['user/test01case'] self.test_suite = unittest.TestSuite() self.suite_module = [] self.path = getpathInfo.get_Path() self.caseFile = os.path.join(self.path, "testCase") for case in self.caseList: #从caselist元素组中循环取出case case_name = case.split("/")[ -1] #通过split函数来将aaa/bbb分割字符串,-1取后面,0取前面 print(case_name + ".py") discover = unittest.defaultTestLoader.discover(self.caseFile, pattern=(case_name + '.py'), top_level_dir=None) # discover = unittest.defaultTestLoader.discover(self.caseFile, pattern='*.py', top_level_dir=None) self.suite_module.append( discover) #将discover中取出test_name,使用addTest添加到测试集 print('suite_module:' + str(self.suite_module)) if len(self.suite_module) > 0: #判断suite_module元素组是否存在元素 for suite in self.suite_module: #如果存在,循环取出元素组内容,命名为suite for test_name in suite: #从discover中取出test_name,使用addTest添加到测试集 self.test_suite.addTest(test_name) else: print('else:') return None return self.test_suite #返回测试集
def __init__(self, logger_name='logs…'): global log_path path = getpathInfo.get_Path() log_path = os.path.join(path, 'Log') # 存放log文件的路径 self.logger = logging.getLogger(logger_name) logging.root.setLevel(logging.NOTSET) self.log_file_name = 'logs' # 日志文件的名称 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 __init__(self): # 读取邮件配置信息,初始化参数 read_conf = readConfig.ReadConfig() self.email_service = read_conf.get_email( 'EMAIL_SERVICE') # 从配置文件中读取,邮件服务器类型 self.email_port = read_conf.get_email('EMAIL_PORT') # 从配置文件中读取,邮件服务器端口 self.sender_address = read_conf.get_email( 'SENDER_ADDRESS') # 从配置文件中读取,发件人邮箱地址 self.sender_password = read_conf.get_email( 'SENDER_PASSWORD') # 从配置文件中读取,发件人邮箱授权码 self.receiver_address = read_conf.get_email( 'RECEIVER_ADDRESS') # 从配置文件中读取,收件人邮箱地址 self.file_path = os.path.join(getpathInfo.get_Path(), 'Report', 'report.html') # 获取测试报告路径 # 日志输出 self.logger = logger
class AllTest(): def __init__(self): self.caseFile = os.path.join(path, 'testCase') self.caseListFile = os.path.join(path, 'caselist.txt') self.caseList = [] def set_case_list(self): fb = open(self.caseListFile, 'r') for value in fb.readlines(): value = value.rstrip( '\n' ) # 防止caselist中回车跳行,导致\n != '',会产生空,导致set_case_suite中多出discover if value and not str(value).startswith('#'): self.caseList.append(str(value).replace('\n', '')) fb.close() return self.caseList def set_case_suite(self): caseList = self.set_case_list() # 需要运行的用例名 list = [] for case in caseList: case_name = case # 批量加载用例,参数1为用例存放路径,2为用例文件名 discover = unittest.defaultTestLoader.discover(self.caseFile, pattern=case_name + '.py', top_level_dir=None) list.append(discover) return list # @threads(8) # def run(self, case, n): # result = os.path.join(getpathInfo.get_Path(), 'result', 'report%s.html' % n ) # with open(result, 'wb') as f: # runner = HTMLTestRunner(stream=f, # title='接口测试结果', # description='以下为七猫免费小说接口测试结果清单', # verbosity=2) # 这里面包含写内容了 # runner.run(case) @threads(8) def run(self, case, n): reportpath = os.path.join(getpathInfo.get_Path(), 'result', 'report.html') result = BeautifulReport(case) result.report(reportpath)
def run(self): """ run test :return: """ try: suit = self.set_case_suite() #调用set_case_suite获取test_suite print('try') print(str(suit)) if suit is not None: #判断test_suite是否为空 print('if-suit') fp = open(resultPath, 'wb') #打开result/20181108/report.html测试报告文件,如果不存在就创建 #调用HTMLTestRunner runner = HTMLTestRunner.HTMLTestRunner( stream=fp, title='Test Report', description='Test Description') runner.run(suit) else: print("Have no case to test.") except Exception as ex: print(str(ex)) #log.info(str(ex)) finally: print("*********TEST END*********") #log.info("*********TEST END*********") fp.close() #判断邮件发送的开关 if on_off == 'on': m = SendEmail( username='******', passwd='DWSBWWENHZPEUZKB', recv=['*****@*****.**'], title='接口自动化测试报告', content='接口自动化测试报告', file=os.path.join(getpathInfo.get_Path(), 'result', 'report.html'), # 获取测试报告路径 ssl=True, ) m.send_email() else: print("邮件发送开关配置关闭,请打开开关后可正常自动发送测试报告")
# coding:utf-8 import unittest import getpathInfo import readConfig import os from BeautifulReport import BeautifulReport # 用例路径 # case_path = os.path.join(os.getcwd(), "case", 'blog') # 报告存放路径 report_path = os.path.join(os.getcwd(), "report") path = getpathInfo.get_Path() # 因为这个拿到的是一个list,所以我们应该循环 top_level_dir = r'D:\pycharm\test02\temp202001\case' print('report_path', report_path) print('path111', path) def all_case(): discover_list = [] # 循环 for case_path in path: discover = unittest.defaultTestLoader.discover( case_path, pattern="test*.py", top_level_dir=top_level_dir) print(discover) discover_list.append(discover) return discover_list if __name__ == "__main__":
import getpathInfo import yagmail import time read_conf = readConfig.ReadConfig() subject = read_conf.get_email('subject') # 从配置文件中读取,邮件主题 contents = read_conf.get_email('contents') # 从配置文件中读取,邮件内容 app = str(read_conf.get_email('app')) # 从配置文件中读取,邮件类型 addressee = read_conf.get_email('addressee') # 从配置文件中读取,邮件收件人 cc = read_conf.get_email('cc') # 从配置文件中读取,邮件抄送人 #mail_path = os.path.join(getpathInfo.get_Path(), 'result', 'report.html') # 获取测试报告路径 send = read_conf.get_email('send') # 从配置文件中读取发送方 host = read_conf.get_email('host') # 从配置文件中读取 password = read_conf.get_email('yzm_password') # 从配置文件中读取授权码 now_date = time.strftime('%Y-%m-%d %H_%M_%S') html_report = os.path.join(getpathInfo.get_Path(), 'result', now_date + '-report.html') class send_email(): def outlook(self): print('123321') olook = win32.Dispatch("%s.Application" % app) mail = olook.CreateItem(win32.constants.olMailItem) 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 = """ 执行测试中……
#-*- coding:utf-8 -*- import os # from common import configparser import getpathInfo # 引入我们自己的写的获取路径的类 path = getpathInfo.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 if __name__ == '__main__': # 测试一下,我们读取配置文件的方法是否可用 print('HTTP中的baseurl值为:', ReadConfig().get_http('baseurl'))
def __init__(self): self.path = getpathInfo.get_Path() # 调用实例化 self.config_path = os.path.join(self.path, 'Config', 'Config.ini') # 这句话是在path路径下再加一级 self.config = configparser.ConfigParser() # 调用外部的读取配置文件的方法 self.config.read(self.config_path, encoding='utf-8')
import os import getpathInfo # 自己定义的内部类,该类返回项目的绝对路径 from xlrd import open_workbook #调用读Excel的第三方库xlrd 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, "testFile", 'case', xls_name) file = open_workbook(xlsPath) # 打开用例Excel sheet = file.sheet_by_name(sheet_name) #获得打开Excel的sheet # 获取这个sheet内容行数 nrows = sheet.nrows for i in range(nrows): #根据行数做循环 if sheet.row_values( i )[0] != u'case_name': #如果这个Excel的这个sheet的第i行的第一列不等于case_name那么我们把这行的数据添加到cls[] cls.append(sheet.row_values(i)) return cls
import os #import win32com.client as win32 import datetime import readConfig import getpathInfo from common.Log import logger 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_path = os.path.join(getpathInfo.get_Path(), 'result', 'report.html') #获取测试报告路径 logger = logger class send_email(): def outlook(self): olook = win32.Dispatch("%s.Application" % app) mail = olook.CreateItem(win32.constants.olMailItem) 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 = """ 执行测试中…… 测试已完成!! 生成报告中…… 报告已生成……
# encoding:utf-8 import os import configparser import getpathInfo path = getpathInfo.get_Path() #调用实例化,返回当前文件的绝对路径 config_path = os.path.join(path, 'config.ini') #在path路径下再加一级, ''' 在对配置文件进行读写操作前,我们需要先进行以下两个操作: 1、实例化configParser对象 2、读取config.ini文件 ''' class ReadConfig(): # 实例化ConfigParser对象 config = configparser.ConfigParser() # 读取配置文件 config.read(config_path, 'utf-8') def get_enterprise(self, name): value = self.config.get('ENTERPRISE', name) return value def get_device(self, name): value = self.config.get('DEVICE', name) return value def get_metting_room(self, name):
msg.attach(MIMEText(self.content)) # 邮件正文的内容 msg['Subject'] = self.title # 邮件主题 msg['From'] = self.username # 发送者账号 msg['To'] = ','.join(self.recv) # 接收者账号列表 if self.ssl: self.smtp = smtplib.SMTP_SSL(self.email_host, port=self.ssl_port) else: self.smtp = smtplib.SMTP(self.email_host, port=self.port) # 发送邮件服务器的对象 self.smtp.login(self.username, self.passwd) try: self.smtp.sendmail(self.username, self.recv, msg.as_string()) pass except Exception as e: print('出错了。。', e) else: print('发送成功!') self.smtp.quit() if __name__ == '__main__': m = SendEmail( username='******', passwd='DWSBWWENHZPEUZKB', recv=['*****@*****.**'], title='', content='测试发送邮件', file=os.path.join(getpathInfo.get_Path(), 'result', 'report.html'), # 获取测试报告路径 ssl=True, ) m.send_email()
def write_value(data, fileUrl): data_value = json.dumps(data) with open(get_Path() + fileUrl, "w") as f: f.write(data_value)
import os import configparser import getpathInfo # 引入我们自己的写的获取路径的类 path = getpathInfo.get_Path() # 调用实例化,还记得这个类返回的路径为D:\myfirstauto\testFile config_path = os.path.join(path, 'config.ini') # 这句话是在path路径下再加一级,最后变成D:\myfirstauto\testFile\config.ini 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 if __name__ == '__main__': # 测试一下,我们读取配置文件的方法是否可用 print('HTTP中的baseurl值为:', ReadConfig().get_http('baseurl')) print('EMAIL中的开关on_off值为:', ReadConfig().get_email('on_off'))
import os import configparser import getpathInfo # 引入我们自己的写的获取路径的类 path = getpathInfo.get_Path() # 获取项目路径 config_path = os.path.join(path, 'config.ini') # 获取配置项文件的路径 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_headers(self, name): value = config.get('HEADERS', name) return value def get_params(self, name): value = config.get('PARAMS', name) return value def get_cases(self, name): value = config.get('TESTCASE', name) return value
def empty_fileData(file_name): file_path = get_Path() + file_name with open(file_path, 'r+', encoding='UTF-8') as f: f.truncate()
import os import configparser import getpathInfo # 引入我们自己的写的获取路径的类 path = getpathInfo.get_Path() # 调用实例化,还记得这个类返回的路径为D:\pycharm\test02 config_path = os.path.join( path, 'config.ini') # 这句话是在path路径下再加一级,最后变成D:\pycharm\test02\config.ini config = configparser.ConfigParser() # 调用外部的读取配置文件的方法 config.read(config_path, encoding='utf-8') print(path) 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('outlookEMAIL', name) return value def get_qqemail(self, name): value = config.get('qqEMAIL', name) return value def get_mysql(self, name): # 写好,留以后备用。但是因为我们没有对数据库的操作,所以这个可以屏蔽掉 value = config.get('DATABASE', name) return value def get_login_user(self, name):
import os import win32.client as win32 import datetime import readConfig import getpathInfo from common.Log import logger 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_path = os.path.join(getpathInfo.get_Path(), 'result', 'report.html') # 获取测试报告路径 logger = logger class send_email(): def outlook(self): olook = win32.Dispatch("%s.Application" % app) mail = olook.CreateItem(win32.constants.olMailItem) 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 = """ 执行测试中…… 测试已完成!! 生成报告中…… 报告已生成…… 报告已邮件发送!! """
# coding=utf-8 ''' Created on May 23, 2019 @author: canace ''' import os import ConfigParser as configparser import getpathInfo # 引入我们自己的写的获取路径的类 path = getpathInfo.get_Path( ) # 调用实例化,还记得这个类返回的路径为 /Users/kascend/eclipse-workspace/chushouInterTest config_path = os.path.join( path, 'config.ini' ) # 这句话是在path路径下再加一级,最后变成 /Users/kascend/eclipse-workspace/chushouInterTest/config.ini config = configparser.ConfigParser() # 调用外部的读取配置文件的方法 config.read(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
#!/usr/bin/env python #读取配置文件的方法,并返回文件中内容 import os import configparser import getpathInfo # 引入我们自己的写的获取路径的类 #from getpathInfo import * path = getpathInfo.get_Path() # 调用实例化,还记得这个类返回的路径为F:\pycharm\1211\接口自动化 #https://blog.csdn.net/hao930826/article/details/52232324 这里getpathInfo 里面没有定义class,所以不用括号 config_path = os.path.join(path, 'config.ini') # 这句话是在path路径下再加一级,最后变成F:\pycharm\1211\接口自动化\config.ini 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 if __name__ == '__main__': # 测试一下,我们读取配置文件的方法是否可用
def __init__(self): self.path = getpathInfo.get_Path() # 拿到该项目所在的绝对路径
import os import configparser import getpathInfo # 引入我们自己的写的获取路径的类 path = getpathInfo.get_Path( ) #调用实例化,还记得这个类返回的路径为C:\Users\songlihui\PycharmProjects\dkxinterfaceTest config_path = os.path.join( path, 'config.ini' ) #这句话是在path路径下再加一级,最后变成C:\Users\songlihui\PycharmProjects\dkxinterfaceTest\config.ini 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 if __name__ == '__main__': # 测试一下,我们读取配置文件的方法是否可用 print('HTTP中的baseurl值为:', ReadConfig().get_http('baseurl')) print('EMAIL中的开关on_off值为:', ReadConfig().get_email('on_off'))
import os import configparser import getpathInfo # 引用我们自己写的获取路径的类 path = getpathInfo.get_Path( ) #调用实例化,路径:/Users/nietingting/Developer/python/practice_2020/device_updates/testFile 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 if __name__ == '__main__': print('HTTP中的baseurl值为: ', ReadConfig().get_http('baseurl')) print('EMAIL中的开关on_off值为: ', ReadConfig().get_email('on_off'))
__author__ = '791399137' import os import common.HTMLTestRunner as HTMLTestRunner import getpathInfo import unittest import readConfig from common.configEmail import send_email from apscheduler.schedulers.blocking import BlockingScheduler import pythoncom import common.Log send_mail = send_email() path = getpathInfo.get_Path() report_path = os.path.join(path, 'result') on_off = readConfig.ReadConfig().get_email('on_off') #log = common.Log.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(path, "testCase") #真正的测试断言文件路径 self.caseList = [] # log.info('resultPath',resultPath) # log.info('caseListFile',self.caseListFile) # log.info('caseList',self.caseList)
import os import configparser import getpathInfo # 引入我们自己的写的获取路径的类 path = getpathInfo.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 if __name__ == '__main__': # 测试一下,我们读取配置文件的方法是否可用 print('HTTP中的baseurl值为:', ReadConfig().get_http('baseurl')) print('EMAIL中的开关on_off值为:', ReadConfig().get_email('on_off'))