python unittest接口测试报告生成,邮件发送及附件

#coding=utf-8
import unittest
import HTMLTestRunner
import time
import os
import smtplib
from common import readConfig
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
localReadConfig = readConfig.ReadConfig()

#用例目录
test_suite_dir = './test_case/'
#报告目录
report_dir = './report/'
now = time.strftime('%Y-%m-%d %H_%M_%S', time.localtime(time.time()))
def creatsuite():
    testunit = unittest.TestSuite()
    #discover 方法定义
    discover = unittest.defaultTestLoader.discover(test_suite_dir, 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 = creatsuite()

#定义发送邮件
def new_report(testreport):
    lists = os.listdir(testreport)
    lists.sort(key=lambda fn: os.path.getatime(testreport + "\\" + fn))
    file_new = os.path.join(testreport,lists[-1])
    print(file_new)
    return file_new


def send_mail(new_report,filename):
    '''
    :param new_report:获取最新的文件
    :param new_report_fail:获取最新的文件的路径
    :param now:当前生成报告的时间
    :return:

    :param sender 读取配置文件中发件人
    :param sendpwd 读取配置文件中发件人密码
    :param receiver 读取配置文件中收件人
    :return:
    '''

    sender = localReadConfig.get_email('sender')
    sendpwd = localReadConfig.get_email('sendpwd')
    receiver = localReadConfig.get_email('receiver')
    print(receiver)

    #获取报告文件:'related'43行
    f = open(new_report,'rb')
    body_main = f.read()

    msg = MIMEMultipart()
    # 邮件标题
    msg['Subject'] = Header('自动化测试报告','utf-8')
    msg['From'] = sender
    msg['To'] = receiver
    #邮件内容
    text = MIMEText(body_main,'html','utf-8')
    msg.attach(text)

    #发送附件
    att = MIMEApplication(open(filename, 'rb').read())
    att['Content-Type'] = 'application/octet-stream'
    att.add_header('Content-Disposition', 'attachment', filename = filename)
    msg.attach(att)

    smtp = smtplib.SMTP()
    smtp.connect(localReadConfig.get_smtp('smtp'))
    smtp.login(sender,sendpwd)

    #将receiver string类型转换成list
    # b = []
    # for n in str(receiver).split(","):
    #     b.append(n)
    # print(b)
    smtp.sendmail(sender,receiver.spilt(","),msg.as_string())


if __name__ == '__main__':

    filename = report_dir + now + 'result.html'
    fp = open(filename, 'wb')
    runner = HTMLTestRunner.HTMLTestRunner(
        stream=fp,
        title=u'接口测试报告',
        description=u'用例执行结果:')

    # 执行测试用例
    runner.run(alltestnames)
    fp.close()
    #搜索最新生成的文件
    new_report = new_report(report_dir)
    #发送邮件
    send_mail(new_report,filename)

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值