def getCode(): if request.method == "POST": emailAddress = request.form.get("userName") code = Code().gen_code() session['code'] = code mail = Mail() msg = mail.send(emailAddress, code) return msg
def send_mail(title): try: server = Server(SMTP_SERVER, PASSWORD, FROM_ADDR) if server.connet(): mail = Mail(server, TO_ADDRS) mail.mail_title = title mail.mail_content = '股票提醒消息来了' mail.send_mail() server.close() print('mail send out!') except Exception as e: print(e) print('mail send failed!')
#coding:utf8 from lxml import etree from common.mail import Mail mail = Mail() f = open("D:\\1.html", "r", encoding="utf8") #读取文件 f = f.read() #把文件内容转化为字符串 html = etree.HTML(f) #把字符串转化为可处理的格式 # 读取成功率(完全符合xpath语法) result2 = html.xpath("//*[@class='details']/tr[2]/td[3]") if result2[0].text == "100.00%": print("pass") else: mail.mail_info['filepaths'] = ["D:"] mail.mail_info['filenames'] = ["D:\\1.html"] mail.send()
writer.set_sheet(sheetname[0]) # 获取结束时间 t = str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) # 设置shet页 # 写入结果文件 writer.write(1, 4, t) writer.save_close() res = Res() # result = res.get_res('./lib/result-HTTP接口用例.xls') result = res.get_res('./lib/result-' + casename) print(result) mail = Mail() mail.mail_info['mail_subject'] = result['title'] mailtxt = config.config['mailtxt'] mailtxt = mailtxt.replace('title', result['title']) mailtxt = mailtxt.replace('status', result['status']) mailtxt = mailtxt.replace('runtype', result['runtype']) mailtxt = mailtxt.replace('passrate', result['passrate']) mailtxt = mailtxt.replace('starttime', result['starttime']) mailtxt = mailtxt.replace('casecount', result['casecount']) mailtxt = mailtxt.replace('endtime', result['endtime']) if result['status'] == 'Fail': mailtxt = mailtxt.replace('#00d800', 'red') config.config['mailtxt'] = mailtxt
logger.info(config.config) # 初始化数据库 mysql = Mysql() mysql.init_mysql('./conf/userinfo.sql') # 结果统计 res = Res() details = res.get_res('./lib/result-%s.xls' % casename) r = res.get_groups('./lib/result-%s.xls' % casename) # 关闭jvm shutdown() # 邮件处理 mail = Mail() htmlmodule = Txt('./conf/' + config.config['mailtxt']) html = htmlmodule.read()[0] # 对模块文本进行处理 # 替换总体统计信息 sumlist = ['status', 'passrate', 'starttime', 'endtime'] for s in sumlist: html = html.replace(s, details[s]) # 生成HTML的一行内容 alltrs = '' for s in r: tr = '<tr><td width="100" height="28" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">分组信息</td><td width="80" height="28" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">用例总数</td><td width="80" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">通过数</td><td width="80" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">状态</td></tr>' tr = tr.replace('分组信息', str(s[0])) tr = tr.replace('用例总数', str(s[1])) tr = tr.replace('通过数', str(s[2]))
line = reader.readline() runCases(line, runType) end_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) writer.set_sheet(sheetname[0]) writer.write(1, 4, str(end_time)) writer.save_close() r = res.get_res(result + ctime + '.xls') print(r) config.get_config('./lib/conf/conf.properties') mysql = Mysql() mysql.init_mysql( 'C:\\Users\\leez\\Documents\\Navicat\\MySQL\\servers\\112\\test_project\\userinfo.sql' ) mail = Mail() mail.mail_info['mail_subject'] = r['title'] + '_' + ctime mail.mail_info['filepaths'] = [result + ctime + '.xls'] mail.mail_info['filenames'] = ['result-' + name + '用例' + ctime + '.xls'] config.config['mail_html'] = config.config['mail_html'].replace( 'title', r['title']) if r['status'] == 'Fail': config.config['mail_html'] = config.config['mail_html'].replace( 'color: #00d800;">status', 'color: #FF0000;">status') config.config['mail_html'] = config.config['mail_html'].replace( 'status', r['status']) else: config.config['mail_html'] = config.config['mail_html'].replace( 'status', r['status']) config.config['mail_html'] = config.config['mail_html'].replace( 'runtype', r['runtype'])
# else: # logger.error('非法用例路径') config.get_config(path + '/lib/conf/conf.txt') # logger.info(config.config) # 跑测试用例前将数据库还原 # mysql = Mysql() # mysql.init_mysql(path + '/lib/conf/userinfo.sql') runCases() res = Res() r = res.get_res(resultpath) text = config.config['mailtext'] # 根据执行结果的pass与fail对字体颜色进行设置 if r['status'] == 'Pass': text = text.replace('status', r['status']) else: text = text.replace( '<font style="font-weight: bold;font-size: 14px;color: #00d800;">status</font>', '<font style="font-weight: bold;font-size: 14px;color: red;">Fail</font>' ) text = text.replace('passrate', r['passrate'] + '%') text = text.replace('casecount', r['casecount']) text = text.replace('title', r['title']) text = text.replace('runtype', r['runtype']) text = text.replace('starttime', r['starttime']) text = text.replace('endtime', r['endtime']) mail = Mail() mail.send(text, resultpath)
logger.info(line) try: runCases(http, line) except Exception as e: logger.exception(e) writer.set_sheet(sheetname[0]) t = str(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) writer.write(1, 4, t) writer.save_close() res = Res() result = res.get_res('./lib/结果-' + casename) mail = Mail() mail.mail_info['mail_subject'] = result['title'] mailtext = config.config['mailtxt'] mailtext = mailtext.replace('title',result['title']) mailtext = mailtext.replace('status', result['status']) mailtext = mailtext.replace('runtype', result['runtype']) mailtext = mailtext.replace('passrate', result['passrate']) mailtext = mailtext.replace('starttime', result['starttime']) mailtext = mailtext.replace('casecount', result['casecount']) mailtext = mailtext.replace('endtime', result['endtime']) if result['status'] == 'Fail': mailtext = mailtext.replace('#00d800', 'red') # 附件的路径,如果有多个就用,分割 mail.mail_info['filepaths'] = ['./lib/结果-' + casename] mail.mail_info['filenames'] = ['结果-' + casename]
#执行用例 config.get_config(path + '/lib/conf/conf.txt') #还原数据库 mysql = Mysql() mysql.init_mysql(path + '/lib/mysql/userinfo.sql') StartTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') runCase() EndTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') res = Res() r = res.get_res(resultpath) #邮件设置 text = config.config['mailtext'] if r['status'] == 'PASS': text = text.replace('status', r['status']) else: text = text.replace( '<font style="font-weight: bold;font-size: 14px;color: #00d800;">status</font>', '<font style="font-weight: bold;font-size: 14px;color: red;">Fail</font>' ) text = text.replace('passrate', r['passrate']) text = text.replace('casecount', r['casecount']) text = text.replace('runtype', r['runtype']) text = text.replace('title', r['title']) text = text.replace('starttime', r['StartTime']) text = text.replace('endtime', r['EndTime']) config.get_config('./lib/conf/conf.txt') mail = Mail() mail.send(text)
with open(report_path, 'wb') as report: runner = HTMLTestRunner(stream=report, title=report_title, description="") runner.run(testsuite) return report_path if __name__ == "__main__": if testcaseid_list or mixcase_list: generate_testcases(testcaseid_list) generate_mix_testcase(mixcase_list) testsuite = generate_testsuite(testcaseid_list, mixid_list) logger.info("【开始执行用例】") report_path = run(testsuite) logger.info("【结束执行用例】") logger.info("") flag = int(readconfig.if_send) if flag: logger.info("【正在发送邮件报告】") mail = Mail(readconfig.email_host, readconfig.email_user, readconfig.email_psw) content = "自动化测试已结束,请查收测试报告" mail.send_mail(readconfig.Receivers, '自动化测试报告', content, report_path) logger.info("【邮件报告发送结束】") logger.info("") else: logger.info("缺少用例数据,请指定或者添加相应的用例数据") logger.info("")
datadriven.writer.save_close() # # 得到报告数据 res = Res() r = res.get_res(resultpath) # 读取配置文件 config.get_config('./conf/conf.properties') # logger.info(config.config) # logger.info(config.config['mail']) # 修改邮件数据 html = config.config['mailtxt'] # 读取报告模板 html = html.replace('title', r['title']) # 替换模板中的数据 html = html.replace('runtype', r['runtype']) html = html.replace('passrate', r['passrate']) html = html.replace('status', r['status']) html = html.replace('casecount', r['casecount']) html = html.replace('starttime', r['starttime']) html = html.replace('endtime', r['endtime']) if r['status'] == 'Fail': html = html.replace('#00d800', 'red') # 根据结果决定是否发送邮件 if r['status'] == 'Fail': logger.info("开始发送邮件") mail = Mail() # mail.send(html) logger.info("结束用例")
# 读取配置文件 config.get_config('./conf/conf.properties') config.config["mailtitle"] = r['title'] # 修改邮件数据 html = config.config['mailtxt'] # 读取报告模板 html = html.replace('title', r['title']) # 替换模板中的数据 html = html.replace('runtype', r['runtype']) html = html.replace('passrate', r['passrate']) html = html.replace('status', r['status']) html = html.replace('casecount', r['casecount']) html = html.replace('starttime', r['starttime']) html = html.replace('endtime', r['endtime']) html = html.replace('successcount', r['passcount']) html = html.replace('failcount', r['failcount']) logger.info(r['status']) if r['status'] != 'Pass': html = html.replace('#00d800', 'red') #根据结果决定是否发送邮件 logger.info("开始发送邮件") mail = Mail() mail.mail_info['filepaths'] = [resultpath] mail.mail_info['filenames'] = [result_name] # mail.send(html) logger.info("结束用例")
if __name__ == "__main__": docker_manager = DockerManager() try: opts, args = getopt.getopt(sys.argv[1:], 'prd', ['provision', 'retire', 'delete']) except getopt.error, msg: print msg sys.exit(2) print args for o, a in opts: # Provision Docker Service if o in ("-p", "--provision"): ret = docker_manager.docker_provisioning(args) mail = Mail() mail.mail_dockerservice(args[2].lower(), args[3].lower(), ret) if not ret[0]: logging.error(ret[1]) sys.exit(1) # Retire Docker Service, run after delete service by shell elif o in ("-r", "--retire"): ret = docker_manager.docker_retire_service(args) mail = Mail() mail.mail_retire_dockerservice(args[0], args[1].lower(), ret) if not ret[0]: logging.error(ret[1]) sys.exit(3)
'<td height="28" bgcolor="#FFFFFF" align="center" style="border:1px solid #ccc;">status</td>', '<td height="28" bgcolor="#FFFFFF" align="center" style="border:1px solid #ccc;color:blue;">Pass</td>' ) elif summary[key] == "Fail": html = html.replace( '<td height="28" bgcolor="#FFFFFF" align="center" style="border:1px solid #ccc;">status</td>', '<td height="28" bgcolor="#FFFFFF" align="center" style="border:1px solid #ccc;color:red;">Fail</td>' ) else: html = html.replace(key, summary[key]) # 获取分组显示 tr = '<tr><td width="35" height="28" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">自动化类型</td><td width="70" height="28" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">用例表名</td><td width="100" height="28" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">分组信息</td><td width="50" height="28" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">用例总数</td><td width="50" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">通过数</td><td width="50" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">状态</td></tr>' trs = "" for i in range(len(groups)): try: tmp = tr.replace('自动化类型', str(groups[i][0])) tmp = tmp.replace('用例表名', str(groups[i][1])) tmp = tmp.replace('分组信息', str(groups[i][2])) tmp = tmp.replace('用例总数', str(groups[i][3])) tmp = tmp.replace('通过数', str(groups[i][4])) tmp = tmp.replace('状态', str(groups[i][5])) except Exception as e: logger.error(str(e)) trs += tmp html = html.replace('mailbody', trs) mail = Mail([f'./lib/{casename}_result.xlsx'], [f'{casename}_测试报告详情.xlsx']) mail.send(html)
def run(casename="多线程APP1"): # 运行的相对路径 path = '.' # 用例路径 casepath = '' resultpath = '' logger.info("开始用例") try: casepath = sys.argv[1] except: casepath = '' casefile = casename # 为空,则使用默认的 if casepath == '': casepath = path + '/data/cases/' + casefile + '.xls' resultpath = path + '/outputs/resultcases/chint结果-' + casefile + '.xls' else: # 如果是绝对路径,就使用绝对路径 if casepath.find(':') >= 0: # 获取用例文件名 resultpath = path + '/outputs/resultcases/chint结果-' + casepath[ casepath.rfind('\\') + 1:] else: logger.error('非法用例路径') config.get_config(path + '/conf/conf.properties') # logger.info(config.config) # mysql = Mysql() # mysql.init_mysql(path + '/lib/userinfo.sql') logger.info("传递的参数是:%s,%s" % (casepath, resultpath)) datadriven.getparams(casepath, resultpath) # print(datadriven.alllist) ### 下面三行代码为BeautifulReport 报告 # suite = unittest.defaultTestLoader.discover("./utest/", pattern="WebTest.py", top_level_dir=None) # # 生成执行用例的对象 # runner = bf(suite) # runner.report(filename='./report/test'+ casefile + '.html', description=datadriven.title) ### 下面几行代码为HTMLTestRunner 报告 suite = unittest.defaultTestLoader.discover("./utest/", pattern="WebTest.py", top_level_dir=None) # 生成执行用例的对象 runner = bf(suite) runner.report(filename='./outputs/report/test' + casefile + '.html', description=datadriven.title) # 记录结束时间,写入结果文件 endtime = str( datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) # 用例结束时间 datadriven.writer.set_sheet((datadriven.writer.get_sheets())[0]) datadriven.writer.write(1, 4, endtime) # 对结果文件第二行第四列写入开始时间 # 保存结果文件 datadriven.writer.save_close() # # 得到报告数据 res = Res() r = res.get_res(resultpath) # 读取配置文件 config.get_config('./conf/conf.properties') # logger.info(config.config) # logger.info(config.config['mail']) # 修改邮件数据 html = config.config['mailtxt'] # 读取报告模板 html = html.replace('title', r['title']) # 替换模板中的数据 html = html.replace('runtype', r['runtype']) html = html.replace('passrate', r['passrate']) html = html.replace('status', r['status']) html = html.replace('casecount', r['casecount']) html = html.replace('starttime', r['starttime']) html = html.replace('endtime', r['endtime']) if r['status'] == 'Fail': html = html.replace('#00d800', 'red') # 根据结果决定是否发送邮件 if r['status'] == 'Fail': logger.info("开始发送邮件") mail = Mail() # mail.send(html) logger.info("结束用例")
writer.set_sheet(sheetname[0]) endtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) writer.write(1, 4, endtime) writer.save_close() # 结果统计 res = Res() details = res.get_res('./lib/result-%s.xls' % casename) r = res.get_groups('./lib/result-%s.xls' % casename) # 关闭jvm shutdown() # 邮件处理 mail = Mail() htmlmodule = Txt('./conf/' + config.config['mailtxt']) html = htmlmodule.read()[0] # 对模块文本进行处理 # 替换总体统计信息 sumlist = ['status', 'passrate', 'starttime', 'endtime'] for s in sumlist: html = html.replace(s, details[s]) # 生成HTML的一行内容 alltrs = '' for s in r: tr = '<tr><td width="100" height="28" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">分组信息</td><td width="80" height="28" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">用例总数</td><td width="80" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">通过数</td><td width="80" align="center" bgcolor="#FFFFFF" style="border:1px solid #ccc;">状态</td></tr>' tr = tr.replace('分组信息', str(s[0])) tr = tr.replace('用例总数', str(s[1])) tr = tr.replace('通过数', str(s[2]))
import unittest from common.HTMLTestRunner_cn import HTMLTestRunner import os import time from common.mail import Mail yag = Mail() case_path = os.path.join(os.getcwd(), 'case') report_path = os.path.join(os.getcwd(), 'report') def all_case(): '''构造待执行的测试用例''' discover = unittest.defaultTestLoader.discover(case_path, pattern='test*.py') print(discover) return discover if __name__ == '__main__': now_time = time.strftime('%Y-%m-%d %H_%M_%S') filename = os.path.join(report_path, now_time + 'result.html') fp = open(filename, 'wb') # 执行测试,输出HTML格式的测试报告 runner = HTMLTestRunner(stream=fp, title='自动化测试报告', description='用例执行情况:') runner.run(all_case()) fp.close() yag.send_mail(filename) #发送邮件