Python+Selenium:自动化测试报告的生成

TestRunner.py发送本地地址

import HTMLTestRunner
import os
import unittest
import time
import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 设置报告文件保存路径
report_path = os.path.dirname(os.path.abspath('.')) + '/test_report/'
# 获取系统当前时间
now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))

# 设置报告名称格式
HtmlFile = report_path + now + "HTMLtemplate.html"
fp = open(HtmlFile, "wb")

# 发送到邮箱

#邮箱服务器
smtpserver = 'smtp.163.com'
#发送邮箱
sender = '[email protected]'
#发送邮箱的账号密码
username = '[email protected]'
password = 'password'
#发送邮件主题
subject = '自动化测试报告'
#接收邮箱
receiver = '[email protected]'
#文件内容
msg = MIMEText( HtmlFile,'html','utf-8')
msg['Subject'] = Header(subject,'utf-8')

msg['from'] = '[email protected]'
msg['to'] = '[email protected]'

smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login('[email protected]','password')
a1=smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()

# 构建suite

listaa = 'C:\\Users\suxiahua\PycharmProjects\\untitled\\automation_framework_demo\\testsuits'


def creatsuite1():
    testunit = unittest.TestSuite()
    # discover 方法定义
    discover = unittest.defaultTestLoader.discover(listaa, pattern='test_*.py', top_level_dir=None)
    # discover 方法筛选出来的用例,循环添加到测试套件中
    for test_suite in discover:
        for test_case in test_suite:
            testunit.addTests(test_case)
            print(testunit)

    return testunit

alltestnames = creatsuite1()


if __name__ == '__main__':
    # 初始化一个HTMLTestRunner实例对象,用来生成报告
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"某某项目测试报告", description=u"用例测试情况")
    # 开始执行测试套件
    runner.run(alltestnames)


另一种发送报告内容

import HTMLTestRunner
import os
import unittest
import time
import smtplib
from email.mime.text import MIMEText
from email.header import Header

def report():
     # 设置报告文件保存路径

    report_path = os.path.dirname(os.path.abspath('.')) + '/test_report/'
    # 获取系统当前时间
    now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))

        # 设置报告名称格式
    HtmlFile = report_path + now +"HTMLtemplate.html"
    fp = open(HtmlFile, "wb")
    return fp,HtmlFile


# 发送到邮箱
def send_mail(HtmlFile):
    #邮箱服务器
    smtpserver = 'smtp.163.com'
    #发送邮箱
    sender = '[email protected]'
    #发送邮箱的账号密码
    username = '[email protected]'
    password = 'password'
    #发送邮件主题
    subject = '自动化测试报告'
    #接收邮箱
    receiver = '[email protected]'
    #文件内容
    f = open(HtmlFile, 'rb')
    mail_body = f.read()
    mail_body=mail_body.decode('utf8')
    f.close()
    msg = MIMEText(mail_body,'html','utf-8')
    msg['Subject'] = Header(subject,'utf-8')

    msg['from'] = '[email protected]'
    msg['to'] = '[email protected]'

    smtp = smtplib.SMTP()
    smtp.connect(smtpserver)
    smtp.login(username,password)
    smtp.sendmail(sender,receiver,msg.as_string())
    smtp.quit()

# 构建suite

listaa = 'C:\\Users\suxiahua\PycharmProjects\\untitled\\automation_framework_demo\\testsuits'


def creatsuite1():
    testunit = unittest.TestSuite()
    # discover 方法定义
    discover = unittest.defaultTestLoader.discover(listaa, pattern='test_baidu_search1.py', top_level_dir=None)
    # discover 方法筛选出来的用例,循环添加到测试套件中
    for test_suite in discover:
        for test_case in test_suite:
            testunit.addTests(test_case)
            print(testunit)

    return testunit

alltestnames = creatsuite1()

if __name__ == '__main__':
    fp,HtmlFile1 = report()
    # 初始化一个HTMLTestRunner实例对象,用来生成报告
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title=u"某某项目测试报告", description=u"用例测试情况")
    # 开始执行测试套件
    runner.run(alltestnames)
    fp.close()
    send_mail(HtmlFile1)



你可能感兴趣的:(python+selenium)