#runtest.py
importsmtplibimportunittestimporttimeimportxlrdimportosfrom HTMLTestRunner importHTMLTestRunnerfrom email.header importHeaderfrom email.mime.text importMIMEText
#匯入baseinfoimportbaseinfofrom email.mime.multipart importMIMEMultipartdefget_sender():
filePath= os.path.join(os.getcwd(),‘user_info‘)print("get_sender")print(filePath)
fileName= filePath + ‘\Mail_info.xlsx‘
print(fileName)
readExcel=xlrd.open_workbook(fileName)
sender= readExcel.sheet_by_name(‘sender‘)
get_sender= sender.row_values(1)#print(get_sender)
returnget_senderdefget_Receiver():
filePath= os.path.join(os.getcwd(),‘user_info‘)
print("get_Receiver()")print(fileName)
receiver_sheet= readExcel.sheet_by_name(‘receiver‘)
receiver_temp=receiver_sheet.col_values(0)
getReceiver= receiver_temp[1:]print(getReceiver)print(type(getReceiver))returngetReceiverdefget_NewReport(testreport):
dirs=os.listdir(testreport)
dirs.sort()
newreportname= dirs[-1]print(‘The new report name: {0}‘.format(newreportname))
file_new=os.path.join(testreport, newreportname)print(file_new)returnfile_newdefsend_Mail(file_new):
f= open(file_new, ‘rb‘)#讀取測試報告正文
mail_body =f.read()
f.close()
try:
#透過 baseinfo 引用 __init__ 初始化的引數值
smtp= smtplib.SMTP(baseinfo.Smtp_Server, 25)
sender=baseinfo.Smtp_Sender
password=baseinfo.Smtp_Sender_Password
receiver=baseinfo.Smtp_Receiver
smtp.login(sender, password)
msg=MIMEMultipart()#編寫html型別的郵件正文,MIMEtext()用於定義郵件正文
#傳送正文
text= MIMEText(mail_body, ‘html‘, ‘utf-8‘)
text[‘Subject‘] = Header(‘自動化測試報告‘, ‘utf-8‘)
msg.attach(text)#傳送附件
#Header()用於定義郵件標題
msg[‘Subject‘] = Header(‘自動化測試報告‘, ‘utf-8‘)
msg_file= MIMEText(mail_body, ‘html‘, ‘utf-8‘)
msg_file[‘Content-Type‘] = ‘application/octet-stream‘msg_file["Content-Disposition"] = ‘attachment; filename="TestReport.html"‘msg.attach(msg_file)#定義發件人,如果不寫,發件人為空
msg[‘From‘] =sender#定義收件人,如果不寫,收件人為空
msg[‘To‘] = ",".join(receiver)
tmp =smtp.sendmail(sender, receiver, msg.as_string())print(receiver)
print(tmp)
smtp.quit()returnTrueexceptsmtplib.SMTPException as e:print(str(e))returnFalseif __name__ == ‘__main__‘:
test_dir= os.path.join(os.getcwd(),‘test_case‘)print(test_dir)
report_dir= os.path.join(os.getcwd(),‘report‘)
test_discover= unittest.defaultTestLoader.discover(test_dir,pattern=‘test*.py‘)print("test_discover")
now= time.strftime("%Y-%m-%d-%H_%M_%S")
filename= report_dir+‘/result-‘+now+‘.html‘fp= open(filename, ‘wb‘)
runner= HTMLTestRunner(stream = fp, title = ‘UI自動化測試報告‘, description = ‘用例執行情況‘)#runner = unittest.TextTestRunner()
runner.run(test_discover)
fp.close()
new_report=get_NewReport(report_dir)
mail=send_Mail(new_report)ifmail:print("傳送成功!")else:print("傳送失敗!")
#runtest.py
importsmtplibimportunittestimporttimeimportxlrdimportosfrom HTMLTestRunner importHTMLTestRunnerfrom email.header importHeaderfrom email.mime.text importMIMEText
#匯入baseinfoimportbaseinfofrom email.mime.multipart importMIMEMultipartdefget_sender():
filePath= os.path.join(os.getcwd(),‘user_info‘)print("get_sender")print(filePath)
fileName= filePath + ‘\Mail_info.xlsx‘
print(fileName)
readExcel=xlrd.open_workbook(fileName)
sender= readExcel.sheet_by_name(‘sender‘)
get_sender= sender.row_values(1)#print(get_sender)
returnget_senderdefget_Receiver():
filePath= os.path.join(os.getcwd(),‘user_info‘)
fileName= filePath + ‘\Mail_info.xlsx‘
print("get_Receiver()")print(fileName)
readExcel=xlrd.open_workbook(fileName)
receiver_sheet= readExcel.sheet_by_name(‘receiver‘)
receiver_temp=receiver_sheet.col_values(0)
getReceiver= receiver_temp[1:]print(getReceiver)print(type(getReceiver))returngetReceiverdefget_NewReport(testreport):
dirs=os.listdir(testreport)
dirs.sort()
newreportname= dirs[-1]print(‘The new report name: {0}‘.format(newreportname))
file_new=os.path.join(testreport, newreportname)print(file_new)returnfile_newdefsend_Mail(file_new):
f= open(file_new, ‘rb‘)#讀取測試報告正文
mail_body =f.read()
f.close()
try:
#透過 baseinfo 引用 __init__ 初始化的引數值
smtp= smtplib.SMTP(baseinfo.Smtp_Server, 25)
sender=baseinfo.Smtp_Sender
password=baseinfo.Smtp_Sender_Password
receiver=baseinfo.Smtp_Receiver
smtp.login(sender, password)
msg=MIMEMultipart()#編寫html型別的郵件正文,MIMEtext()用於定義郵件正文
#傳送正文
text= MIMEText(mail_body, ‘html‘, ‘utf-8‘)
text[‘Subject‘] = Header(‘自動化測試報告‘, ‘utf-8‘)
msg.attach(text)#傳送附件
#Header()用於定義郵件標題
msg[‘Subject‘] = Header(‘自動化測試報告‘, ‘utf-8‘)
msg_file= MIMEText(mail_body, ‘html‘, ‘utf-8‘)
msg_file[‘Content-Type‘] = ‘application/octet-stream‘msg_file["Content-Disposition"] = ‘attachment; filename="TestReport.html"‘msg.attach(msg_file)#定義發件人,如果不寫,發件人為空
msg[‘From‘] =sender#定義收件人,如果不寫,收件人為空
msg[‘To‘] = ",".join(receiver)
tmp =smtp.sendmail(sender, receiver, msg.as_string())print(receiver)
print(tmp)
smtp.quit()returnTrueexceptsmtplib.SMTPException as e:print(str(e))returnFalseif __name__ == ‘__main__‘:
test_dir= os.path.join(os.getcwd(),‘test_case‘)print(test_dir)
report_dir= os.path.join(os.getcwd(),‘report‘)
test_discover= unittest.defaultTestLoader.discover(test_dir,pattern=‘test*.py‘)print("test_discover")
now= time.strftime("%Y-%m-%d-%H_%M_%S")
filename= report_dir+‘/result-‘+now+‘.html‘fp= open(filename, ‘wb‘)
runner= HTMLTestRunner(stream = fp, title = ‘UI自動化測試報告‘, description = ‘用例執行情況‘)#runner = unittest.TextTestRunner()
runner.run(test_discover)
fp.close()
new_report=get_NewReport(report_dir)
mail=send_Mail(new_report)ifmail:print("傳送成功!")else:print("傳送失敗!")