Python 2.7+Appium 1.4.16+Pycharm
1、生成测试报告report
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
import HTMLTestRunner
import sys
reload(sys)
#sys.setfaultencoding('utf-8')
class report():
def sgReport(self, suite):
now = time.strftime("%Y-%m-%d %H-%M-%S")
# 定义报告存放路径
filepath = './report/' + now + 'test_result.html'
fp = open(filepath, "wb")
testreport = os.path.join(os.getcwd(), 'report')
dirs = os.listdir(testreport)
dirs.sort()
newreportname = dirs[-1]
print('The new report name: {0}'.format(newreportname))
file_new = os.path.join(testreport, newreportname)
fp = open(file_new, 'wb')
runner = HTMLTestRunner.HTMLTestRunner(
stream = fp,
title = '测试结果',
description = '测试报告'
)
runner.run(suite)
fp.close()
return file_new
2、发送测试报告邮件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import sys
from email.mime.image import MIMEImage
from email.header import Header
def sendEmail(file_new):
_user = "xxx@qq.com"
_pwd = "xxxxx"
_to = "xxx@xx.com"
#many receivers
#_to = ['xxx@qq.com', 'xxxx@qq.com']
#msg['To'] = ";".join(_to)
f = open(file_new, 'rb')
mail_body = f.read()
f.close()
msg = MIMEMultipart("mixed")
#attach
sendfile = open(r'D:\xx.txt', 'rb').read()
text_att = MIMEText(sendfile, 'base64', 'utf-8')
text_att["Content-Type"] = 'application/octet-stream'
text_att.add_header('Content-Disposition', 'attachment', filename ='xx.txt' )
msg.attach(text_att)
#content
body = "python test"
msg.attach(MIMEText(body, 'plain'))
#image
sendimagefile = open(r'D:\xx.jpg', 'rb').read()
image = MIMEImage(sendimagefile)
image.add_header('Content-ID', '<image1>')
image["COntent-Disposition"] = 'attachment; filename = "xx.jpg"'
msg.attach(image)
#html
html = """
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.baidu.com">link</a> you wanted.<br>
</p>
</body>
</html>
"""
text_html = MIMEText(html, 'html', 'utf-8')
text_html["Content-Disposition"] = 'attachment; filename = "texthtml.html"'
msg.attach(text_html)
msg["From"] = _user
msg["To"] = _to
msg["Subject"] = "Python test"
try:
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
s.login(_user, _pwd)
s.sendmail(_user, _to, msg.as_string())
s.quit()
print "sendemail Success!"
except smtplib.SMTPException, e:
print "Failed, %s" % e
3、qq邮箱的密码需要使用授权码,可参考文章:
http://www.jb51.net/article/105078.htm