python+android自动化测试生成report、发送report邮件

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值