def File_to_Zip(self, reportpath, Zipfilename): """ :param reportpath: 源文件路径 :param Zipfilename: 压缩文件路径 :return: 打包好的文件路径 """ reportZip = os.path.join(Zipfilename, 'report.rar') filelist = [] if os.path.isfile(reportpath): filelist.append(reportpath) else: for root, dirs, files in os.walk(reportpath): for dir in dirs: filelist.append(os.path.join(root, dir)) for name in files: filelist.append(os.path.join(root, name)) zf = zipfile.ZipFile(reportZip, "w", zipfile.zlib.DEFLATED) for tar in filelist: arcname = tar[len(reportpath):] # print(arcname) zf.write(tar, arcname) zf.close() logconf().info('测试报告打包完成{0}'.format(reportZip)) return reportZip
def test_add(self): logconf().info('计算{0}+{1}'.format(self.num1, self.num2)) with allure.step('输入数字'): allure.attach('输入第一个数字:', '{0}'.format(self.num1)) allure.attach('输入第二个数字:', '{0}'.format(self.num2)) # logconf().error('错误:{0}+{1}'.format(num1,num2)) assert self.add() == 6
def testcase_02(self): with allure.step('搜索python'): allure.attach('输入框输入', 'python') logconf().info('执行输入关键字') self.home_page.input('首页', By.ID, 'input').send_keys('python') allure.attach('点击按钮', '搜索') logconf().info('执行点击按钮') self.home_page.click('首页', By.ID, 'search-button').click()
def Send_email(self, file): """ :param file: 压缩文件 :return: 邮件发送状态 """ stress_body = Consts.STRESS_LIST reslut_body = Consts.RESULT_LIST mail_host = self.config.smtpserver mail_user = self.config.username mail_pwd = self.config.password sender = self.config.sender receivers = [self.config.receiver] mail_title = '主题:测试报告{0}'.format(time.strftime('%Y-%m-%d_%H_%M_%S')) # print(file) body = 'Hi,all;\n本次接口自动化测试报告如下:\n 接口响应时间集(ms):{0}\n 接口运行结果集:{1}'.format(stress_body, reslut_body) message = MIMEMultipart() message['From'] = "{}".format(sender) message['To'] = ','.join(receivers) message['Subject'] = Header(mail_title, 'utf-8').encode() message.attach( MIMEText(body + "\n此附件为自动开发,请勿回复/转发;\n查看请解压附件,并使用win10自带浏览器Microsoft Edge \ 打开index.html文件,即可在线观看;或使用 pycharm打开index.html 选择谷歌浏览器。" , 'plain', 'utf-8')) with open(file, 'rb')as f: ctype, encoding = mimetypes.guess_type(file) if ctype is None or encoding is not None: ctype = 'application/x-zip-compressed' maintype, subtype = ctype.split('/', 1) # message = MIMEText(mail_body, 'text', 'utf-8') mime = MIMEBase(maintype, subtype, filename='测试报告.rar') mime.add_header('Content-Disposition', 'attachment', filename='测试报告.rar') mime.add_header('Content-ID', '<0>') mime.add_header('X-Attachment-Id', '0') mime.set_payload(f.read()) encoders.encode_base64(mime) message.attach(mime) try: smtp = smtplib.SMTP_SSL(mail_host) smtp.login(mail_user, mail_pwd) smtp.sendmail(sender, receivers, message.as_string()) # logconf().info('发送测试报告邮件完成') smtp.close() except smtplib.SMTPException: logconf().error('邮件发送失败!!!') raise
def run(times): conf = Config.Config() log = logconf.logconf() log.info('初始化配置文件,path= ' + conf.conf_path) shell = Shell.Shell() root_dir = os.path.dirname(__file__) # print(root_dir) root_path = root_dir.replace(root_dir.split('\\')[-1], 'report') + r'\allure-report\{0}'.format(times) # print(root_path) log.info('创建按时间生成测试报告文件夹') os.mkdir(root_path) xml_dir = root_path + r'\xml' html_dir = root_path + r'\html' os.mkdir(xml_dir) os.mkdir(html_dir) args = ['-s', '-q', '--alluredir', xml_dir] # pytest.main(['-s', 'test_search.py', '--alluredir', xmldir] pytest.main(args) cmd = 'allure generate {0} -o {1} --clean'.format(xml_dir, html_dir) # os.system('allure generate {0} -o {1} --clean'.format(xml_dir, html_dir)) # print(root_dir) try: shell.invoke(cmd) except Exception: log.error('执行测试用例失败,请检查环境配置') raise try: mail = SendReport.SendReportEmail(root_dir.replace(root_dir.split('\\')[-1], 'report') + r'\allure-report') mail.Find_File() except Exception as e: log.error('发送邮件失败,请检查邮件配置') raise
def __init__(self): """ 初始化 """ self.config = ConfigParser() self.log = logconf() self.conf_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), 'config.ini') if not os.path.exists(self.conf_path): raise FileNotFoundError('请确保配置文件存在!') self.config.read(self.conf_path, encoding='utf-8') self.tester_debug = self.get_conf(Config.TITLE_DEBUG, Config.VALUE_TESTER) self.environment_debug = self.get_conf(Config.TITLE_DEBUG, Config.VALUE_ENVIRONMENT) self.versionCode_debug = self.get_conf(Config.TITLE_DEBUG, Config.VALUE_VERSION_CODE) self.host_debug = self.get_conf(Config.TITLE_DEBUG, Config.VALUE_HOST) self.verifyCodeUrl_debug = self.get_conf(Config.TITLE_DEBUG, Config.VALUE_VERIFYCODEURL) self.loginUrl_debug = self.get_conf(Config.TITLE_DEBUG, Config.VALUE_LOGINURL) self.smtpserver = self.get_conf(Config.TITLE_EMAIL, Config.VALUE_SMTP_SERVER) self.sender = self.get_conf(Config.TITLE_EMAIL, Config.VALUE_SENDER) self.receiver = self.get_conf(Config.TITLE_EMAIL, Config.VALUE_RECEIVER) self.username = self.get_conf(Config.TITLE_EMAIL, Config.VALUE_USERNAME) self.password = self.get_conf(Config.TITLE_EMAIL, Config.VALUE_PASSWORD)
def __init__(self, env): """ :param env: 环境变量 """ self.config = Config.Config() self.log = logconf() self.param = params.Basic() self.env = env
def Find_File(self): """ :return: 添加文件打包 """ # for root,dirs,files in os.walk(path): # print(files) files = os.listdir(self.path) Newdir = files[-1] if Newdir: self.path = os.path.join(self.path, Newdir) # print(self.path) reportpath = self.path + '\\html' # for _ in os.listdir(reportpath): # if 'index.html' in _: # # filepath = reportpath + '\\' + _ # print(reportpath) self.Send_email(self.File_to_Zip(reportpath, self.path)) else: # print('未找到目标源文件:path={0}'.format(self.path)) logconf().error('本次未找到测试文件:{0}'.format(self.path))
def __init__(self): self.log = logconf()
def setUp(self, url="http://www.so.com"): logconf().info('打开网页:{}'.format(url)) self.home_page.get(url)
def tearDownClass(cls): logconf().info('测试结束,关闭浏览器') cls.driver.close()
def setUpClass(cls): cls.driver = GetDriver("chrome").driver cls.home_page = HomePage(cls.driver) logconf().info('初始化Chrome浏览器对象')
def tearDown(self): logconf().info('浏览器休眠1s') time.sleep(1) # self.driver.close() pass
with allure.step('搜索python'): allure.attach('输入框输入', 'python') logconf().info('执行输入关键字') self.home_page.input('首页', By.ID, 'input').send_keys('python') allure.attach('点击按钮', '搜索') logconf().info('执行点击按钮') self.home_page.click('首页', By.ID, 'search-button').click() if __name__ == '__main__': rootdir = os.getcwd() # print(rootdir) time = time.strftime('%Y-%m-%d_%H_%M_%S') rootpath = rootdir.replace(rootdir.split('\\')[-1], 'report') + r'\allure-report\{0}'.format(time) # print(rootpath) logconf('创建按时间生成测试报告文件夹') os.mkdir(rootpath) xmldir = rootpath + r'\xml' htmldir = rootpath + r'\html' os.mkdir(xmldir) os.mkdir(htmldir) logconf().info('执行pytest.main方法') pytest.main(['-s', 'test_search.py', '--alluredir', xmldir]) logconf().info('美化测试报告') os.system('allure generate {0} -o {1} --clean'.format(xmldir, htmldir)) SendReportEmail( rootdir.replace(rootdir.split('\\')[-1], 'report') + r'\allure-report').Find_File()
# coding = utf-8 """ Author:micheryu Date:2020/3/24 Motto: 能用脑,就别动手~ """ import os from log.logconf import logconf from Params import tools log = logconf() path_dir = str( os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) # print(path_dir) def get_parameter(name): data = tools.GetPages().get_page_list() param = data[name] return param class Basic: log.info('解析yaml,Path: ' + path_dir + r'/Params/Params/Basic.yml') params = get_parameter('Basic') url = []